Module: Fabulator::Expr::NodeLogic

Included in:
Node
Defined in:
lib/fabulator/expr/node_logic.rb

Instance Method Summary collapse

Instance Method Details

#copy(c) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fabulator/expr/node_logic.rb', line 67

def copy(c)
  self.value = c.value
  self.vtype = c.vtype
  # TODO: attributes
  
  c.attributes.each do |a|
    self.set_attribute(a.name,a.value)
  end
  c.children.each do |cc|
    n = self.create_child(cc.name, cc.value)
    n.copy(cc)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/fabulator/expr/node_logic.rb', line 81

def empty?
  self.value.nil? && self.children.empty?
end

#in_context(&block) ⇒ Object



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

def in_context(&block)
    yield
end

#pathObject



59
60
61
62
63
64
65
# File 'lib/fabulator/expr/node_logic.rb', line 59

def path
  if self.parent.nil? || self.parent == self
    return ''
  else
    return self.parent.path + '/' + (self.is_attribute? ? '@' : '') + self.name
  end
end

#root(a = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fabulator/expr/node_logic.rb', line 85

def root(a = nil)
  if(a.nil? || a == '')
    a = self.axis
  end
  if a.nil? || a == '' || self.roots[a].nil?
    p = self
    while !p.parent.nil? && p.parent != self
      p = p.parent
    end
    self.roots[a] = p unless a.nil? || a == ''
    return p
  else
    self.roots[a.nil? ? self.axis : a]
  end
end

#to(t, ctx = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fabulator/expr/node_logic.rb', line 24

def to(t, ctx = nil)
  if @vtype.nil? || t.nil?
    return self.anon_node(self.value, self.vtype)
  end
  if self.vtype.join('') == t.join('')
    return self
  end
  # see if there's a path between @vtype and t
  #   if so, do the conversion
  #   otherwise, return nil
  path = Fabulator::TagLib.type_path(self.vtype, t)
  return self.anon_node(nil,nil) if path.empty?
  v = self
  ctx = Fabulator::Expr::Context.new if ctx.nil?
  path.each do |p|
    vv = nil
    begin
      vv = p.convert(ctx.with_root(v))
    rescue => e
      raise "Converting to #{t[1]} raised #{e}"
    end
    if vv.is_a?(Fabulator::Expr::Node)
      v = vv
    else
      v = self.anon_node(vv)
    end
  end
  v.vtype = t
  return v
end

#to_hObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fabulator/expr/node_logic.rb', line 8

def to_h
  r = { }
  #  :attributes => { },

  r[:name] = self.name unless self.name.nil?
  cs = self.children.collect { |c| c.to_h }
  r[:children] = cs unless cs.empty?
  r[:value] = self.value unless self.value.nil?
  r[:type] = self.vtype.join('') unless self.vtype.nil?
  r[:attributes] = { }
  self.attributes.each do |a|
    r[:attributes][a.name] = a.value
  end
  r
end

#to_sObject



4
5
6
# File 'lib/fabulator/expr/node_logic.rb', line 4

def to_s
  self.to([ FAB_NS, 'string' ]).value
end