Class: QueryInterface::Server::LazyQuery
- Inherits:
-
Object
- Object
- QueryInterface::Server::LazyQuery
show all
- Defined in:
- lib/query-interface-server/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
|
# File 'lib/query-interface-server/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_model = result_model
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
114
115
116
|
# File 'lib/query-interface-server/lazy_query.rb', line 114
def method_missing(method_name, *args, &block)
evaluate.send(method_name, *args, &block)
end
|
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
5
6
7
|
# File 'lib/query-interface-server/lazy_query.rb', line 5
def model
@model
end
|
#result_model ⇒ Object
Returns the value of attribute result_model.
5
6
7
|
# File 'lib/query-interface-server/lazy_query.rb', line 5
def result_model
@result_model
end
|
Returns the value of attribute transformations.
5
6
7
|
# File 'lib/query-interface-server/lazy_query.rb', line 5
def transformations
@transformations
end
|
Instance Method Details
33
34
35
|
# File 'lib/query-interface-server/lazy_query.rb', line 33
def add_transformation(type, parameter=nil)
self.transformations << {'transformation' => type, 'parameter' => parameter}
end
|
#context(association, model = nil) ⇒ Object
51
52
53
54
55
56
|
# File 'lib/query-interface-server/lazy_query.rb', line 51
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.to_s)
end
end
|
#copy(options = {}) ⇒ Object
29
30
31
|
# File 'lib/query-interface-server/lazy_query.rb', line 29
def copy(options = {})
self.class.new(self.model, self.transformations, self.result_model)
end
|
#count ⇒ Object
91
92
93
94
95
|
# File 'lib/query-interface-server/lazy_query.rb', line 91
def count
query = self.copy
query.add_transformation("count")
query.do_query[:count]
end
|
#dataset ⇒ Object
81
82
83
|
# File 'lib/query-interface-server/lazy_query.rb', line 81
def dataset
self.do_query(false)
end
|
#do_query(add_default_order = true) ⇒ Object
97
98
99
100
|
# File 'lib/query-interface-server/lazy_query.rb', line 97
def do_query(add_default_order=true)
transformer = Transformations::SequelTransformer.new(self.model.filter)
transformer.run(self.transformations, add_default_order)
end
|
#evaluate ⇒ Object
77
78
79
|
# File 'lib/query-interface-server/lazy_query.rb', line 77
def evaluate
self.do_query
end
|
#filter(conditions = {}) ⇒ Object
37
38
39
40
41
42
43
|
# File 'lib/query-interface-server/lazy_query.rb', line 37
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
102
103
104
|
# File 'lib/query-interface-server/lazy_query.rb', line 102
def first(*args)
one("first", *args)
end
|
#ids ⇒ Object
85
86
87
88
89
|
# File 'lib/query-interface-server/lazy_query.rb', line 85
def ids
query = self.copy
query.add_transformation("map_ids")
query.do_query
end
|
#instance(id) ⇒ Object
45
46
47
48
49
|
# File 'lib/query-interface-server/lazy_query.rb', line 45
def instance(id)
self.copy.tap do |query|
query.add_transformation("instance", id)
end
end
|
#instantiate(data) ⇒ Object
21
22
23
|
# File 'lib/query-interface-server/lazy_query.rb', line 21
def instantiate(data)
(self.result_model ? self.result_model.new(data) : self.model.new(data))
end
|
#instantiate_collection(parsed_data) ⇒ Object
25
26
27
|
# File 'lib/query-interface-server/lazy_query.rb', line 25
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
106
107
108
|
# File 'lib/query-interface-server/lazy_query.rb', line 106
def last(*args)
one("last", *args)
end
|
#order(*fields) ⇒ Object
69
70
71
72
73
74
75
|
# File 'lib/query-interface-server/lazy_query.rb', line 69
def order(*fields)
self.copy.tap do |query|
fields.each do |field|
query.add_transformation("order", field)
end
end
end
|
#parse(data) ⇒ Object
17
18
19
|
# File 'lib/query-interface-server/lazy_query.rb', line 17
def parse(data)
(self.result_model ? self.result_model.parse(data) : self.model.parse(data))
end
|
#to_json(*args) ⇒ Object
110
111
112
|
# File 'lib/query-interface-server/lazy_query.rb', line 110
def to_json(*args)
evaluate.to_json(*args)
end
|
#with(*fields, **opts) ⇒ Object
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/query-interface-server/lazy_query.rb', line 58
def with(*fields, **opts)
self.copy.tap do |query|
fields.each do |field|
query.add_transformation("with", {"field" => field.to_s})
end
opts.each do |field, param|
query.add_transformation("with", {"field" => field.to_s, "param" => param})
end
end
end
|