Class: KiCad::AST::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/kicad/ast.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, children) ⇒ Node



6
7
8
9
# File 'lib/kicad/ast.rb', line 6

def initialize values, children
  @values = values
  @children = children
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



4
5
6
# File 'lib/kicad/ast.rb', line 4

def children
  @children
end

#valuesObject (readonly)

Returns the value of attribute values.



4
5
6
# File 'lib/kicad/ast.rb', line 4

def values
  @values
end

Class Method Details

.child_types(*cts) ⇒ Object

Define methods for each child type this class allows



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/kicad/ast.rb', line 60

def self.child_types *cts
  # puts "#{self.name} allows child types #{cts.inspect}"
  cts.each do |c|
    if Array === c
      define_method(:"all_#{c[0].to_s}") do
        # puts "Looking for all [#{class_names*', '}] in #{@children.map{|q| q.class.name}.inspect}"
        children_of_type(c)
      end
      # REVISIT: Allow deleting and adding instances to the array
      # new_child = KiCad.parse('(some new node)').value
      # @children.append(new_child)
    else
      class_name = 'KiCad::AST::'+to_class_name(c)
      define_method(:"#{c}") do
        # puts "Looking for first #{class_name} in #{@children.map{|q| q.class.name}.inspect}"
        a = children_of_type(c)
        puts "Choosing first #{self.class.name}.#{c} of #{a.size}" if a.size > 1
        a.first
      end
      # Allow deleting this instance
      define_method(:"unset_#{c.to_s}") do
        child = send(:"#{c}")
        @children = @children - [child] if child
        child ? true : nil
      end
    end
  end
end

.to_class_name(sym) ⇒ Object



89
90
91
# File 'lib/kicad/ast.rb', line 89

def self.to_class_name sym
  sym.to_s.gsub(/\A[a-z]|_[a-z]/) {|from| from[-1].upcase }
end

.to_symbol(class_name) ⇒ Object



93
94
95
# File 'lib/kicad/ast.rb', line 93

def self.to_symbol class_name
  class_name.to_s.gsub(/[A-Z]/) {|from| '_'+from[-1].downcase }.sub(/\A_/,'')
end

.value_types(vts) ⇒ Object

Define setter and getter methods for each value type this class allows



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kicad/ast.rb', line 33

def self.value_types vts
  i = 1   # @values[0] is always the class symbol
  vts.each do |k, v|
    # puts "#{self.name} attribute #{k.to_s} => #{v.inspect}"
    # puts "attr_accessor #{self.name}.#{k} (values #{v.inspect}) is stored in @values[#{i}]"
    begin
      o = i # Avoid capturing i after the loop ends
      define_method(:"#{k}") do
        # puts "accessing #{self.class.name}.#{k} as @values[#{o}]"
        @values[o]
      end
      define_method(:"#{k}=") do |v|
        # puts "setting #{self.class.name}.#{k} as @values[#{o}]"
        # REVISIT: Check valid data type matching v
        @values[o] = v
      end
    end
    i = i+1
  end
end

Instance Method Details

#children_of_type(*cts) ⇒ Object

cts is an array of AST class symbols or strings



54
55
56
57
# File 'lib/kicad/ast.rb', line 54

def children_of_type *cts  # cts is an array of AST class symbols or strings
  class_names = cts.flatten.map{|k| 'KiCad::AST::'+self.class.to_class_name(k)}
  @children.filter{|h| class_names.include?(h.class.name) }
end

#emit(depth = 0) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/kicad/ast.rb', line 11

def emit depth = 0
  "\t"*depth +
  '(' +
  @values.map{|v| String === v ? v.inspect : v.to_s }*' ' +
  (@children.size == 0 ? '' : "\n" + @children.map{|c| c.emit(depth+1) }*''+"\t"*depth) +
  ")\n"
end

#emit_compactObject



24
25
26
27
28
29
30
# File 'lib/kicad/ast.rb', line 24

def emit_compact
  '(' +
  [ @values.map{|v| String === v ? v.inspect : v.to_s },
    @children.map{|c| c.emit_compact() }
  ].flatten*' ' +
  ')'
end

#emit_compacting(depth = 0) ⇒ Object

A replacement for a Node#emit that wants descendents compacted



20
21
22
# File 'lib/kicad/ast.rb', line 20

def emit_compacting depth = 0
  "\t"*depth + emit_compact + "\n"
end