Class: Qrpm::Node

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

Direct Known Subclasses

ContainerNode, ValueNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name, expr) ⇒ Node

Returns a new instance of Node.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/qrpm/node.rb', line 31

def initialize(parent, name, expr)
  constrain parent, HashNode, ArrayNode, nil
  constrain name, Fragment::Fragment, String, Integer, nil
  constrain expr, Fragment::Fragment, Hash, Array
  @parent = parent
  @name = name
  @expr = expr
  @interpolated = false
  @parent&.send(:add_node, self)
  @path = Node.ref(@parent&.path, self.is_a?(DirectoryNode) ? @name.source : name) # FIXME
end

Instance Attribute Details

#exprObject (readonly) Also known as: value

The value of this node. This can be a Fragment::Expression (ValueNode), Hash, or Array object



23
24
25
# File 'lib/qrpm/node.rb', line 23

def expr
  @expr
end

#nameObject (readonly) Also known as: key

Name of node within its parent. ArrayNode element objects (this include files) has integer “names” and only DirectoryNode keys may contain references. It is initialized with a String or an Integer except for DirectoryNode objects that are initialized with a Fragment::Expression (directory nodes is not part of the dictionary). When computing directory paths, the source of the Fragment::Expression is used



19
20
21
# File 'lib/qrpm/node.rb', line 19

def name
  @name
end

#parentObject (readonly)

Parent node



5
6
7
# File 'lib/qrpm/node.rb', line 5

def parent
  @parent
end

#pathObject (readonly)

Path to node. This is a String expression that leads from the root node down to the current node. The expression may only contain variables if the node is a DirectoryNode but it can include integer indexes



11
12
13
# File 'lib/qrpm/node.rb', line 11

def path
  @path
end

Instance Method Details

#class_nameObject

Name of class



59
# File 'lib/qrpm/node.rb', line 59

def class_name = self.class.to_s.sub(/.*::/, "")

#dot(expr) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/qrpm/node.rb', line 84

def dot(expr)
  curr = self
  src = ".#{expr}" if expr[0] != "["
  while src =~ /^\.(#{IDENT_RE})|\[(\d+)\]/
    member, index = $1, $2
    src = $'
    if member
      curr.is_a?(HashNode) or raise ArgumentError, "#{member} is not a hash in '#{expr}'"
      curr.key?(member) or raise ArgumentError, "Unknown member '#{member}' in '#{expr}'"
      curr = curr[member]
    else
      curr.is_a?(ArrayNode) or raise ArgumentError, "#{curr.key_source} is not an array in '#{expr}'"
      curr.size > index.to_i or raise "Out of range index '#{index}' in '#{expr}'"
      curr = curr[index]
    end
  end
  src.empty? or raise ArgumentError, "Illegal expression: #{expr}"
  curr
end

#dumpObject



65
# File 'lib/qrpm/node.rb', line 65

def dump = abstract_method

#inspectObject



64
# File 'lib/qrpm/node.rb', line 64

def inspect = "#<#{self.class} #{path.inspect}>"

#interpolate(dict) ⇒ Object

Interpolate variables in Node. Note that interpolation is not recursive except for DirectoryNode objects that interpolates both key and elements. #interpolate sets the interpolated flag and returns self



50
51
52
53
# File 'lib/qrpm/node.rb', line 50

def interpolate(dict) 
  @interpolated = true
  self 
end

#interpolated?Boolean

True if object has been interpolated

Returns:

  • (Boolean)


56
# File 'lib/qrpm/node.rb', line 56

def interpolated? = @interpolated

#signature(klass: self.class_name, key: self.name) ⇒ Object

Signature. Used in tests



62
# File 'lib/qrpm/node.rb', line 62

def signature(klass: self.class_name, key: self.name) = abstract_method

#traverse(*klasses, &block) ⇒ Object

Traverse node and its children recursively and execute block on each node. The nodes are traversed in depth-first order. The optional klasses argument is a list of Class objects and the block is only executed on matching nodes. The default is to execute the block on all nodes. Returns an array of traversed nodes if no block is given



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/qrpm/node.rb', line 72

def traverse(*klasses, &block)
  klasses = klasses.flatten
  if block_given?
    yield self if klasses.empty? || klasses.include?(self.class)
    traverse_recursively(&block)
  else
    l = klasses.empty? || klasses.include?(self.class) ? [self] : []
    traverse_recursively { |n| l << n }
    l
  end
end

#variablesObject

Return list of variable names in #expr. Directory nodes may include variables in the key so they include key variables too



45
# File 'lib/qrpm/node.rb', line 45

def variables = abstract_method