Class: Hieracles::Puppetdb::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/hieracles/puppetdb/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(elements) ⇒ Query

Returns a new instance of Query.



5
6
7
# File 'lib/hieracles/puppetdb/query.rb', line 5

def initialize(elements)
  @elements = parse(elements)
end

Instance Method Details

#build_and(arrays) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/hieracles/puppetdb/query.rb', line 47

def build_and(arrays)
  if arrays.length > 1
    ['and', arrays]
  else
    arrays
  end
end

#defined?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/hieracles/puppetdb/query.rb', line 55

def defined?
  @elements.count > 0
end

#match(item) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hieracles/puppetdb/query.rb', line 59

def match(item)
  @elements.each do |e|
    matched = false
    e.each do |a|
      case a[0]
      when '='

      when '!='
      when '<'
      when '>'
      when '!'
      else
      end
    end
    return true if matched
  end
end

#outputObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hieracles/puppetdb/query.rb', line 32

def output
  back = []
  if @elements.length > 1
    back << 'or' 
    @elements.each do |e|
      back << build_and(e)
    end
  else
    @elements.each do |e|
      back << build_and(e)
    end
  end
  back
end

#parse(elements) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hieracles/puppetdb/query.rb', line 9

def parse(elements)
  items = []
  index = 0
  if elements.length > 1
    elements.each do |e|
      puts items.inspect
      if e == 'or'
        index++
        continue
      end
      if /(.*)(>|<|=|!=|~)(.*)/.match e
        items[index] ||= []
        items[index] << [$2, $1, $3]
      end
    end
  else
    if /(.*)(>|<|=|!=|~)(.*)/.match elements[0]
      items = [$2, $1, $3]
    end
  end
  items
end