string_enumerator 0.0.1
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 +5 -0
- data/Gemfile +4 -0
- data/README.rdoc +43 -0
- data/Rakefile +19 -0
- data/lib/string_enumerator/version.rb +3 -0
- data/lib/string_enumerator.rb +53 -0
- data/string_enumerator.gemspec +22 -0
- data/test/helper.rb +9 -0
- data/test/test_string_enumerator.rb +53 -0
- metadata +81 -0
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
=string_enumerator
|
2
|
+
|
3
|
+
You provide a list of replacements ("replace [color] with red and blue"):
|
4
|
+
|
5
|
+
require 'string_enumerator'
|
6
|
+
class ColorEnumerator < StringEnumerator
|
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...>
|
17
|
+
?> u.enumerate 'http://example.com/[color]'
|
18
|
+
=> [ 'http://example.com/blue', 'http://example.com/red' ]
|
19
|
+
|
20
|
+
The start and end of placeholders are marked with "[" and "]" (although you can override this if you want, see the tests.)
|
21
|
+
|
22
|
+
==Multiple placeholders per string
|
23
|
+
|
24
|
+
You can define as many replacements as you want, which will exponentially increase the final number of enumerations
|
25
|
+
|
26
|
+
require 'string_enumerator'
|
27
|
+
class ColorAndTasteEnumerator < StringEnumerator
|
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...>
|
40
|
+
?> u2.enumerate 'http://example.com/[color]/[taste]'
|
41
|
+
=> [ 'http://example.com/blue/savory', 'http://example.com/blue/sweet', 'http://example.com/red/savory', 'http://example.com/red/sweet' ]
|
42
|
+
|
43
|
+
Copyright 2011 Seamus Abshere
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
require 'rake/testtask'
|
4
|
+
Rake::TestTask.new(:test) do |test|
|
5
|
+
test.libs << 'lib' << 'test'
|
6
|
+
test.pattern = 'test/**/test_*.rb'
|
7
|
+
test.verbose = true
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'rake/rdoctask'
|
11
|
+
Rake::RDocTask.new do |rdoc|
|
12
|
+
rdoc.rdoc_dir = 'rdoc'
|
13
|
+
rdoc.title = 'string_enumerator'
|
14
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
15
|
+
rdoc.rdoc_files.include('README*')
|
16
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
17
|
+
end
|
18
|
+
|
19
|
+
task :default => :test
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "string_enumerator/version"
|
2
|
+
|
3
|
+
class StringEnumerator
|
4
|
+
# Get all the possible enumerations of <tt>str</tt> given the <tt>replacements</tt> you defined
|
5
|
+
def enumerate(str)
|
6
|
+
enumerations = [str] # seed it
|
7
|
+
str.scan(pattern) do |placeholder|
|
8
|
+
placeholder = placeholder[0]
|
9
|
+
if r = stringified_replacements[placeholder]
|
10
|
+
enumerations = enumerations.map do |e|
|
11
|
+
r.map { |replacement| e.gsub(%{[#{placeholder}]}, replacement) }
|
12
|
+
end.flatten
|
13
|
+
end
|
14
|
+
end
|
15
|
+
enumerations
|
16
|
+
end
|
17
|
+
|
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
|
+
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
|
+
|
42
|
+
def pattern
|
43
|
+
@pattern ||= /#{::Regexp.escape(start_mark)}([^#{::Regexp.escape(end_mark)}]+)#{::Regexp.escape(end_mark)}/
|
44
|
+
end
|
45
|
+
|
46
|
+
def start_mark
|
47
|
+
'['
|
48
|
+
end
|
49
|
+
|
50
|
+
def end_mark
|
51
|
+
']'
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "string_enumerator/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "string_enumerator"
|
7
|
+
s.version = StringEnumerator::VERSION
|
8
|
+
s.authors = ["Seamus Abshere"]
|
9
|
+
s.email = ["seamus@abshere.net"]
|
10
|
+
s.homepage = "https://github.com/seamusabshere/string_enumerator"
|
11
|
+
s.summary = %q{Given a string containing placeholders (like [color]), enumerate all of the possible strings resulting from filling those placeholders with replacements (like red, blue).}
|
12
|
+
s.description = %q{Fill strings that have placeholders like [color], possibly returning multiple results.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "string_enumerator"
|
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_development_dependency 'activesupport'
|
21
|
+
s.add_development_dependency 'brighter_planet_metadata'
|
22
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'brighter_planet_metadata'
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
|
5
|
+
class UrlFiller < StringEnumerator
|
6
|
+
# this has to return a Hash of placeholder => replacement pairs
|
7
|
+
def replacements
|
8
|
+
{
|
9
|
+
:certified_emitter_underscore_plural => BrighterPlanet.metadata.certified_emitters.map { |emitter| emitter.underscore.pluralize },
|
10
|
+
:emission_estimate_service_color => %w{red blue},
|
11
|
+
:emitter_underscore_plural => BrighterPlanet.metadata.emitters.map { |emitter| emitter.underscore.pluralize },
|
12
|
+
: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
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_001_enumerates_emitters
|
23
|
+
automatically_enumerated = @f.enumerate("http://carbon.brighterplanet.com/[emitter_underscore_plural]/options.html")
|
24
|
+
manually_enumerated = BrighterPlanet.metadata.emitters.map do |emitter|
|
25
|
+
"http://carbon.brighterplanet.com/#{emitter.underscore.pluralize}/options.html"
|
26
|
+
end
|
27
|
+
assert_equal automatically_enumerated.sort, manually_enumerated.sort
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_002_enumerates_emitters_and_colors
|
31
|
+
automatically_enumerated = @f.enumerate("http://cm1-production-[emission_estimate_service_color].carbon.brighterplanet.com/[emitter_underscore_plural]/options.html")
|
32
|
+
manually_enumerated = BrighterPlanet.metadata.emitters.map do |emitter|
|
33
|
+
%w{red blue}.map do |color|
|
34
|
+
"http://cm1-production-#{color}.carbon.brighterplanet.com/#{emitter.underscore.pluralize}/options.html"
|
35
|
+
end
|
36
|
+
end.flatten
|
37
|
+
assert_equal automatically_enumerated.sort, manually_enumerated.sort
|
38
|
+
end
|
39
|
+
|
40
|
+
# sanity check, we'd never do this
|
41
|
+
def test_003_enumerates_emitters_and_colors_and_resources
|
42
|
+
enumerated = @f.enumerate("http://cm1-production-[emission_estimate_service_color].carbon.brighterplanet.com/[emitter_underscore_plural]/options.html?foobar=[resource_underscore]")
|
43
|
+
assert enumerated.include?("http://cm1-production-red.carbon.brighterplanet.com/flights/options.html?foobar=flight_segment")
|
44
|
+
assert enumerated.include?("http://cm1-production-blue.carbon.brighterplanet.com/flights/options.html?foobar=flight_segment")
|
45
|
+
assert enumerated.include?("http://cm1-production-red.carbon.brighterplanet.com/automobile_trips/options.html?foobar=flight_segment")
|
46
|
+
assert enumerated.include?("http://cm1-production-blue.carbon.brighterplanet.com/automobile_trips/options.html?foobar=flight_segment")
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_004_edge_case_empty_placeholder
|
50
|
+
enumerated = @f.enumerate("http://carbon.brighterplanet.com/[]/options.html")
|
51
|
+
assert_equal ["http://carbon.brighterplanet.com/[]/options.html"], enumerated
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: string_enumerator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Seamus Abshere
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-06-27 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &2155467220 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2155467220
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: brighter_planet_metadata
|
27
|
+
requirement: &2155466800 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2155466800
|
36
|
+
description: Fill strings that have placeholders like [color], possibly returning
|
37
|
+
multiple results.
|
38
|
+
email:
|
39
|
+
- seamus@abshere.net
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- README.rdoc
|
47
|
+
- Rakefile
|
48
|
+
- lib/string_enumerator.rb
|
49
|
+
- lib/string_enumerator/version.rb
|
50
|
+
- string_enumerator.gemspec
|
51
|
+
- test/helper.rb
|
52
|
+
- test/test_string_enumerator.rb
|
53
|
+
homepage: https://github.com/seamusabshere/string_enumerator
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project: string_enumerator
|
73
|
+
rubygems_version: 1.8.5
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Given a string containing placeholders (like [color]), enumerate all of the
|
77
|
+
possible strings resulting from filling those placeholders with replacements (like
|
78
|
+
red, blue).
|
79
|
+
test_files:
|
80
|
+
- test/helper.rb
|
81
|
+
- test/test_string_enumerator.rb
|