string_utility_belt 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -1,5 +1,3 @@
1
- *.gemspec
2
-
3
1
  pkg/**/*
4
2
  nbproject/**/*
5
3
  *.swp
@@ -1,10 +1,7 @@
1
- require File.dirname(__FILE__) + '/configurations'
2
- require File.dirname(__FILE__) + '/helpers'
1
+ require File.dirname(__FILE__) + '/helper'
3
2
 
4
3
  module RegexMe
5
4
  module Builders
6
- include RegexMe::Configurations::Builder
7
-
8
5
  def regex_me_to_search_ruby(options = {})
9
6
  regex_me_to_search(:ruby, options)
10
7
  end
@@ -15,85 +12,10 @@ module RegexMe
15
12
 
16
13
  private
17
14
 
18
- def options_handler(options)
19
- handled = \
20
- {:case_insensitive => (options[:case_insensitive] ? Regexp::IGNORECASE : nil ),
21
- :multiline => (options[:multiline] ? Regexp::MULTILINE : nil ),
22
- :or => (options[:or] == false ? false : true)}
23
-
24
- return options.merge(handled)
25
- end
26
-
27
15
  def regex_me_to_search(env, options)
28
16
  return EMPTYs[env] if self.strip.empty?
29
17
 
30
- execute_builder(env, options)
31
- end
32
-
33
- def execute_builder(env, options)
34
- opt_handled = options_handler(options)
35
-
36
- builder_result = builder(env, opt_handled)
37
-
38
- case env
39
- when :ruby
40
- options = [opt_handled[:case_insensitive], opt_handled[:multiline]].compact
41
- Regexp.new(builder_result, *options)
42
- when :mysql
43
- builder_result
44
- end
45
- end
46
-
47
- def builder(border_to, options)
48
- string = self
49
-
50
- lcv = options[:latin_chars_variations]
51
-
52
- if options[:exact_phrase]
53
- @regexp = \
54
- string \
55
- .strip.simple_space \
56
- .regex_latin_ci_list \
57
- .gsub(/\s/, WORDS_INTERVAL_PATTERN_FOR_EXACT_PHRASES) \
58
- .regex_builder(:or => false,
59
- :border => {:to => border_to,
60
- :direction => :both})
61
- else
62
- @regexp = '('
63
-
64
- for word in string.strip.split
65
- if options[:exact_word]
66
- @regexp << word.regex_builder(:border => {:to => border_to, :direction => :both}, :latin_chars_variations => lcv, :or => true)
67
- elsif have_the_any_char?(word)
68
- @regexp << word.regex_builder(:any => true, :border => border(border_to, word) , :latin_chars_variations => lcv, :or => true)
69
- else
70
- @regexp << word.regex_builder(:latin_chars_variations => lcv, :or => true)
71
- end
72
- end
73
-
74
- @regexp = (@regexp << ')').sub!(/\|\)/,')')
75
- end
76
-
77
- return @regexp
78
- end
79
-
80
- def have_the_any_char?(string)
81
- string.include?('*')
82
- end
83
-
84
- def border(to, word)
85
- direction = nil
86
-
87
- case word
88
- when/^\*/
89
- direction = :right
90
- when /\*$/
91
- direction = :left
92
- when /^.*\*.*$/
93
- direction = :both
94
- end
95
-
96
- {:to => to, :direction => direction}
18
+ RegexMe::Helper.new(self).generate_for(env, options)
97
19
  end
98
20
  end
99
21
  end
@@ -4,6 +4,31 @@ module RegexMe
4
4
  EMPTYs = {:ruby => //, :mysql => ''}
5
5
  WORDS_INTERVAL_PATTERN_FOR_EXACT_PHRASES = '[^0-9a-zA-Z\_]+'
6
6
  end
7
+
8
+ module Variation
9
+ A_VARIATIONS = "(a|à|á|â|ã|ä)"
10
+ E_VARIATIONS = "(e|è|é|ê|ë)"
11
+ I_VARIATIONS = "(i|ì|í|î|ï)"
12
+ O_VARIATIONS = "(o|ò|ó|ô|õ|ö)"
13
+ U_VARIATIONS = "(u|ù|ú|û|ü)"
14
+ C_VARIATIONS = "(c|ç)"
15
+ N_VARIATIONS = "(n|ñ)"
16
+
17
+ LATIN_CHARS_VARIATIONS = [A_VARIATIONS,
18
+ E_VARIATIONS,
19
+ I_VARIATIONS,
20
+ O_VARIATIONS,
21
+ U_VARIATIONS,
22
+ C_VARIATIONS,
23
+ N_VARIATIONS]
24
+ end
25
+
26
+ module Border
27
+ BORDER_TO = {
28
+ :ruby => {:left => '\b', :right => '\b' },
29
+ :mysql => {:left => '[[:<:]]', :right => '[[:>:]]' }
30
+ }
31
+ end
7
32
  end
8
33
  end
9
34
 
@@ -0,0 +1,168 @@
1
+ # coding: utf-8
2
+ require File.dirname(__FILE__) + '/configurations'
3
+
4
+ module RegexMe
5
+ class Helper
6
+ include RegexMe::Configurations::Builder
7
+ include RegexMe::Configurations::Variation
8
+ include RegexMe::Configurations::Border
9
+
10
+ attr_accessor :string
11
+
12
+ def initialize(string = "")
13
+ @string = string
14
+ end
15
+
16
+ def generate_for(env, options)
17
+ @env = env
18
+ @options = options_handler(options)
19
+
20
+ regexp = factory
21
+
22
+ case @env
23
+ when :ruby
24
+ options = [@options[:case_insensitive], @options[:multiline]].compact
25
+ return Regexp.new(regexp, *options)
26
+ when :mysql
27
+ return regexp
28
+ end
29
+ end
30
+
31
+ def set_latin_variations(string = nil)
32
+ memo = ""
33
+ text = string || @string
34
+
35
+ text.each_char do |char|
36
+ changed = false
37
+
38
+ for variations in LATIN_CHARS_VARIATIONS
39
+ variations_pattern = Regexp.new(variations, Regexp::IGNORECASE)
40
+
41
+ if char =~ variations_pattern
42
+ changed = true
43
+ memo.insert(-1, variations)
44
+ break
45
+ end
46
+ end
47
+
48
+ memo.insert(-1, char) unless changed
49
+ end
50
+
51
+ return text.replace(memo)
52
+ end
53
+
54
+ def builder(text, options)
55
+ @text = text
56
+
57
+ if options[:any]
58
+ replace_the_any_char_per_any_pattern
59
+ end
60
+
61
+ if options[:latin_chars_variations]
62
+ set_latin_variations(@text)
63
+ end
64
+
65
+ if options[:border]
66
+ insert_border(options[:border])
67
+ end
68
+
69
+ if options[:or]
70
+ insert_OR
71
+ end
72
+
73
+ return @text
74
+ end
75
+
76
+ private
77
+
78
+ def options_handler(options)
79
+ handled = \
80
+ {:case_insensitive => (options[:case_insensitive] ? Regexp::IGNORECASE : nil ),
81
+ :multiline => (options[:multiline] ? Regexp::MULTILINE : nil ),
82
+ :or => (options[:or] == false ? false : true)}
83
+
84
+ return options.merge(handled)
85
+ end
86
+
87
+ def factory
88
+ lcv = @options[:latin_chars_variations]
89
+
90
+ if @options[:exact_phrase]
91
+ regexp = \
92
+ set_latin_variations(@string.strip.simple_space) \
93
+ .gsub(/\s/, WORDS_INTERVAL_PATTERN_FOR_EXACT_PHRASES)
94
+
95
+ @regexp = \
96
+ builder(regexp, :or => false,
97
+ :border => {:to => @env,
98
+ :direction => :both})
99
+ else
100
+ @regexp = '('
101
+
102
+ for word in @string.strip.split
103
+ if @options[:exact_word]
104
+ @regexp << builder(word, :border => {:to => @env, :direction => :both},
105
+ :latin_chars_variations => lcv,
106
+ :or => true)
107
+
108
+ elsif have_the_any_char?(word)
109
+ @regexp << builder(word, :any => true,
110
+ :border => border(@env, word),
111
+ :latin_chars_variations => lcv,
112
+ :or => true)
113
+
114
+ else
115
+ @regexp << builder(word, :latin_chars_variations => lcv,
116
+ :or => true)
117
+ end
118
+ end
119
+
120
+ @regexp = (@regexp << ')').sub!(/\|\)/,')')
121
+ end
122
+
123
+ return @regexp
124
+ end
125
+
126
+ def have_the_any_char?(string)
127
+ string.include?('*')
128
+ end
129
+
130
+ def border(to, word)
131
+ direction = nil
132
+
133
+ case word
134
+ when/^\*/
135
+ direction = :right
136
+ when /\*$/
137
+ direction = :left
138
+ when /^.*\*.*$/
139
+ direction = :both
140
+ end
141
+
142
+ {:to => to, :direction => direction}
143
+ end
144
+
145
+ def replace_the_any_char_per_any_pattern
146
+ @text.gsub!(/\*/, '.*')
147
+ end
148
+
149
+ def insert_border(options)
150
+ border = BORDER_TO[options[:to]]
151
+
152
+ case options[:direction]
153
+ when :left
154
+ @text.insert(0, border[:left])
155
+ when :right
156
+ @text.insert(-1, border[:right])
157
+ when :both
158
+ @text.insert(0, border[:left]).insert(-1, border[:right])
159
+ else
160
+ @text
161
+ end
162
+ end
163
+
164
+ def insert_OR
165
+ @text.insert(-1, "|")
166
+ end
167
+ end
168
+ end
@@ -1,3 +1,3 @@
1
1
  module StringUtilityBelt
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "string_utility_belt/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "string_utility_belt"
7
+ s.version = StringUtilityBelt::VERSION
8
+ s.authors = ["Rodrigo Serradura"]
9
+ s.email = ["rserradura@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Useful methods to handle strings}
12
+ s.description = %q{Adds new features for String objects.}
13
+
14
+ s.rubyforge_project = "string_utility_belt"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ s.add_dependency("htmlentities", "4.3.0")
21
+ end
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', 'test_helper')
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
2
 
3
3
  class RegexMeToSearchTest < Test::Unit::TestCase
4
4
 
@@ -1,49 +1,62 @@
1
1
  # coding: utf-8
2
2
 
3
- require File.join(File.dirname(__FILE__), '..', 'test_helper')
3
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
4
4
 
5
5
  class RegexMeHelperTest < Test::Unit::TestCase
6
6
 
7
+ def setup
8
+ @rmh = RegexMe::Helper.new
9
+ end
10
+
7
11
  def test_should_create_a_string_with_the_latin_char_variations_for_the_letter_A
8
- assert_equal("(a|à|á|â|ã|ä)", "a".regex_latin_ci_list)
12
+ @rmh.string = "a"
13
+ assert_equal("(a|à|á|â|ã|ä)", @rmh.set_latin_variations)
9
14
  end
10
15
 
11
16
  def test_should_create_a_string_with_the_latin_char_variations_for_the_letter_E
12
- assert_equal("(e|è|é|ê|ë)", "e".regex_latin_ci_list)
17
+ @rmh.string = "e"
18
+ assert_equal("(e|è|é|ê|ë)", @rmh.set_latin_variations)
13
19
  end
14
20
 
15
21
  def test_should_create_a_string_with_the_latin_char_variations_for_the_letter_I
16
- assert_equal("(i|ì|í|î|ï)", "i".regex_latin_ci_list)
22
+ @rmh.string = "i"
23
+ assert_equal("(i|ì|í|î|ï)", @rmh.set_latin_variations)
17
24
  end
18
25
 
19
26
  def test_should_create_a_string_with_the_latin_char_variations_for_the_letter_O
20
- assert_equal("(o|ò|ó|ô|õ|ö)", "o".regex_latin_ci_list)
27
+ @rmh.string = "o"
28
+ assert_equal("(o|ò|ó|ô|õ|ö)", @rmh.set_latin_variations)
21
29
  end
22
30
 
23
31
  def test_should_create_a_string_with_the_latin_char_variations_for_the_letter_U
24
- assert_equal("(u|ù|ú|û|ü)", "u".regex_latin_ci_list)
32
+ @rmh.string = "u"
33
+ assert_equal("(u|ù|ú|û|ü)", @rmh.set_latin_variations)
25
34
  end
26
35
 
27
36
  def test_should_create_a_string_with_the_latin_char_variations_for_the_letter_C
28
- assert_equal("(c|ç)", "c".regex_latin_ci_list)
37
+ @rmh.string = "c"
38
+ assert_equal("(c|ç)", @rmh.set_latin_variations)
29
39
  end
30
40
 
31
41
  def test_should_create_a_string_with_the_latin_char_variations_for_the_letter_N
32
- assert_equal("(n|ñ)", "n".regex_latin_ci_list)
42
+ @rmh.string = "n"
43
+ assert_equal("(n|ñ)", @rmh.set_latin_variations)
33
44
  end
34
45
 
35
46
  def test_should_replace_all_chars_that_found_in_latin_variantion_list
36
47
  expected = "r(e|è|é|ê|ë)g(e|è|é|ê|ë)xp (i|ì|í|î|ï)s p(o|ò|ó|ô|õ|ö)w(e|è|é|ê|ë)rf(u|ù|ú|û|ü)ll"
37
48
 
38
- assert_equal expected, "regexp is powerfull".regex_latin_ci_list
49
+ @rmh.string = "regexp is powerfull"
50
+
51
+ assert_equal expected, @rmh.set_latin_variations
39
52
  end
40
53
 
41
54
  def test_should_returns_the_OR_metachar_if_or_options_was_passed
42
- assert_equal "OR == |", "OR == ".regex_builder(:or => true)
55
+ assert_equal "OR == |", @rmh.builder("OR == ", :or => true)
43
56
  end
44
57
 
45
58
  def test_should_not_return_OR_when_the_OR_options_is_false
46
- assert_equal "without OR", "without OR".regex_builder(:or => false)
59
+ assert_equal "without OR", @rmh.builder("without OR", :or => false)
47
60
  end
48
61
 
49
62
  def test_should_return_the_border_metachar_in_left_when_its_required_for_ruby
@@ -51,7 +64,7 @@ class RegexMeHelperTest < Test::Unit::TestCase
51
64
  expected = left + "string"
52
65
  border = {:to => :ruby, :direction => :left}
53
66
 
54
- assert_equal expected, "string".regex_builder(:border => border)
67
+ assert_equal expected, @rmh.builder("string", :border => border)
55
68
  end
56
69
 
57
70
  def test_should_return_the_border_metachar_in_right_when_its_required_for_ruby
@@ -59,7 +72,7 @@ class RegexMeHelperTest < Test::Unit::TestCase
59
72
  expected = "string" + right
60
73
  border = {:to => :ruby, :direction => :right}
61
74
 
62
- assert_equal expected, "string".regex_builder(:border => border)
75
+ assert_equal expected, @rmh.builder("string", :border => border)
63
76
  end
64
77
 
65
78
  def test_should_return_the_border_metachar_in_left_and_right_when_its_required_for_ruby
@@ -68,14 +81,14 @@ class RegexMeHelperTest < Test::Unit::TestCase
68
81
  expected = left + "string" + right
69
82
  border = {:to => :ruby, :direction => :both}
70
83
 
71
- assert_equal expected, "string".regex_builder(:border => border)
84
+ assert_equal expected, @rmh.builder("string", :border => border)
72
85
  end
73
86
 
74
87
  def test_for_ruby_should_return_the_same_text_if_the_border_to_direction_is_invalid
75
88
  text = "string"
76
89
  border = {:to => :ruby}
77
90
 
78
- assert_same text, text.regex_builder(:border => border)
91
+ assert_same text, @rmh.builder(text, :border => border)
79
92
  end
80
93
 
81
94
  def test_should_return_the_border_metachar_in_left_when_its_required_for_mysql
@@ -83,7 +96,7 @@ class RegexMeHelperTest < Test::Unit::TestCase
83
96
  expected = left + "string"
84
97
  border = {:to => :mysql, :direction => :left}
85
98
 
86
- assert_equal expected, "string".regex_builder(:border => border)
99
+ assert_equal expected, @rmh.builder("string", :border => border)
87
100
  end
88
101
 
89
102
  def test_should_return_the_border_metachar_in_right_when_its_required_for_mysql
@@ -91,7 +104,7 @@ class RegexMeHelperTest < Test::Unit::TestCase
91
104
  expected = "string" + right
92
105
  border = {:to => :mysql, :direction => :right}
93
106
 
94
- assert_equal expected, "string".regex_builder(:border => border)
107
+ assert_equal expected, @rmh.builder("string", :border => border)
95
108
  end
96
109
 
97
110
  def test_should_return_the_border_metachar_in_left_and_right_when_its_required_for_mysql
@@ -100,18 +113,18 @@ class RegexMeHelperTest < Test::Unit::TestCase
100
113
  expected = left + "string" + right
101
114
  border = {:to => :mysql, :direction => :both}
102
115
 
103
- assert_equal expected, "string".regex_builder(:border => border)
116
+ assert_equal expected, @rmh.builder("string", :border => border)
104
117
  end
105
118
 
106
119
  def test_for_mysql_should_return_the_same_text_if_the_border_to_direction_is_invalid
107
120
  text = "string"
108
121
  border = {:to => :mysql, :direction => nil}
109
122
 
110
- assert_same text, text.regex_builder(:border => border)
123
+ assert_same text, @rmh.builder(text, :border => border)
111
124
  end
112
125
 
113
126
  def test_should_return_a_mix_of_regexp_options
114
127
  expected = "f.*(o|ò|ó|ô|õ|ö)"
115
- assert_equal expected, "f*o".regex_builder(:any => true, :latin_chars_variations => true)
128
+ assert_equal expected, @rmh.builder("f*o", :any => true, :latin_chars_variations => true)
116
129
  end
117
130
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string_utility_belt
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 1
10
- version: 0.3.1
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Rodrigo Serradura
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-12 00:00:00 Z
18
+ date: 2011-07-13 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: htmlentities
@@ -55,15 +55,16 @@ files:
55
55
  - lib/string_utility_belt/regex_me.rb
56
56
  - lib/string_utility_belt/regex_me/lib/builders.rb
57
57
  - lib/string_utility_belt/regex_me/lib/configurations.rb
58
- - lib/string_utility_belt/regex_me/lib/helpers.rb
58
+ - lib/string_utility_belt/regex_me/lib/helper.rb
59
59
  - lib/string_utility_belt/regex_me/regex_me.rb
60
60
  - lib/string_utility_belt/tags.rb
61
61
  - lib/string_utility_belt/version.rb
62
+ - string_utility_belt.gemspec
62
63
  - test/string_utility_belt/entities_test.rb
63
64
  - test/string_utility_belt/general_test.rb
64
65
  - test/string_utility_belt/match_rank_test.rb
65
- - test/string_utility_belt/regex_me_helper_test.rb
66
- - test/string_utility_belt/regex_me_to_search_test.rb
66
+ - test/string_utility_belt/regex_me/builders_test.rb
67
+ - test/string_utility_belt/regex_me/helper_test.rb
67
68
  - test/string_utility_belt/tags_test.rb
68
69
  - test/test_helper.rb
69
70
  homepage: ""
@@ -1,102 +0,0 @@
1
- # coding: utf-8
2
-
3
- module StringUtilityBelt
4
- module RegexMe
5
- module Helper
6
- A_VARIATIONS = "(a|à|á|â|ã|ä)"
7
- E_VARIATIONS = "(e|è|é|ê|ë)"
8
- I_VARIATIONS = "(i|ì|í|î|ï)"
9
- O_VARIATIONS = "(o|ò|ó|ô|õ|ö)"
10
- U_VARIATIONS = "(u|ù|ú|û|ü)"
11
- C_VARIATIONS = "(c|ç)"
12
- N_VARIATIONS = "(n|ñ)"
13
-
14
- LATIN_CHARS_VARIATIONS = [A_VARIATIONS,
15
- E_VARIATIONS,
16
- I_VARIATIONS,
17
- O_VARIATIONS,
18
- U_VARIATIONS,
19
- C_VARIATIONS,
20
- N_VARIATIONS]
21
-
22
- BORDER_TO = {
23
- :ruby => {:left => '\b', :right => '\b' },
24
- :mysql => {:left => '[[:<:]]', :right => '[[:>:]]' }
25
- }
26
-
27
- def regex_latin_ci_list
28
- memo = ""
29
-
30
- self.each_char do |char|
31
- changed = false
32
-
33
- for variations in LATIN_CHARS_VARIATIONS
34
- variations_pattern = Regexp.new(variations, Regexp::IGNORECASE)
35
-
36
- if char =~ variations_pattern
37
- changed = true
38
- memo.insert(-1, variations)
39
- break
40
- end
41
- end
42
-
43
- memo.insert(-1, char) unless changed
44
- end
45
-
46
- self.replace(memo)
47
- end
48
-
49
- def regex_builder(options)
50
- if options[:any]
51
- replace_the_any_char_per_any_pattern
52
- end
53
-
54
- if options[:latin_chars_variations]
55
- replace_chars_includeds_in_latin_variation_list
56
- end
57
-
58
- if options[:border]
59
- insert_border(options[:border])
60
- end
61
-
62
- if options[:or]
63
- insert_OR
64
- end
65
-
66
- return self
67
- end
68
-
69
- private
70
- def replace_the_any_char_per_any_pattern
71
- self.gsub!(/\*/, '.*')
72
- end
73
-
74
- def replace_chars_includeds_in_latin_variation_list
75
- self.regex_latin_ci_list
76
- end
77
-
78
- def insert_border(options)
79
- border = BORDER_TO[options[:to]]
80
-
81
- case options[:direction]
82
- when :left
83
- self.insert(0, border[:left])
84
- when :right
85
- self.insert(-1, border[:right])
86
- when :both
87
- self.insert(0, border[:left]).insert(-1, border[:right])
88
- else
89
- self
90
- end
91
- end
92
-
93
- def insert_OR
94
- self.insert(-1, "|")
95
- end
96
- end
97
- end
98
- end
99
-
100
- class String
101
- include StringUtilityBelt::RegexMe::Helper
102
- end