Class: Teapot::Substitutions::SymbolicSubstitution

Inherits:
Object
  • Object
show all
Defined in:
lib/teapot/substitutions.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword, value) ⇒ SymbolicSubstitution

Returns a new instance of SymbolicSubstitution.



103
104
105
106
# File 'lib/teapot/substitutions.rb', line 103

def initialize(keyword, value)
  @keyword = keyword
  @value = value
end

Instance Attribute Details

#keywordObject (readonly)

Returns the value of attribute keyword.



108
109
110
# File 'lib/teapot/substitutions.rb', line 108

def keyword
  @keyword
end

#valueObject (readonly)

Returns the value of attribute value.



109
110
111
# File 'lib/teapot/substitutions.rb', line 109

def value
  @value
end

Class Method Details

.apply(text, group) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/teapot/substitutions.rb', line 115

def self.apply(text, group)
  substitutions = Hash[group.collect{|substitution| [substitution.keyword, substitution.value]}]

  pattern = Regexp.new(substitutions.keys.map{|key| Regexp.escape(key)}.join('|'))

  text.gsub(pattern) {|key| substitutions[key]}
end

Instance Method Details

#apply(text) ⇒ Object



111
112
113
# File 'lib/teapot/substitutions.rb', line 111

def apply(text)
  text.gsub(@keyword, @value)
end