Class: Fabulator::Expr::Node

Inherits:
Object
  • Object
show all
Includes:
NodeLogic
Defined in:
lib/fabulator/expr/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NodeLogic

#copy, #empty?, #in_context, #path, #root, #to, #to_h, #to_s

Constructor Details

#initialize(a, r, v, c, p = nil) ⇒ Node

,f={})



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fabulator/expr/node.rb', line 8

def initialize(a,r,v,c,p = nil) #,f={})
  @roots = r
  @axis = a
  @children = []
  @children = @children + c if c.is_a?(Array)
  @value = v
  @vtype = nil
  @parent = p
  @name = nil
#        @e_ctx = f
  @attributes = [ ]
  @is_attribute = false

  if @value.is_a?(String)
    @vtype = [ FAB_NS, 'string' ]
  elsif @value.is_a?(Numeric)
    @vtype = [ FAB_NS, 'numeric' ]
  elsif @value.is_a?(TrueClass) || @value.is_a?(FalseClass)
    @vtype = [ FAB_NS, 'boolean' ]
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/fabulator/expr/node.rb', line 6

def attributes
  @attributes
end

#is_attributeObject

Returns the value of attribute is_attribute.



6
7
8
# File 'lib/fabulator/expr/node.rb', line 6

def is_attribute
  @is_attribute
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/fabulator/expr/node.rb', line 6

def name
  @name
end

#rootsObject

Returns the value of attribute roots.



6
7
8
# File 'lib/fabulator/expr/node.rb', line 6

def roots
  @roots
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/fabulator/expr/node.rb', line 6

def value
  @value
end

#vtypeObject

Returns the value of attribute vtype.



6
7
8
# File 'lib/fabulator/expr/node.rb', line 6

def vtype
  @vtype
end

Class Method Details

.new_context_environmentObject



63
64
65
66
67
68
# File 'lib/fabulator/expr/node.rb', line 63

def self.new_context_environment
  r = { }
  d = Fabulator::Expr::Node.new('data', r, nil, [])
  r['data'] = d
  d
end

Instance Method Details

#add_child(c) ⇒ Object



125
126
127
128
129
130
# File 'lib/fabulator/expr/node.rb', line 125

def add_child(c)
  c.parent.prune(c) if c.parent
  c.parent = self
  c.axis = self.axis
  @children << c
end

#anon_node(v, t = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/fabulator/expr/node.rb', line 70

def anon_node(v, t = nil)
  if v.is_a?(Array)
    n = self.class.new(self.axis, self.roots, nil, v.collect{ |vv| self.anon_node(vv, t) })
  else
    n = self.class.new(self.axis, self.roots, v, [])
    n.vtype = t unless t.nil?
  end
  n
end

#axisObject



34
35
36
# File 'lib/fabulator/expr/node.rb', line 34

def axis
  @axis
end

#axis=(a) ⇒ Object



38
39
40
41
# File 'lib/fabulator/expr/node.rb', line 38

def axis=(a)
  @axis = a
  self.children.each { |c| c.axis=a }
end

#children(n = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/fabulator/expr/node.rb', line 105

def children(n = nil)
  op = TagLib.find_op(@vtype, :children)
  possible = op.nil? ? @children : op.call(self)
  if n.nil?
    possible
  else
    possible.select{|c| c.name == n }
  end
end

#clone(deep = false) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/fabulator/expr/node.rb', line 80

def clone(deep = false)
  node = self.anon_node(self.value, self.vtype)
  node.name = self.name
  node.attributes = self.attributes.collect { |a| a.clone(deep) }
  node.copy(self) if deep
  node
end

#create_child(n, v = nil, t = nil) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/fabulator/expr/node.rb', line 88

def create_child(n,v = nil,t=nil)
  node = self.class.new(@axis, @roots, v, [], self)
  node.name = n
  node.vtype = t unless t.nil?
  @children << node
  node
end

#get_attribute(k) ⇒ Object



55
56
57
# File 'lib/fabulator/expr/node.rb', line 55

def get_attribute(k)
  (@attributes.select{ |a| a.name == k }.first rescue nil)
end

#is_attribute?Boolean

Returns:

  • (Boolean)


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

def is_attribute?
  @is_attribute
end

#parentObject



101
102
103
# File 'lib/fabulator/expr/node.rb', line 101

def parent
  @parent.nil? ? self : @parent
end

#parent=(p) ⇒ Object



96
97
98
99
# File 'lib/fabulator/expr/node.rb', line 96

def parent=(p)
  @parent = p
  @axis = p.axis
end

#prune(c = nil) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/fabulator/expr/node.rb', line 115

def prune(c = nil)
  if c.nil?
    @children = [ ]
  elsif c.is_a?(Array)
    @children = @children - c
  else
    @children = @children - [ c ]
  end
end

#set_attribute(k, v) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fabulator/expr/node.rb', line 43

def set_attribute(k, v)
  if v.is_a?(Fabulator::Expr::Node)
    v = v.clone
  else
    v = Fabulator::Expr::Node.new(self.axis, self.roots, v, [], self)
  end
  v.name = k
  v.is_attribute = true
  @attributes.delete_if{|a| a.name == k }
  @attributes << v
end