Class: Brief::DocumentMapper::Query
- Inherits:
-
Object
- Object
- Brief::DocumentMapper::Query
show all
- Defined in:
- lib/brief/document_mapper.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(model) ⇒ Query
Returns a new instance of Query.
33
34
35
36
|
# File 'lib/brief/document_mapper.rb', line 33
def initialize(model)
@model = model
@where = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
141
142
143
144
145
146
147
|
# File 'lib/brief/document_mapper.rb', line 141
def method_missing(meth, *args, &block)
if all.respond_to?(meth)
all.send(meth, *args, &block)
else
super
end
end
|
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
31
32
33
|
# File 'lib/brief/document_mapper.rb', line 31
def model
@model
end
|
Instance Method Details
#all ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/brief/document_mapper.rb', line 100
def all
results = run_query
if @order_by
order_by_attr = @order_by.keys.first
direction = @order_by.values.first
results.select! do |result|
result.respond_to?(order_by_attr)
end
results.sort_by! do |result|
result.send(order_by_attr)
end
results.reverse! if direction == :desc
end
if @offset.present?
results = results.last([results.size - @offset, 0].max)
end
if @limit.present?
results = results.first(@limit)
end
results
end
|
#first ⇒ Object
64
65
66
|
# File 'lib/brief/document_mapper.rb', line 64
def first
all.first
end
|
#inspect ⇒ Object
137
138
139
|
# File 'lib/brief/document_mapper.rb', line 137
def inspect
"Query: #{ @where.map { |k, v| "#{k.attribute} #{k.operator} #{v}" }}"
end
|
#last ⇒ Object
68
69
70
|
# File 'lib/brief/document_mapper.rb', line 68
def last
all.last
end
|
#limit(number) ⇒ Object
59
60
61
62
|
# File 'lib/brief/document_mapper.rb', line 59
def limit(number)
@limit = number
self
end
|
#offset(number) ⇒ Object
54
55
56
57
|
# File 'lib/brief/document_mapper.rb', line 54
def offset(number)
@offset = number
self
end
|
#order_by(field) ⇒ Object
49
50
51
52
|
# File 'lib/brief/document_mapper.rb', line 49
def order_by(field)
@order_by = field.is_a?(Symbol) ? { field => :asc } : field
self
end
|
#query_is_empty? ⇒ Boolean
133
134
135
|
# File 'lib/brief/document_mapper.rb', line 133
def query_is_empty?
@where.empty? && @limit.nil? && @order_by.nil?
end
|
#run_query ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/brief/document_mapper.rb', line 72
def run_query
if query_is_empty?
model.to_a
else
model.select do |obj|
match = true
@where.each do |selector, value|
obj = obj.symbolize_keys if obj.is_a?(Hash)
if obj.respond_to?(selector.attribute)
test_value = obj.send(selector.attribute)
operator = OPERATOR_MAPPING[selector.operator]
match = false unless test_value.send(operator, value)
elsif obj.key?(selector.attribute.to_sym)
test_value = obj.send(:[], selector.attribute.to_sym)
operator = OPERATOR_MAPPING[selector.operator]
match = false unless test_value.send(operator, value)
else
match = false
end
end
match
end
end
end
|
#to_a ⇒ Object
129
130
131
|
# File 'lib/brief/document_mapper.rb', line 129
def to_a
all
end
|
#where(constraints_hash) ⇒ Object
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/brief/document_mapper.rb', line 38
def where(constraints_hash)
selector_hash = constraints_hash.reject { |key, _value| !key.is_a? Selector }
symbol_hash = constraints_hash.reject { |key, _value| key.is_a? Selector }
symbol_hash.each do |attribute, value|
selector = Selector.new(attribute: attribute, operator: 'equal')
selector_hash.update(selector => value)
end
@where.merge! selector_hash
self
end
|