Class: Turnip::Placeholder
- Inherits:
-
Object
- Object
- Turnip::Placeholder
- Defined in:
- lib/turnip/placeholder.rb
Defined Under Namespace
Classes: Match
Class Method Summary collapse
- .add(name, &block) ⇒ Object
- .apply(name, value) ⇒ Object
- .find(name) ⇒ Object
- .resolve(name) ⇒ Object
Instance Method Summary collapse
- #apply(value) ⇒ Object
-
#initialize(name, &block) ⇒ Placeholder
constructor
A new instance of Placeholder.
- #match(regexp, &block) ⇒ Object
- #regexp ⇒ Object
Constructor Details
#initialize(name, &block) ⇒ Placeholder
Returns a new instance of Placeholder.
37 38 39 40 41 |
# File 'lib/turnip/placeholder.rb', line 37 def initialize(name, &block) @name = name @matches = [] instance_eval(&block) end |
Class Method Details
.add(name, &block) ⇒ Object
6 7 8 |
# File 'lib/turnip/placeholder.rb', line 6 def add(name, &block) placeholders[name] = Placeholder.new(name, &block) end |
.apply(name, value) ⇒ Object
14 15 16 |
# File 'lib/turnip/placeholder.rb', line 14 def apply(name, value) find(name).apply(value) end |
.find(name) ⇒ Object
18 19 20 |
# File 'lib/turnip/placeholder.rb', line 18 def find(name) placeholders[name] or default end |
.resolve(name) ⇒ Object
10 11 12 |
# File 'lib/turnip/placeholder.rb', line 10 def resolve(name) find(name).regexp end |
Instance Method Details
#apply(value) ⇒ Object
43 44 45 46 |
# File 'lib/turnip/placeholder.rb', line 43 def apply(value) match, params = find_match(value) if match and match.block then match.block.call(*params) else value end end |
#match(regexp, &block) ⇒ Object
48 49 50 |
# File 'lib/turnip/placeholder.rb', line 48 def match(regexp, &block) @matches << Match.new(regexp, block) end |
#regexp ⇒ Object
52 53 54 |
# File 'lib/turnip/placeholder.rb', line 52 def regexp Regexp.new(@matches.map(&:regexp).join('|')) end |