Class: PuppetDB::ASTNode
- Inherits:
-
Object
- Object
- PuppetDB::ASTNode
- Defined in:
- lib/puppetdb/astnode.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #capitalize! ⇒ Object
-
#evaluate(mode = :nodes) ⇒ Array
Evalutate the node and all children.
-
#evaluate_children(mode) ⇒ Array
Evaluate all children nodes.
-
#initialize(type, value, children = []) ⇒ ASTNode
constructor
A new instance of ASTNode.
-
#optimize ⇒ Object
Go through the AST and optimize boolean expressions into triplets etc Changes the AST in place.
-
#subquery(from_mode, to_mode, query) ⇒ Array
Generate the the query code for a subquery.
Constructor Details
#initialize(type, value, children = []) ⇒ ASTNode
Returns a new instance of ASTNode.
4 5 6 7 8 |
# File 'lib/puppetdb/astnode.rb', line 4 def initialize(type, value, children=[]) @type = type @value = value @children = children end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
2 3 4 |
# File 'lib/puppetdb/astnode.rb', line 2 def children @children end |
#type ⇒ Object
Returns the value of attribute type.
2 3 4 |
# File 'lib/puppetdb/astnode.rb', line 2 def type @type end |
#value ⇒ Object
Returns the value of attribute value.
2 3 4 |
# File 'lib/puppetdb/astnode.rb', line 2 def value @value end |
Instance Method Details
#capitalize! ⇒ Object
10 11 12 13 |
# File 'lib/puppetdb/astnode.rb', line 10 def capitalize! @value=@value.to_s.split("::").collect { |s| s.capitalize }.join("::") return self end |
#evaluate(mode = :nodes) ⇒ Array
Evalutate the node and all children
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/puppetdb/astnode.rb', line 49 def evaluate(mode = :nodes) case @type when :booleanop return [@value.to_s, *evaluate_children(mode)] when :subquery return subquery(mode, @value, *evaluate_children(@value)) when :exp case @value when :equals then op = '=' when :greaterthan then op = '>' when :lessthan then op = '<' when :match then op = '~' end case mode when :nodes,:facts # Do a subquery to match nodes matching the facts return subquery(mode, :facts, ['and', ['=', 'name', @children[0].evaluate(mode)], [op, 'value', @children[1].evaluate(mode)]]) when :resources return [op, ['parameter', @children[0].evaluate(mode)], @children[1].evaluate(mode)] end when :string return @value.to_s when :number return @value when :boolean return @value when :resourcetitle return ['=', 'title', @value] when :resourcetype return ['=', 'type', @value] when :resexported return ['=', 'exported', @value] end end |
#evaluate_children(mode) ⇒ Array
Evaluate all children nodes
87 88 89 |
# File 'lib/puppetdb/astnode.rb', line 87 def evaluate_children(mode) return children.collect { |c| c.evaluate mode } end |
#optimize ⇒ Object
Go through the AST and optimize boolean expressions into triplets etc Changes the AST in place
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/puppetdb/astnode.rb', line 31 def optimize case @type when :booleanop @children.each do |c| if c.type == :booleanop and c.value == @value c.children.each { |cc| @children << cc } @children.delete c end end end @children.each { |c| c.optimize } return self end |
#subquery(from_mode, to_mode, query) ⇒ Array
Generate the the query code for a subquery
21 22 23 24 25 |
# File 'lib/puppetdb/astnode.rb', line 21 def subquery(from_mode, to_mode, query) return ['in', (from_mode == :nodes) ? 'name' : 'certname', ['extract', (to_mode == :nodes) ? 'name' : 'certname', ["select-#{to_mode.to_s}", query]]] end |