Class: QueryInterface::Server::LazyQuery

Inherits:
Object
  • Object
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



128
129
130
# File 'lib/query-interface-server/lazy_query.rb', line 128

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-server/lazy_query.rb', line 5

def model
  @model
end

#result_modelObject

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

#transformationsObject

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

#add_transformation(type, parameter = nil) ⇒ Object



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



59
60
61
62
63
64
# File 'lib/query-interface-server/lazy_query.rb', line 59

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

#countObject



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

def count
  query = self.copy
  query.add_transformation("count")
  query.do_query[:count]
end

#datasetObject



89
90
91
# File 'lib/query-interface-server/lazy_query.rb', line 89

def dataset
  self.do_query(false)
end

#do_query(add_default_order = true) ⇒ Object



111
112
113
114
# File 'lib/query-interface-server/lazy_query.rb', line 111

def do_query(add_default_order=true)
  transformer = Transformations::SequelTransformer.new(self.model.filter)
  transformer.run(self.transformations, add_default_order)
end

#evaluateObject



85
86
87
# File 'lib/query-interface-server/lazy_query.rb', line 85

def evaluate
  self.do_query
end

#exclude(conditions = {}) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/query-interface-server/lazy_query.rb', line 37

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



45
46
47
48
49
50
51
# File 'lib/query-interface-server/lazy_query.rb', line 45

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



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

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

#idsObject



93
94
95
96
97
# File 'lib/query-interface-server/lazy_query.rb', line 93

def ids
  query = self.copy
  query.add_transformation("map_ids")
  query.do_query
end

#instance(id) ⇒ Object



53
54
55
56
57
# File 'lib/query-interface-server/lazy_query.rb', line 53

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



120
121
122
# File 'lib/query-interface-server/lazy_query.rb', line 120

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

#order(*fields) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/query-interface-server/lazy_query.rb', line 77

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



124
125
126
# File 'lib/query-interface-server/lazy_query.rb', line 124

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

#update(data) ⇒ Object



105
106
107
108
109
# File 'lib/query-interface-server/lazy_query.rb', line 105

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

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



66
67
68
69
70
71
72
73
74
75
# File 'lib/query-interface-server/lazy_query.rb', line 66

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