class Freud::Variables
Constants
- ESCAPED_SIGILS
- UNDEFINED
- VARIABLES
Public Class Methods
new(*args)
click to toggle source
Calls superclass method
# File lib/freud/variables.rb, line 13 def initialize(*args) super @stack = {} end
Public Instance Methods
apply(input)
click to toggle source
# File lib/freud/variables.rb, line 36 def apply(input) return(nil) if input.nil? interpolated = input.gsub(VARIABLES) do key = $~[2] || $~[3] || $~[4] default = $~[5] || UNDEFINED push_stack(key, input) output = apply(fetch(key, default).to_s) pop_stack(key) output end interpolated.gsub(ESCAPED_SIGILS, "%") end
each_pair(&block)
click to toggle source
# File lib/freud/variables.rb, line 18 def each_pair(&block) @state.each_pair(&block) end
fetch(key, default = UNDEFINED)
click to toggle source
# File lib/freud/variables.rb, line 30 def fetch(key, default = UNDEFINED) key = key.to_sym return(@state.fetch(key, default)) unless (default == UNDEFINED) @state.fetch(key) { raise(KeyError, "Unknown variable: #{key}") } end
merge(updates)
click to toggle source
# File lib/freud/variables.rb, line 26 def merge(updates) chain(updates) end
pop_stack(key)
click to toggle source
# File lib/freud/variables.rb, line 59 def pop_stack(key) @stack.delete(key) self end
push_stack(key, input)
click to toggle source
# File lib/freud/variables.rb, line 49 def push_stack(key, input) if @stack[key] message = "Infinite loop evaluating '%#{key}' in '#{input}'" raise(RuntimeError, message) else @stack[key] = true self end end
test(input)
click to toggle source
# File lib/freud/variables.rb, line 22 def test(input) (input =~ VARIABLES) ? true : false end