string_enumerator 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +9 -27
- data/lib/string_enumerator.rb +23 -33
- data/lib/string_enumerator/version.rb +1 -1
- data/test/test_string_enumerator.rb +10 -11
- metadata +5 -5
data/README.rdoc
CHANGED
@@ -1,19 +1,9 @@
|
|
1
1
|
=string_enumerator
|
2
2
|
|
3
|
-
You provide a list of replacements ("replace [color] with red and blue")
|
3
|
+
You provide a list of replacements ("replace [color] with red and blue") and then call the <tt>#enumerate</tt> method on a <tt>String</tt>:
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
# Interface to StringEnumerator
|
8
|
-
def replacements
|
9
|
-
{ :color => [ 'red', 'blue' ] }
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
Then you call the <tt>#enumerate</tt> method on a <tt>String</tt>:
|
14
|
-
|
15
|
-
?> u = ColorEnumerator.new
|
16
|
-
=> #<ColorEnumerator...>
|
5
|
+
?> u = StringEnumerator.new(:color => [ 'red', 'blue' ])
|
6
|
+
=> #<StringEnumerator...>
|
17
7
|
?> u.enumerate 'http://example.com/[color]'
|
18
8
|
=> [ 'http://example.com/blue', 'http://example.com/red' ]
|
19
9
|
|
@@ -23,21 +13,13 @@ The start and end of placeholders are marked with "[" and "]" (although you can
|
|
23
13
|
|
24
14
|
You can define as many replacements as you want, which will exponentially increase the final number of enumerations
|
25
15
|
|
26
|
-
|
27
|
-
|
28
|
-
def replacements
|
29
|
-
{
|
30
|
-
:color => [ 'red', 'blue' ],
|
31
|
-
:taste => [ 'savory', 'sweet' ]
|
32
|
-
}
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
Then you'll get
|
37
|
-
|
38
|
-
?> u2 = ColorAndTasteEnumerator.new
|
39
|
-
=> #<ColorAndTasteEnumerator...>
|
16
|
+
?> u2 = StringEnumerator.new(:color => [ 'red', 'blue' ], :taste => [ 'savory', 'sweet' ])
|
17
|
+
=> #<StringEnumerator...>
|
40
18
|
?> u2.enumerate 'http://example.com/[color]/[taste]'
|
41
19
|
=> [ 'http://example.com/blue/savory', 'http://example.com/blue/sweet', 'http://example.com/red/savory', 'http://example.com/red/sweet' ]
|
42
20
|
|
21
|
+
==Thanks to
|
22
|
+
|
23
|
+
* https://github.com/trans for https://github.com/seamusabshere/string_enumerator/issues/1
|
24
|
+
|
43
25
|
Copyright 2011 Seamus Abshere
|
data/lib/string_enumerator.rb
CHANGED
@@ -1,53 +1,43 @@
|
|
1
1
|
require "string_enumerator/version"
|
2
2
|
|
3
3
|
class StringEnumerator
|
4
|
+
def initialize(replacements)
|
5
|
+
@replacements = replacements.inject({}) do |memo, (k, v)|
|
6
|
+
memo[k.to_s] = v
|
7
|
+
memo
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :replacements
|
12
|
+
|
13
|
+
attr_writer :start_mark
|
14
|
+
def start_mark
|
15
|
+
@start_mark || '['
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_writer :end_mark
|
19
|
+
def end_mark
|
20
|
+
@end_mark || ']'
|
21
|
+
end
|
22
|
+
|
4
23
|
# Get all the possible enumerations of <tt>str</tt> given the <tt>replacements</tt> you defined
|
5
24
|
def enumerate(str)
|
6
25
|
enumerations = [str] # seed it
|
7
26
|
str.scan(pattern) do |placeholder|
|
8
27
|
placeholder = placeholder[0]
|
9
|
-
if r =
|
28
|
+
if r = replacements[placeholder]
|
29
|
+
# "recursion"
|
10
30
|
enumerations = enumerations.map do |e|
|
11
|
-
r.map { |replacement| e.gsub(%{
|
31
|
+
r.map { |replacement| e.gsub(%{#{start_mark}#{placeholder}#{end_mark}}, replacement) }
|
12
32
|
end.flatten
|
13
33
|
end
|
14
34
|
end
|
15
35
|
enumerations
|
16
36
|
end
|
17
37
|
|
18
|
-
# Your class should override this
|
19
|
-
#
|
20
|
-
# For example:
|
21
|
-
# class ColorAndTasteEnumerator < StringEnumerator
|
22
|
-
# def replacements
|
23
|
-
# {
|
24
|
-
# :color => [ 'red', 'blue' ],
|
25
|
-
# :taste => [ 'savory', 'sweet' ]
|
26
|
-
# }
|
27
|
-
# end
|
28
|
-
# end
|
29
|
-
def replacements
|
30
|
-
raise "[StringEnumerator] Override this method with your replacements"
|
31
|
-
end
|
32
|
-
|
33
38
|
private
|
34
|
-
|
35
|
-
def stringified_replacements
|
36
|
-
@stringified_replacements ||= replacements.inject({}) do |memo, (k, v)|
|
37
|
-
memo[k.to_s] = v
|
38
|
-
memo
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
39
|
+
|
42
40
|
def pattern
|
43
41
|
@pattern ||= /#{::Regexp.escape(start_mark)}([^#{::Regexp.escape(end_mark)}]+)#{::Regexp.escape(end_mark)}/
|
44
42
|
end
|
45
|
-
|
46
|
-
def start_mark
|
47
|
-
'['
|
48
|
-
end
|
49
|
-
|
50
|
-
def end_mark
|
51
|
-
']'
|
52
|
-
end
|
53
43
|
end
|
@@ -2,21 +2,14 @@ require 'helper'
|
|
2
2
|
require 'brighter_planet_metadata'
|
3
3
|
require 'active_support/core_ext'
|
4
4
|
|
5
|
-
class
|
6
|
-
|
7
|
-
|
8
|
-
{
|
5
|
+
class TestStringEnumerator < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@f = StringEnumerator.new({
|
9
8
|
:certified_emitter_underscore_plural => BrighterPlanet.metadata.certified_emitters.map { |emitter| emitter.underscore.pluralize },
|
10
9
|
:emission_estimate_service_color => %w{red blue},
|
11
10
|
:emitter_underscore_plural => BrighterPlanet.metadata.emitters.map { |emitter| emitter.underscore.pluralize },
|
12
11
|
:resource_underscore => BrighterPlanet.metadata.resources.map { |resource| resource.underscore }
|
13
|
-
}
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class TestStringEnumerator < Test::Unit::TestCase
|
18
|
-
def setup
|
19
|
-
@f = UrlFiller.new
|
12
|
+
})
|
20
13
|
end
|
21
14
|
|
22
15
|
def test_001_enumerates_emitters
|
@@ -50,4 +43,10 @@ class TestStringEnumerator < Test::Unit::TestCase
|
|
50
43
|
enumerated = @f.enumerate("http://carbon.brighterplanet.com/[]/options.html")
|
51
44
|
assert_equal ["http://carbon.brighterplanet.com/[]/options.html"], enumerated
|
52
45
|
end
|
46
|
+
|
47
|
+
def test_005_different_start_mark
|
48
|
+
@f.start_mark = 'BOOYAH'
|
49
|
+
enumerated = @f.enumerate("http://carbon.brighterplanet.com/BOOYAHemitter_underscore_plural]/options.html")
|
50
|
+
assert enumerated.include?("http://carbon.brighterplanet.com/flights/options.html")
|
51
|
+
end
|
53
52
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: string_enumerator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-06-27 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &2161206980 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2161206980
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: brighter_planet_metadata
|
27
|
-
requirement: &
|
27
|
+
requirement: &2161206560 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2161206560
|
36
36
|
description: Fill strings that have placeholders like [color], possibly returning
|
37
37
|
multiple results.
|
38
38
|
email:
|