Class: CaChing::Query::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/ca_ching/query/abstract.rb

Direct Known Subclasses

Calculation, Select

Constant Summary collapse

AND =

Unceremoniously taken from cache-money

/\s+AND\s+/i
OR =
/\s+OR\s+/i
TABLE_AND_COLUMN =

Matches: ‘users`.id, `users`.`id`, users.id, id

/(?:(?:`|")?(\w+)(?:`|")?\.)?(?:`|")?(\w+)(?:`|")?/
VALUE =

Matches: 123, ?, ‘123’, ‘12”3’

/'?(\d+|\?|(?:(?:[^']|'')*))'?/
KEY_CMP_VALUE =

Matches: KEY = VALUE, (KEY = VALUE)

/^\(?#{TABLE_AND_COLUMN}\s+(=|<|<=|>|>=)\s+#{VALUE}\)?$/
ORDER =

Matches: COLUMN ASC, COLUMN DESC, COLUMN

/^#{TABLE_AND_COLUMN}\s*(ASC|DESC)?$/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(active_record_collection, options = {}) ⇒ Abstract

Returns a new instance of Abstract.



14
15
16
17
18
19
20
# File 'lib/ca_ching/query/abstract.rb', line 14

def initialize(active_record_collection, options={})
  self.tap do
    @collection = active_record_collection
    @sql = active_record_collection.to_sql
    @klass = active_record_collection.klass
  end
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



12
13
14
# File 'lib/ca_ching/query/abstract.rb', line 12

def klass
  @klass
end

Instance Method Details

#calculation?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/ca_ching/query/abstract.rb', line 70

def calculation?
  false
end

#limitObject



62
63
64
# File 'lib/ca_ching/query/abstract.rb', line 62

def limit
  @collection.limit_value
end

#offsetObject



66
67
68
# File 'lib/ca_ching/query/abstract.rb', line 66

def offset
  @collection.offset_value
end

#orderObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/ca_ching/query/abstract.rb', line 51

def order
  order_values = @collection.order_values.map { |v| v.split(',').map { |v| v.strip } }.flatten
  
  order_values.inject({}) do |hash, value|
    match = ORDER.match(value).captures
    hash[match[1].to_sym] = match[2] == "ASC" ? :asc : :desc
    
    hash
  end
end

#primary_key?Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/ca_ching/query/abstract.rb', line 80

def primary_key?
  @where ||= where
  @where.keys.length == 1 && @where.keys.include?(@collection.primary_key.to_sym)
end

#table_nameObject



22
23
24
# File 'lib/ca_ching/query/abstract.rb', line 22

def table_name
  @collection.table_name
end

#to_keyObject

Formats the query to a key for the value store. Takes the form table_name:encode(field1, [operator1, value1])&encode(field2, [operator2, value2])…



76
77
78
# File 'lib/ca_ching/query/abstract.rb', line 76

def to_key
  "#{table_name}:#{where.map { |field, operator_and_value| encode(field, operator_and_value) } * "&" }"
end

#whereObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ca_ching/query/abstract.rb', line 26

def where
  raise UncacheableConditionError unless cacheable?
  
  @collection.where_values.inject({}) do |hash, value|
    if value.respond_to? :left
      left = value.left.name.to_sym
      right = value.right
      if right == '?'
        right = @collection.bind_values.find { |value| value.first.name == left.to_s }[1]
      end
      
      hash[left] = ['=', right]
    else
      value.split(AND).each do |value|
        match = KEY_CMP_VALUE.match(value).captures
        left, comparator, right = match[1].to_sym, match[2], match[3]
        
        hash[left] = [comparator, right]
      end
    end
    
    @where = hash
  end
end