Class: Hieracles::Puppetdb::Query
- Inherits:
-
Object
- Object
- Hieracles::Puppetdb::Query
- Defined in:
- lib/hieracles/puppetdb/query.rb
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
Instance Method Summary collapse
- #build_and(arrays) ⇒ Object
- #build_or(arrays) ⇒ Object
- #expression(e) ⇒ Object
-
#initialize(elements) ⇒ Query
constructor
A new instance of Query.
- #number_or(a) ⇒ Object
- #parse(elements) ⇒ Object
Constructor Details
#initialize(elements) ⇒ Query
Returns a new instance of Query.
7 8 9 |
# File 'lib/hieracles/puppetdb/query.rb', line 7 def initialize(elements) @elements = parse(elements) end |
Instance Attribute Details
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
5 6 7 |
# File 'lib/hieracles/puppetdb/query.rb', line 5 def elements @elements end |
Instance Method Details
#build_and(arrays) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/hieracles/puppetdb/query.rb', line 18 def build_and(arrays) back = arrays.reduce([]) do |a, v| if v.class.name == 'Array' if v.length > 1 a << ['and'] + v.map { |e| expression(e) } else e = expression(v[0]) a << e if e end elsif v == 'or' a << 'or' else e = expression(v) a << e if e end a end if back.length == 1 and back[0].class.name == 'Array' back = back[0] elsif back[0].class.name == 'Array' and back.length > 1 back.unshift('and') end back end |
#build_or(arrays) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/hieracles/puppetdb/query.rb', line 43 def build_or(arrays) back = ['or'] (0..number_or(arrays)-1).each do |a| back << arrays.slice!(0..arrays.index('or')-1) arrays.shift end back << arrays end |
#expression(e) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/hieracles/puppetdb/query.rb', line 52 def expression(e) if /([^!]*)(!)?(>|<|=|~)(.*)/.match e if $2 ['not', [$3, $1, $4]] else [$3, $1, $4] end end end |
#number_or(a) ⇒ Object
62 63 64 |
# File 'lib/hieracles/puppetdb/query.rb', line 62 def number_or(a) a.select { |i| i == 'or' }.length end |
#parse(elements) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/hieracles/puppetdb/query.rb', line 11 def parse(elements) if number_or(elements) > 0 elements = build_or(elements) end build_and elements end |