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.



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

def ignore_failure
  @ignore_failure
end

#indexObject (readonly)

Returns the value of attribute index.



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

def index
  @index
end

Instance Method Details

#and(args = {}) ⇒ Object



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

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

#each(&block) ⇒ Object



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

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

#encodeObject

Search related methods



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

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

#not(args = {}) ⇒ Object



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

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

#or(args = {}) ⇒ Object



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

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

#partial(keys, &block) ⇒ Object



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

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

  begin
    if Kernel.block_given?
      Chef::PartialSearch.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



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

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



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

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

#where(args = {}) ⇒ Object

Spawn a new scopes



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

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