Class: QuerySyntax::Query

Inherits:
NestedScope show all
Defined in:
lib/query_syntax/query.rb

Instance Attribute Summary collapse

Attributes inherited from NestedScope

#scopes

Attributes inherited from Scope

#operator

Instance Method Summary collapse

Methods inherited from NestedScope

#and!, #compact, #compact!, #conditions, #empty?, #nest!, #not!, #or!, #push, #scope, #scope!, #to_s, #where!

Constructor Details

#initialize(index) ⇒ Query

Returns a new instance of Query.



6
7
8
9
10
# File 'lib/query_syntax/query.rb', line 6

def initialize(index)
  super "AND"
  @index = index
  @ignore_failure = true
end

Instance Attribute Details

#ignore_failureObject

Returns the value of attribute ignore_failure.



12
13
14
# File 'lib/query_syntax/query.rb', line 12

def ignore_failure
  @ignore_failure
end

Instance Method Details

#and(args = {}) ⇒ Object



38
39
40
41
# File 'lib/query_syntax/query.rb', line 38

def and(args={})
  scope = spawn("AND")
  scope.and!(args)
end

#each(&block) ⇒ Object



97
98
99
# File 'lib/query_syntax/query.rb', line 97

def each(&block)
  search(&block)
end

#encodeObject

Search related methods



51
52
53
# File 'lib/query_syntax/query.rb', line 51

def encode
  URI.escape(to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end

#not(args = {}) ⇒ Object



33
34
35
36
# File 'lib/query_syntax/query.rb', line 33

def not(args={})
  scope = spawn("NOT")
  scope.not!(args)
end

#or(args = {}) ⇒ Object



43
44
45
46
# File 'lib/query_syntax/query.rb', line 43

def or(args={})
  scope = spawn("OR")
  scope.or!(args)
end

#partial(keys, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/query_syntax/query.rb', line 75

def partial(keys, &block)
  keys = Hash[keys.map { |k,v| [k,Array(v)] }]

  begin
    if Kernel.block_given?
      Chef::PartialSearc.new.search(index, encode, keys:keys, &block)
    else
      results = Array.new
      partial(keys) { |result| results << result }
      results
    end
  rescue Net::HTTPServerException => e
    error = Chef::JSONCompat.from_json(e.response.body)["error"].first
    Chef::Log.error("Search failed with : #{error}")

    if @ignore_failure
    then Array.new
    else raise Net::HTTPServerException
    end
  end
end

#search(&block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/query_syntax/query.rb', line 55

def search(&block)
  begin
    if Kernel.block_given?
      Chef::Search::Query.new.search(index, encode, &block)
    else 
      results = Array.new
      search { |result| results << result }
      results
    end
  rescue Net::HTTPServerException => e
    error = Chef::JSONCompat.from_json(e.response.body)["error"].first
    Chef::Log.error("Search failed with : #{error}")

    if @ignore_failure
    then Array.new
    else raise Net::HTTPServerException
    end
  end
end

#spawn(operator = "AND") ⇒ Object

Spawn! returns a clone to continue processing without mucking self



17
18
19
20
21
22
# File 'lib/query_syntax/query.rb', line 17

def spawn(operator="AND")
  scope = clone
  scope.scopes = scopes.clone
  scope.scope!(operator)
  scope
end

#where(args = {}) ⇒ Object

Spawn a new scopes



28
29
30
31
# File 'lib/query_syntax/query.rb', line 28

def where(args={})
  scope = spawn("AND")
  scope.where!(args) if args
end