Class: AridCache::CacheProxy::Options

Inherits:
Hash
  • Object
show all
Defined in:
lib/arid_cache/cache_proxy/options.rb

Overview

A class representing a hash of options with methods to return subsets of those options.

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Options

Returns a new instance of Options.



6
7
8
# File 'lib/arid_cache/cache_proxy/options.rb', line 6

def initialize(opts={})
  self.merge!(opts)
end

Instance Method Details

#count_only?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/arid_cache/cache_proxy/options.rb', line 66

def count_only?
  !!self[:count_only]
end

#deprecated_raw?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/arid_cache/cache_proxy/options.rb', line 87

def deprecated_raw?
  !!(raw? && !AridCache.raw_with_options)
end

#force?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/arid_cache/cache_proxy/options.rb', line 54

def force?
  !!self[:force]
end

#opts_for_cacheObject



41
42
43
# File 'lib/arid_cache/cache_proxy/options.rb', line 41

def opts_for_cache
  reject { |k,v| !OPTIONS_FOR_CACHE.include?(k) }
end

#opts_for_cache_keyObject



45
46
47
# File 'lib/arid_cache/cache_proxy/options.rb', line 45

def opts_for_cache_key
  reject { |k,v| !OPTIONS_FOR_CACHE_KEY.include?(k) }
end

#opts_for_cache_proxyObject

Returns options that affect the cache proxy result



50
51
52
# File 'lib/arid_cache/cache_proxy/options.rb', line 50

def opts_for_cache_proxy
  reject { |k,v| !OPTIONS_FOR_CACHE_PROXY.include?(k) }
end

#opts_for_find(ids) ⇒ Object

Return options suitable to pass to ActiveRecord::Base#find. Preserve the original order of the results if no :order option is specified. If an offset is specified but no limit, ActiveRecord will not apply the offset, so pass in a limit that is as big as ids.size



34
35
36
37
38
39
# File 'lib/arid_cache/cache_proxy/options.rb', line 34

def opts_for_find(ids)
  find_opts = reject { |k,v| !OPTIONS_FOR_FIND.include?(k) }
  find_opts[:order] = AridCache.order_by(ids, self[:result_klass]) unless find_opts.include?(:order)
  find_opts[:limit] = ids.size unless find_opts.include?(:limit)
  find_opts
end

#opts_for_paginate(records = nil) ⇒ Object

Filter options for paginate. Set total_entries to records.size if records is supplied Get the :per_page value from the result_klass, or receiver_klass if its set and responds to per_page. Otherwise default to 30 results per page.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/arid_cache/cache_proxy/options.rb', line 14

def opts_for_paginate(records=nil)
  paginate_opts = reject { |k,v| !OPTIONS_FOR_PAGINATE.include?(k) }
  paginate_opts[:finder] = :find_all_by_id unless paginate_opts.include?(:finder)
  unless paginate_opts.key?(:per_page)
    klass = values_at(:result_klass, :receiver_klass).find do |klass|
      klass.respond_to?(:per_page)
    end
    paginate_opts[:per_page] = klass && klass.per_page || 30
  end
  paginate_opts[:page] = 1 if paginate_opts[:page].nil?
  paginate_opts[:total_entries] = records.size unless records.nil?
  paginate_opts
end

#order_by_key?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/arid_cache/cache_proxy/options.rb', line 74

def order_by_key?
  include?(:order) && (self[:order].is_a?(Symbol) || self[:order].is_a?(String))
end

#order_by_proc?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/arid_cache/cache_proxy/options.rb', line 70

def order_by_proc?
  include?(:order) && self[:order].is_a?(Proc)
end

#paginate?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/arid_cache/cache_proxy/options.rb', line 58

def paginate?
  include?(:page)
end

#proxy(direction) ⇒ Object

Return the user’s proxy method for the given direction. Returns a symbol, Proc or nil if no proxy is defined.

  • direction - :in or :out, depending on whether we are putting results into

    the cache, or returning results from the cache, respectively
    


108
109
110
# File 'lib/arid_cache/cache_proxy/options.rb', line 108

def proxy(direction)
  self[:proxy] || self["proxy_#{direction}".to_sym]
end

#proxy?(direction) ⇒ Boolean

Return true if the user has defined a proxy for results processing in the given direction.

  • direction - :in or :out, depending on whether we are putting results into

    the cache, or returning results from the cache, respectively
    

Returns:

  • (Boolean)


83
84
85
# File 'lib/arid_cache/cache_proxy/options.rb', line 83

def proxy?(direction)
  include?(:proxy) || include?("proxy_#{direction}".to_sym)
end

#raw?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/arid_cache/cache_proxy/options.rb', line 62

def raw?
  !!self[:raw]
end

#receiver_klassObject

Returns the class of the receiver object or raises IndexError if the receiver klass has not been set.



93
94
95
# File 'lib/arid_cache/cache_proxy/options.rb', line 93

def receiver_klass
  fetch :receiver_klass
end

#result_klassObject

Returns the class of the result records or raises IndexError if the result klass has not been set.



99
100
101
# File 'lib/arid_cache/cache_proxy/options.rb', line 99

def result_klass
  fetch :result_klass
end