Class: Plucky::CriteriaHash
- Inherits:
-
Object
- Object
- Plucky::CriteriaHash
show all
- Defined in:
- lib/plucky/criteria_hash.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash = {}, options = {}) ⇒ CriteriaHash
Returns a new instance of CriteriaHash.
6
7
8
9
|
# File 'lib/plucky/criteria_hash.rb', line 6
def initialize(hash={}, options={})
@source, @options = {}, options
hash.each { |key, value| self[key] = value }
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
95
96
97
|
# File 'lib/plucky/criteria_hash.rb', line 95
def method_missing(method, *args, &block)
@source.send(method, *args, &block)
end
|
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
4
5
6
|
# File 'lib/plucky/criteria_hash.rb', line 4
def options
@options
end
|
#source ⇒ Object
Returns the value of attribute source.
4
5
6
|
# File 'lib/plucky/criteria_hash.rb', line 4
def source
@source
end
|
Instance Method Details
#==(other) ⇒ Object
34
35
36
|
# File 'lib/plucky/criteria_hash.rb', line 34
def ==(other)
source == other.source
end
|
#[]=(key, value) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/plucky/criteria_hash.rb', line 17
def []=(key, value)
normalized_key = normalized_key(key)
if key.is_a?(SymbolOperator)
operator = "$#{key.operator}"
normalized_value = normalized_value(normalized_key, operator, value)
source[normalized_key] ||= {}
source[normalized_key][operator] = normalized_value
else
if key == :conditions
value.each { |k, v| self[k] = v }
else
normalized_value = normalized_value(normalized_key, normalized_key, value)
source[normalized_key] = normalized_value
end
end
end
|
#initialize_copy(source) ⇒ Object
11
12
13
14
15
|
# File 'lib/plucky/criteria_hash.rb', line 11
def initialize_copy(source)
super
@options = @options.dup
@source = @source.dup
end
|
#merge(other) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/plucky/criteria_hash.rb', line 42
def merge(other)
target = source.dup
other.source.each_key do |key|
value, other_value = target[key], other[key]
target[key] =
if target.key?(key)
value_is_hash = value.is_a?(Hash)
other_is_hash = other_value.is_a?(Hash)
if value_is_hash && other_is_hash
value.update(other_value) do |key, old_value, new_value|
Array(old_value).concat(Array(new_value)).uniq
end
elsif value_is_hash && !other_is_hash
if modifier_key = value.keys.detect { |k| k.to_s[0, 1] == '$' }
value[modifier_key].concat(Array(other_value)).uniq!
else
end
elsif other_is_hash && !value_is_hash
if modifier_key = other_value.keys.detect { |k| k.to_s[0, 1] == '$' }
other_value[modifier_key].concat(Array(value)).uniq!
else
end
else
Array(value).concat(Array(other_value)).uniq
end
else
other_value
end
end
self.class.new(target)
end
|
#object_ids ⇒ Object
77
78
79
|
# File 'lib/plucky/criteria_hash.rb', line 77
def object_ids
@options[:object_ids] ||= []
end
|
#object_ids=(value) ⇒ Object
81
82
83
84
|
# File 'lib/plucky/criteria_hash.rb', line 81
def object_ids=(value)
raise ArgumentError unless value.is_a?(Array)
@options[:object_ids] = value.flatten
end
|
#simple? ⇒ Boolean
The definition of simple is querying by only _id or _id and _type. If this is the case, you can use IdentityMap in library to not perform query and instead just return from map.
89
90
91
92
|
# File 'lib/plucky/criteria_hash.rb', line 89
def simple?
key_set = keys.to_set
key_set == [:_id].to_set || key_set == [:_id, :_type].to_set
end
|
#to_hash ⇒ Object
38
39
40
|
# File 'lib/plucky/criteria_hash.rb', line 38
def to_hash
source
end
|