Class: TireSwing::Node

Inherits:
Object show all
Defined in:
lib/tire_swing/node.rb

Overview

TireSwing nodes are auto-generated by node definitions (see TireSwing::NodeDefinition). A node is meant to be a part of an AST external to the AST that Treetop generates at parse-time with Treetop::Runtime::SyntaxNodes.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ Node

Instantiate a node.

Values can either be a hash of values to set the values of this node’s attributes, or a Treetop syntax node which is used to automatically build this node.



39
40
41
42
43
44
45
46
47
# File 'lib/tire_swing/node.rb', line 39

def initialize(values={})
  if values.kind_of?(Treetop::Runtime::SyntaxNode)
    build_from_parsed_node(values)
  else
    values.each do |name, value|
      send("#{name}=", value)
    end
  end
end

Class Method Details

.attribute_mappingObject

The mapping of attribute names to the auto-build values/methods/lambdas used by create_node



30
31
32
# File 'lib/tire_swing/node.rb', line 30

def self.attribute_mapping
  @attribute_mapping ||= {}
end

.create(*attribs) ⇒ Object

Create a subclass of Node with the given attributes, both simple and mapped.

(see NodeDefinitions#node for more information)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tire_swing/node.rb', line 13

def self.create(*attribs)
  Class.new(self) do
    attribs.each do |attrib|
      case attrib
      when Symbol, String
        attribute(attrib.to_s) { raise "no value given for #{attrib}" }
      when Hash
        attrib.each do |name, symbol|
          attribute(name.to_s) { raise "no value given for #{name}" }
          attribute_mapping[name.to_s] = symbol
        end
      end
    end
  end
end