Class: PuppetDB::Query

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query = []) ⇒ Query

Returns a new instance of Query.



7
8
9
# File 'lib/puppetdb/query.rb', line 7

def initialize(query = [])
  @sexpr = query
end

Instance Attribute Details

#sexprObject (readonly)

Returns the value of attribute sexpr.



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

def sexpr
  @sexpr
end

Class Method Details

.[](*args) ⇒ Object



11
12
13
# File 'lib/puppetdb/query.rb', line 11

def self.[](*args)
  Query.new(args)
end

.maybe_promote(query) ⇒ Object



15
16
17
18
# File 'lib/puppetdb/query.rb', line 15

def self.maybe_promote(query)
  return Query.new(query) unless query.class == Query
  query
end

Instance Method Details

#and(query) ⇒ Object



42
43
44
# File 'lib/puppetdb/query.rb', line 42

def and(query)
  compose(query) { |q| Query.new([:and, @sexpr, q.sexpr]) }
end

#buildObject



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

def build
  JSON.dump(@sexpr)
end

#compose(query) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puppetdb/query.rb', line 24

def compose(query)
  query = self.class.maybe_promote(query)

  # If an operand is the empty query ([]), compose returns a copy
  # of the non-empty operand. If both operands are empty, the
  # empty query is returned. If both operands are non-empty, the
  # compose continues.
  if query.empty? && !empty?
    Query.new(@sexpr)
  elsif empty? && !query.empty?
    Query.new(query.sexpr)
  elsif empty? && query.empty?
    Query.new([])
  else
    yield query
  end
end

#empty?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/puppetdb/query.rb', line 20

def empty?
  @sexpr.empty?
end

#or(query) ⇒ Object



46
47
48
# File 'lib/puppetdb/query.rb', line 46

def or(query)
  compose(query) { |q| Query.new([:or, @sexpr, q.sexpr]) }
end

#push(query) ⇒ Object



50
51
52
# File 'lib/puppetdb/query.rb', line 50

def push(query)
  compose(query) { |q| Query.new(@sexpr.dup.push(q.sexpr)) }
end