string_enumerator
You provide a list of replacements (“replace [color] with red and blue”):
require 'string_enumerator'
class ColorEnumerator < StringEnumerator
# Interface to StringEnumerator
def replacements
{ :color => [ 'red', 'blue' ] }
end
end
Then you call the #enumerate
method on a String
:
?> u = ColorEnumerator.new
=> #<ColorEnumerator...>
?> u.enumerate 'http://example.com/[color]'
=> [ 'http://example.com/blue', 'http://example.com/red' ]
The start and end of placeholders are marked with “[” and “]” (although you can override this if you want, see the tests.)
Multiple placeholders per string
You can define as many replacements as you want, which will exponentially increase the final number of enumerations
require 'string_enumerator'
class ColorAndTasteEnumerator < StringEnumerator
def replacements
{
:color => [ 'red', 'blue' ],
:taste => [ 'savory', 'sweet' ]
}
end
end
Then you’ll get
?> u2 = ColorAndTasteEnumerator.new
=> #<ColorAndTasteEnumerator...>
?> u2.enumerate 'http://example.com/[color]/[taste]'
=> [ 'http://example.com/blue/savory', 'http://example.com/blue/sweet', 'http://example.com/red/savory', 'http://example.com/red/sweet' ]
Copyright 2011 Seamus Abshere