Class: Teapot::Substitutions::SymbolicSubstitution
- Inherits:
-
Object
- Object
- Teapot::Substitutions::SymbolicSubstitution
- Defined in:
- lib/teapot/substitutions.rb
Instance Attribute Summary collapse
-
#keyword ⇒ Object
readonly
Returns the value of attribute keyword.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #apply(text) ⇒ Object
-
#initialize(keyword, value) ⇒ SymbolicSubstitution
constructor
A new instance of SymbolicSubstitution.
Constructor Details
#initialize(keyword, value) ⇒ SymbolicSubstitution
Returns a new instance of SymbolicSubstitution.
99 100 101 102 |
# File 'lib/teapot/substitutions.rb', line 99 def initialize(keyword, value) @keyword = keyword @value = value end |
Instance Attribute Details
#keyword ⇒ Object (readonly)
Returns the value of attribute keyword.
104 105 106 |
# File 'lib/teapot/substitutions.rb', line 104 def keyword @keyword end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
105 106 107 |
# File 'lib/teapot/substitutions.rb', line 105 def value @value end |
Class Method Details
.apply(text, group) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/teapot/substitutions.rb', line 111 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
107 108 109 |
# File 'lib/teapot/substitutions.rb', line 107 def apply(text) text.gsub(@keyword, @value) end |