Class: Plucky::CriteriaHash

Inherits:
Object 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 (private)



104
105
106
# File 'lib/plucky/criteria_hash.rb', line 104

def method_missing(method, *args, &block)
  @source.send(method, *args, &block)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/plucky/criteria_hash.rb', line 4

def options
  @options
end

#sourceObject (readonly)

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



37
38
39
# File 'lib/plucky/criteria_hash.rb', line 37

def ==(other)
  source == other.source
end

#[]=(key, value) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/plucky/criteria_hash.rb', line 20

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
16
17
18
# File 'lib/plucky/criteria_hash.rb', line 11

def initialize_copy(source)
  super
  @options = @options.dup
  @source  = @source.dup
  each do |key, value|
    self[key] = value.clone if value.duplicable?
  end
end

#merge(other) ⇒ Object



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
76
77
78
# File 'lib/plucky/criteria_hash.rb', line 45

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
            # kaboom! Array(value).concat(Array(other_value)).uniq
          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
            # kaboom! Array(value).concat(Array(other_value)).uniq
          end
        else
          Array(value).concat(Array(other_value)).uniq
        end
      else
        other_value
      end
  end
  self.class.new(target)
end

#merge!(other) ⇒ Object



80
81
82
83
84
# File 'lib/plucky/criteria_hash.rb', line 80

def merge!(other)
  merge(other).to_hash.each do |key, value|
    self[key] = value
  end
end

#object_idsObject



86
87
88
# File 'lib/plucky/criteria_hash.rb', line 86

def object_ids
  @options[:object_ids] ||= []
end

#object_ids=(value) ⇒ Object

Raises:

  • (ArgumentError)


90
91
92
93
# File 'lib/plucky/criteria_hash.rb', line 90

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.

Returns:

  • (Boolean)


98
99
100
101
# File 'lib/plucky/criteria_hash.rb', line 98

def simple?
  key_set = keys.to_set
  key_set == [:_id].to_set || key_set == [:_id, :_type].to_set
end

#to_hashObject



41
42
43
# File 'lib/plucky/criteria_hash.rb', line 41

def to_hash
  source
end