Class: StringEnumerator

Inherits:
Object
  • Object
show all
Defined in:
lib/string_enumerator.rb,
lib/string_enumerator/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#enumerate(str) ⇒ Object

Get all the possible enumerations of str given the replacements you defined



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/string_enumerator.rb', line 5

def enumerate(str)
  enumerations = [str] # seed it
  str.scan(pattern) do |placeholder|
    placeholder = placeholder[0]
    if r = stringified_replacements[placeholder]
      enumerations = enumerations.map do |e|
        r.map { |replacement| e.gsub(%{[#{placeholder}]}, replacement) }
      end.flatten
    end
  end
  enumerations
end

#replacementsObject

Your class should override this

For example:

class ColorAndTasteEnumerator < StringEnumerator
  def replacements
    {
      :color => [ 'red', 'blue' ],
      :taste => [ 'savory', 'sweet' ]
    }
  end
end


29
30
31
# File 'lib/string_enumerator.rb', line 29

def replacements
  raise "[StringEnumerator] Override this method with your replacements"
end