Method: SlideField::Interpreter#interpret_tree

Defined in:
lib/slidefield/interpreter.rb

#interpret_tree(tree, object, child_path = nil, child_context = nil, close_object = true) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/slidefield/interpreter.rb', line 76

def interpret_tree(tree, object, child_path = nil, child_context = nil, close_object = true)
  tree.respond_to? :each and tree.each {|stmt|
    if stmt_data = stmt[:assignment]
      interpret_assignment stmt_data, object
    elsif stmt_data = stmt[:object]
      interpret_object stmt_data, object, child_path, child_context
    else
      # we got strange data from the parser?!
      raise "Unsupported statement '#{stmt.keys.first}'"
    end
  }

  if close_object
    # finalize the object once all its content has been processed

    rules = object.rules
    rules.required_properties.each {|name|
      unless object.get name
        raise SlideField::InterpreterError,
          "Missing property '#{name}' for object '#{object.type}' at #{object.loc}"
      end
    }

    rules.optional_properties.each {|name|
      next unless object.get(name).nil?

      default = rules.default_value name
      type = rules.type_of_property name

      object.set name, default, 'default', type
    }

    rules.accepted_children.each {|type|
      min, max = rules.requirements_of_child type
      count = object[type].count

      if count < min
        raise SlideField::InterpreterError,
          "Object '#{object.type}' must have at least #{min} '#{type}', #{count} found at #{object.loc}"
      end

      if max > 0 && count > max
        raise SlideField::InterpreterError,
          "Object '#{object.type}' can not have more than #{max} '#{type}', #{count} found at #{object.loc}"
      end
    }
  end

  object
end