From 5cbb08ccb4701c0a0ce24efd0c13227a17bea716 Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@gmail.com>
Date: Sat, 5 Jan 2013 18:08:58 +0100
Subject: [PATCH] New TagParser

---
 lib/music.rb            | 12 ++++++------
 lib/tag.rb              |  4 ++--
 lib/tag_parser.rb       | 21 +++++++++++++++++++++
 spec/tag_parser_spec.rb | 10 ++++++++++
 4 files changed, 39 insertions(+), 8 deletions(-)
 create mode 100644 lib/tag_parser.rb
 create mode 100644 spec/tag_parser_spec.rb

diff --git a/lib/music.rb b/lib/music.rb
index 946e45d..cd15202 100644
--- a/lib/music.rb
+++ b/lib/music.rb
@@ -5,11 +5,11 @@ require 'active_record'
 # Music Object
 #
 # Its attribut are :
-#  * title
-#  * author
-#  * url
-#  * sender
-#  * file
-#  * created_ad
+# * title
+# * author
+# * url
+# * sender
+# * file
+# * created_ad
 class Music < ActiveRecord::Base
 end
diff --git a/lib/tag.rb b/lib/tag.rb
index e92b897..d850b5e 100644
--- a/lib/tag.rb
+++ b/lib/tag.rb
@@ -5,7 +5,7 @@ require 'active_record'
 # Tag Object
 #
 # Its attribute are :
-#  * video
-#  * tage
+# * video
+# * tage
 class Tag < ActiveRecord::Base
 end
diff --git a/lib/tag_parser.rb b/lib/tag_parser.rb
new file mode 100644
index 0000000..c902155
--- /dev/null
+++ b/lib/tag_parser.rb
@@ -0,0 +1,21 @@
+# Parse a text and extract tags.
+#
+# A tag starts with a "#" and can contains the following characters : [a-zA-Z0-9_-].
+class TagParser
+    # Parse a text and extract tags.
+    #
+    # * *Args*:
+    #   - +text+: the text to parse
+    #
+    # * *Returns*:
+    #   - an array containing the tags
+    def self.parse! (text)
+        tags = []
+
+        text.gsub(/(#[a-zA-Z0-9_-]*)/).each do |tag|
+            tags << tag
+        end
+
+        return tags
+    end
+end
diff --git a/spec/tag_parser_spec.rb b/spec/tag_parser_spec.rb
new file mode 100644
index 0000000..fb11ae0
--- /dev/null
+++ b/spec/tag_parser_spec.rb
@@ -0,0 +1,10 @@
+require_relative '../lib/tag_parser.rb'
+
+describe TagParser do
+    describe :parse! do
+        it 'return tags in a text given' do
+            tags = TagParser.parse! "hey #i contain #two tags !"
+            tags.should == ["#i", "#two"]
+        end
+    end
+end
-- 
GitLab