Class: Cash::Query::Abstract

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

Direct Known Subclasses

Calculation, PrimaryKey, Select

Constant Summary collapse

DESC =
/DESC/i

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(active_record, options1, options2) ⇒ Abstract

Returns a new instance of Abstract.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cash/query/abstract.rb', line 10

def initialize(active_record, options1, options2)
  @active_record, @options1, @options2 = active_record, options1, options2 || {}

  # if @options2.empty? and active_record.base_class != active_record
  #   @options2 = { :conditions => { active_record.inheritance_column => active_record.to_s }}
  # end
  # if active_record.base_class != active_record
  #   @options2[:conditions] = active_record.merge_conditions(
  #     @options2[:conditions], { active_record.inheritance_column => active_record.to_s }
  #   )
  # end
end

Class Method Details

.perform(*args) ⇒ Object



6
7
8
# File 'lib/cash/query/abstract.rb', line 6

def self.perform(*args)
  new(*args).perform
end

Instance Method Details

#calculation?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/cash/query/abstract.rb', line 58

def calculation?
  false
end

#limitObject



50
51
52
# File 'lib/cash/query/abstract.rb', line 50

def limit
  @limit ||= @options1[:limit] || @options2[:limit]
end

#offsetObject



54
55
56
# File 'lib/cash/query/abstract.rb', line 54

def offset
  @offset ||= @options1[:offset] || @options2[:offset] || 0
end

#orderObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cash/query/abstract.rb', line 37

def order
  @order ||= begin
    if order_sql = @options1[:order] || @options2[:order]
      matched, table_name, column_name, direction = *(ORDER.match(order_sql.to_s))
      [column_name, direction =~ DESC ? :desc : :asc]
    else
      ['id', :asc]
    end
  end
rescue TypeError
  ['id', :asc]
end

#perform(find_options = {}, get_options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cash/query/abstract.rb', line 23

def perform(find_options = {}, get_options = {})
  if cache_config = cacheable?(@options1, @options2, find_options)
    cache_keys, index = cache_keys(cache_config[0]), cache_config[1]

    misses, missed_keys, objects = hit_or_miss(cache_keys, index, get_options)
    format_results(cache_keys, choose_deserialized_objects_if_possible(missed_keys, cache_keys, misses, objects))
  else
    logger.debug("---- UNCACHEABLE #{table_name} - #{find_options.inspect} - #{get_options.inspect} - #{@options1.inspect} - #{@options2.inspect}") if logger
    uncacheable
  end
end