class Turnip::Placeholder
Public Class Methods
add(name, &block)
click to toggle source
# File lib/turnip/placeholder.rb, line 6 def add(name, &block) if placeholders.key?(name) location = caller_locations.detect { |l| l.to_s !~ /lib\/turnip\/dsl\.rb/ } warn "Placeholder :#{name} was replaced at #{location}." end placeholders[name] = Placeholder.new(name, &block) end
apply(name, value)
click to toggle source
# File lib/turnip/placeholder.rb, line 19 def apply(name, value) find(name).apply(value) end
find(name)
click to toggle source
# File lib/turnip/placeholder.rb, line 23 def find(name) placeholders[name] or default end
new(name, &block)
click to toggle source
# File lib/turnip/placeholder.rb, line 42 def initialize(name, &block) @name = name @matches = [] @default = nil instance_eval(&block) end
resolve(name)
click to toggle source
# File lib/turnip/placeholder.rb, line 15 def resolve(name) find(name).regexp end
Private Class Methods
default()
click to toggle source
# File lib/turnip/placeholder.rb, line 33 def default @default ||= new(:default) do default do |value| value end end end
placeholders()
click to toggle source
# File lib/turnip/placeholder.rb, line 29 def placeholders @placeholders ||= {} end
Public Instance Methods
apply(value)
click to toggle source
# File lib/turnip/placeholder.rb, line 49 def apply(value) match, params = find_match(value) if match and match.block then match.block.call(*params) else value end end
default(&block)
click to toggle source
# File lib/turnip/placeholder.rb, line 58 def default(&block) @default ||= Match.new( %r{['"]?((?:(?<=")[^"]*)(?=")|(?:(?<=')[^']*(?='))|(?<!['"])[[:alnum:]_-]+(?!['"]))['"]?}, block ) end
match(regexp, &block)
click to toggle source
# File lib/turnip/placeholder.rb, line 54 def match(regexp, &block) @matches << Match.new(regexp, block) end
regexp()
click to toggle source
# File lib/turnip/placeholder.rb, line 65 def regexp Regexp.new(placeholder_matches.map(&:regexp).join('|')) end
Private Instance Methods
find_match(value)
click to toggle source
# File lib/turnip/placeholder.rb, line 71 def find_match(value) @matches.each do |m| result = value.scan(m.regexp) return m, result.flatten unless result.empty? end # # value is one of the following: # # %{Jhon Doe} # %{"Jhon Doe"} # %{'Jhon Doe'} # # In any case, it passed to the step block in the state without quotes return @default, value.sub(/^(["'])([^\1]*)\1$/, '\2') end
placeholder_matches()
click to toggle source
# File lib/turnip/placeholder.rb, line 88 def placeholder_matches matches = @matches matches += [@default] if @default matches end