Class: TinyCache::Arel::Wheres

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_cache/arel/wheres.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(where_values) ⇒ Wheres

Returns a new instance of Wheres.



7
8
9
# File 'lib/tiny_cache/arel/wheres.rb', line 7

def initialize(where_values)
  @where_values = where_values
end

Instance Attribute Details

#where_valuesObject (readonly)

Returns the value of attribute where_values.



5
6
7
# File 'lib/tiny_cache/arel/wheres.rb', line 5

def where_values
  @where_values
end

Instance Method Details

#all_equality?Boolean

Determine whether all conditions is equality, for example:

Article.where("user_id = 1").where(:status => 1).find(1)

Returns:

  • (Boolean)


14
15
16
# File 'lib/tiny_cache/arel/wheres.rb', line 14

def all_equality?
  where_values.all?{|where_value| where_value.is_a?(::Arel::Nodes::Equality)}
end

#extract_pairsObject

Extract conditions to pairs, for checking whether cache match the conditions.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tiny_cache/arel/wheres.rb', line 19

def extract_pairs
  where_values.map do |where_value|
    if where_value.is_a?(String)
      left, right = where_value.split(/\s*=\s*/, 2)
      right = right.to_i
    else
      left, right = where_value.left.name, where_value.right
    end

    {
      :left  => left,
      :right => right
    }
  end
end