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.



121
122
123
124
# File 'lib/teapot/substitutions.rb', line 121

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

Instance Attribute Details

#keywordObject (readonly)

Returns the value of attribute keyword.



126
127
128
# File 'lib/teapot/substitutions.rb', line 126

def keyword
  @keyword
end

#valueObject (readonly)

Returns the value of attribute value.



127
128
129
# File 'lib/teapot/substitutions.rb', line 127

def value
  @value
end

Class Method Details

.apply(text, group) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/teapot/substitutions.rb', line 140

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



136
137
138
# File 'lib/teapot/substitutions.rb', line 136

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

#freezeObject



129
130
131
132
133
134
# File 'lib/teapot/substitutions.rb', line 129

def freeze
	@keyword.freeze
	@value.freeze
	
	super
end