Class: Plucky::CriteriaHash
Constant Summary collapse
- SimpleIdQueryKeys =
Internal: Used to determine if criteria keys match simple id lookup.
[:_id].to_set
- SimpleIdAndTypeQueryKeys =
Internal: Used to determine if criteria keys match simple id and type lookup (for single collection inheritance).
[:_id, :_type].to_set
- SimpleQueryMaxSize =
Internal: Used to quickly check if it is possible that the criteria hash is simple.
[SimpleIdQueryKeys.size, SimpleIdAndTypeQueryKeys.size].max
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Private: The Hash that stores options.
-
#source ⇒ Object
readonly
Private: The Hash that stores query criteria.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Public.
-
#[](key) ⇒ Object
Public.
-
#[]=(key, value) ⇒ Object
Public The contents of this make me sad…need to clean it up.
-
#initialize(hash = {}, options = {}) ⇒ CriteriaHash
constructor
Public.
- #initialize_copy(original) ⇒ Object
-
#keys ⇒ Object
Public.
-
#merge(other) ⇒ Object
Public.
-
#merge!(other) ⇒ Object
Public.
- #object_id?(key) ⇒ Boolean
-
#object_ids ⇒ Object
Private.
-
#object_ids=(value) ⇒ Object
Private.
-
#simple? ⇒ Boolean
Public: The definition of simple is querying by only _id or _id and _type.
-
#to_hash ⇒ Object
Public.
Constructor Details
#initialize(hash = {}, options = {}) ⇒ CriteriaHash
Public
26 27 28 29 |
# File 'lib/plucky/criteria_hash.rb', line 26 def initialize(hash={}, ={}) @source, @options = {}, hash.each { |key, value| self[key] = value } end |
Instance Attribute Details
#options ⇒ Object (readonly)
Private: The Hash that stores options
12 13 14 |
# File 'lib/plucky/criteria_hash.rb', line 12 def @options end |
#source ⇒ Object (readonly)
Private: The Hash that stores query criteria
9 10 11 |
# File 'lib/plucky/criteria_hash.rb', line 9 def source @source end |
Instance Method Details
#==(other) ⇒ Object
Public
71 72 73 |
# File 'lib/plucky/criteria_hash.rb', line 71 def ==(other) @source == other.source end |
#[](key) ⇒ Object
Public
41 42 43 |
# File 'lib/plucky/criteria_hash.rb', line 41 def [](key) @source[key] end |
#[]=(key, value) ⇒ Object
Public The contents of this make me sad…need to clean it up
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/plucky/criteria_hash.rb', line 47 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(original) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/plucky/criteria_hash.rb', line 31 def initialize_copy(original) super @options = @options.dup @source = @source.dup @source.each do |key, value| self[key] = value.clone if value.duplicable? end end |
#keys ⇒ Object
Public
66 67 68 |
# File 'lib/plucky/criteria_hash.rb', line 66 def keys @source.keys end |
#merge(other) ⇒ Object
Public
81 82 83 |
# File 'lib/plucky/criteria_hash.rb', line 81 def merge(other) self.class.new hash_merge(@source, other.source) end |
#merge!(other) ⇒ Object
Public
86 87 88 89 90 91 |
# File 'lib/plucky/criteria_hash.rb', line 86 def merge!(other) merge(other).to_hash.each do |key, value| self[key] = value end self end |
#object_id?(key) ⇒ Boolean
104 105 106 |
# File 'lib/plucky/criteria_hash.rb', line 104 def object_id?(key) object_ids.include?(key.to_sym) end |
#object_ids ⇒ Object
Private
109 110 111 |
# File 'lib/plucky/criteria_hash.rb', line 109 def object_ids @options[:object_ids] ||= [] end |
#object_ids=(value) ⇒ Object
Private
114 115 116 117 |
# File 'lib/plucky/criteria_hash.rb', line 114 def object_ids=(value) raise ArgumentError unless value.is_a?(Array) @options[:object_ids] = value.flatten end |
#simple? ⇒ Boolean
Public: 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.
Returns true or false
98 99 100 101 102 |
# File 'lib/plucky/criteria_hash.rb', line 98 def simple? return false if keys.size > SimpleQueryMaxSize key_set = keys.to_set key_set == SimpleIdQueryKeys || key_set == SimpleIdAndTypeQueryKeys end |
#to_hash ⇒ Object
Public
76 77 78 |
# File 'lib/plucky/criteria_hash.rb', line 76 def to_hash @source end |