Class: Turnip::Placeholder

Inherits:
Object
  • Object
show all
Defined in:
lib/turnip/placeholder.rb

Defined Under Namespace

Classes: Match

Class Method Summary collapse

Instance Method Summary collapse

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

#regexpObject



52
53
54
# File 'lib/turnip/placeholder.rb', line 52

def regexp
  Regexp.new(@matches.map(&:regexp).join('|'))
end