Class: Q_Node

Inherits:
Object
  • Object
show all
Defined in:
lib/q-language/node.rb

Overview

Copyright © 2010-2011 Jesse Sielaff

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_type, name, klass, string, nodes) ⇒ Q_Node

Returns a new instance of Q_Node.


7
8
9
10
11
12
13
14
15
# File 'lib/q-language/node.rb', line 7

def initialize (node_type, name, klass, string, nodes)
  @node_type = node_type
  
  @value = case
    when name then name
    when klass then Object.const_get(:"Q#{klass}").from_s(string)
    when nodes then nodes.each {|node| node.instance_variable_set(:@block, self) }
  end
end

Instance Attribute Details

#node_typeObject (readonly)

Returns the value of attribute node_type.


17
18
19
# File 'lib/q-language/node.rb', line 17

def node_type
  @node_type
end

#valueObject (readonly)

Returns the value of attribute value.


17
18
19
# File 'lib/q-language/node.rb', line 17

def value
  @value
end

Instance Method Details

#blockObject

• User method Returns the block that contains the Node.


22
23
24
# File 'lib/q-language/node.rb', line 22

def block
  @block
end

#nodesObject

• User method Returns an Array of the block Node’s contents. If the Node is not a block, returns nil.


30
31
32
# File 'lib/q-language/node.rb', line 30

def nodes
  @value if @node_type == :block
end

#to_scriptObject

• User method Returns a String representing the Node in Q script format.


37
38
39
40
41
42
43
44
# File 'lib/q-language/node.rb', line 37

def to_script
  case @node_type
    when :block then "{#{@value.map {|n| n.to_script }.join ?\ }}"
    when :literal then "#{@value.to_qliteral}"
    when :method then "#@value"
    when :variable then ":#@value"
  end
end