module Iowa
# Parse through binding declarations to resolve bindings.
# Resolved bindings are stored within an object variable,
# @bindings.
class BindingsParser
MainPattern = /([\w.]+)\s*(:\s*(\w+)\s*)?[{](.*?)^\s*[}]/m
BodyPattern = /(\w+)\s*=\s*(.+)/
TrimPattern = /\s*$/
# Apply the MainPattern to the binding data, passing matched
# information to processMatch().
def initialize(data)
@bindings = {}
while data.sub!(MainPattern, "")
processMatch($1, $3, $4)
end
end
# Create the defined binding.
def processMatch(id, klass, data)
bindingHash = {}
if klass
bindingHash["class"] = klass
end
while data.sub!(BodyPattern, "")
key, value = $1, $2
value.sub!(TrimPattern,"")
# Just to make sure it is clear, if the binding value either
# starts with a digit, a quote character, or a colon, then
# it is assumed to be literal binding. The value will be
# ran through eval, and whatever is returned will be used
# as the value of the binding.
if value =~ /^[\d"':]/
bindingHash[key] = LiteralAssociation.new(eval(value))
else
bindingHash[key] = PathAssociation.new(value)
end
end
@bindings[id] = bindingHash
end
# Return the Hash of bindings.
def bindings
@bindings
end
end
end