Class: QueryInterface::Client::LazyQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/query-interface-client/lazy_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, transformations = nil, result_model = nil) ⇒ LazyQuery

Returns a new instance of LazyQuery.



7
8
9
10
11
12
13
14
15
16
# File 'lib/query-interface-client/lazy_query.rb', line 7

def initialize(model, transformations=nil, result_model=nil)
  self.model = model
  if transformations
    self.transformations = transformations.map {|item| item.dup}
  else
    self.transformations = []
  end
  self.result = nil
  self.result_model = result_model
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



151
152
153
# File 'lib/query-interface-client/lazy_query.rb', line 151

def method_missing(method_name, *args, &block)
  evaluate.send(method_name, *args, &block)
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/query-interface-client/lazy_query.rb', line 5

def model
  @model
end

#resultObject

Returns the value of attribute result.



5
6
7
# File 'lib/query-interface-client/lazy_query.rb', line 5

def result
  @result
end

#result_modelObject

Returns the value of attribute result_model.



5
6
7
# File 'lib/query-interface-client/lazy_query.rb', line 5

def result_model
  @result_model
end

#transformationsObject

Returns the value of attribute transformations.



5
6
7
# File 'lib/query-interface-client/lazy_query.rb', line 5

def transformations
  @transformations
end

Instance Method Details

#add_transformation(type, parameter = nil) ⇒ Object



30
31
32
# File 'lib/query-interface-client/lazy_query.rb', line 30

def add_transformation(type, parameter=nil)
  self.transformations << {transformation: type, parameter: parameter}
end

#context(association, model = nil) ⇒ Object



56
57
58
59
60
61
# File 'lib/query-interface-client/lazy_query.rb', line 56

def context(association, model=nil)
  self.copy.tap do |query|
    query.result_model = (model ? model : association.to_s.singularize.camelize.constantize)
    query.add_transformation(:context, association)
  end
end

#copy(options = {}) ⇒ Object



26
27
28
# File 'lib/query-interface-client/lazy_query.rb', line 26

def copy(options = {})
  self.class.new(self.model, self.transformations, self.result_model)
end

#countObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/query-interface-client/lazy_query.rb', line 105

def count
  if self.result
    self.result.count
  else
    query = self.copy
    query.add_transformation(:count)
    r = query.do_raw_query()
    r[:parsed_data][:data][:count]
  end
end

#do_queryObject



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/query-interface-client/lazy_query.rb', line 122

def do_query
  parsed_data = self.do_raw_query[:parsed_data]
  if parsed_data[:data].is_a?(Array)
    self.instantiate_collection(parsed_data)
  else
    unless parsed_data[:data].empty?
      self.instantiate(parsed_data[:data])
    else
      nil
    end
  end
end

#do_raw_queryObject



135
136
137
# File 'lib/query-interface-client/lazy_query.rb', line 135

def do_raw_query
  self.model.get_raw(path: :query, params: {transformations: JSON.dump(self.transformations)})
end

#evaluateObject



82
83
84
# File 'lib/query-interface-client/lazy_query.rb', line 82

def evaluate
  self.result ||= self.do_query()
end

#exclude(conditions = {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/query-interface-client/lazy_query.rb', line 34

def exclude(conditions={})
  self.copy.tap do |query|
    conditions.each do |key, value|
      query.add_transformation(:exclude, {field: key, value: value})
    end
  end
end

#filter(conditions = {}) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/query-interface-client/lazy_query.rb', line 42

def filter(conditions={})
  self.copy.tap do |query|
    conditions.each do |key, value|
      query.add_transformation(:filter, {field: key, value: value})
    end
  end
end

#first(*args) ⇒ Object



139
140
141
# File 'lib/query-interface-client/lazy_query.rb', line 139

def first(*args)
  one(:first, *args)
end

#idsObject



99
100
101
102
103
# File 'lib/query-interface-client/lazy_query.rb', line 99

def ids
  query = self.copy
  query.add_transformation(:map_ids)
  query.do_raw_query()[:parsed_data][:data]
end

#instance(id) ⇒ Object



50
51
52
53
54
# File 'lib/query-interface-client/lazy_query.rb', line 50

def instance(id)
  self.copy.tap do |query|
    query.add_transformation(:instance, id)
  end
end

#instantiate(data) ⇒ Object



18
19
20
# File 'lib/query-interface-client/lazy_query.rb', line 18

def instantiate(data)
  (self.result_model ? self.result_model.new_from_hash(data) : self.model.new_from_hash(data))
end

#instantiate_collection(parsed_data) ⇒ Object



22
23
24
# File 'lib/query-interface-client/lazy_query.rb', line 22

def instantiate_collection(parsed_data)
  (self.result_model ? self.result_model.new_collection(parsed_data) : self.model.new_collection(parsed_data))
end

#last(*args) ⇒ Object



143
144
145
# File 'lib/query-interface-client/lazy_query.rb', line 143

def last(*args)
  one(:last, *args)
end

#order(*fields) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/query-interface-client/lazy_query.rb', line 74

def order(*fields)
  self.copy.tap do |query|
    fields.each do |field|
      query.add_transformation(:order, field)
    end
  end
end

#paginate(page: 1, per_page: 10) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/query-interface-client/lazy_query.rb', line 90

def paginate(page: 1, per_page: 10)
  query = self.copy
  query.add_transformation(:paginate, {page: page, per_page: per_page})
  raw = query.do_raw_query()
  result = raw[:parsed_data][:data]
  objects = result[:objects].map { |h| query.instantiate(h) }
  RsPaginator::Collection.new(objects, page, per_page, result[:total])
end

#to_json(*args) ⇒ Object



147
148
149
# File 'lib/query-interface-client/lazy_query.rb', line 147

def to_json(*args)
  evaluate.to_json(*args)
end

#to_paramObject



86
87
88
# File 'lib/query-interface-client/lazy_query.rb', line 86

def to_param
  persisted? ? to_key.join('-') : nil
end

#update(data) ⇒ Object



116
117
118
119
120
# File 'lib/query-interface-client/lazy_query.rb', line 116

def update(data)
  query = self.copy
  query.add_transformation(:update, data)
  query.do_raw_query()[:parsed_data][:data][:updated_count]
end

#with(*fields, **opts) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/query-interface-client/lazy_query.rb', line 63

def with(*fields, **opts)
  self.copy.tap do |query|
    fields.each do |field|
      query.add_transformation(:with, {field: field})
    end
    opts.each do |field, param|
      query.add_transformation(:with, {field: field, param: param})
    end
  end
end