Class: RecordCache::Scope
- Inherits:
-
Object
- Object
- RecordCache::Scope
- Defined in:
- lib/record_cache/scope.rb
Instance Attribute Summary collapse
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
- #conditions ⇒ Object
- #empty? ⇒ Boolean
- #fields ⇒ Object
-
#initialize(model_class, query) ⇒ Scope
constructor
A new instance of Scope.
- #match?(field, value) ⇒ Boolean
- #match_current?(model) ⇒ Boolean
- #match_previous?(model) ⇒ Boolean
Constructor Details
#initialize(model_class, query) ⇒ Scope
Returns a new instance of Scope.
5 6 7 8 |
# File 'lib/record_cache/scope.rb', line 5 def initialize(model_class, query) @model_class = model_class @query = query end |
Instance Attribute Details
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
3 4 5 |
# File 'lib/record_cache/scope.rb', line 3 def model_class @model_class end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
3 4 5 |
# File 'lib/record_cache/scope.rb', line 3 def query @query end |
Instance Method Details
#conditions ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/record_cache/scope.rb', line 41 def conditions @conditions ||= begin query.collect do |field, scope| if defined?(AntiObject) and scope.kind_of?(AntiObject) scope = ~scope invert = true end if scope.nil? op = invert ? 'IS NOT' : 'IS' "#{field} #{op} NULL" elsif scope.is_a?(Array) op = invert ? 'NOT IN' : 'IN' model_class.send(:sanitize_sql, ["#{field} #{op} (?)", scope]) else op = invert ? '!=' : '=' model_class.send(:sanitize_sql, ["#{field} #{op} ?", scope]) end end.join(' AND ') end @conditions end |
#empty? ⇒ Boolean
10 11 12 |
# File 'lib/record_cache/scope.rb', line 10 def empty? query.empty? end |
#fields ⇒ Object
14 15 16 |
# File 'lib/record_cache/scope.rb', line 14 def fields query.keys end |
#match?(field, value) ⇒ Boolean
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/record_cache/scope.rb', line 30 def match?(field, value) scope = query[field] if defined?(AntiObject) and scope.kind_of?(AntiObject) scope = ~scope invert = true end match = [*scope].include?(value) invert ? !match : match end |
#match_current?(model) ⇒ Boolean
18 19 20 21 22 |
# File 'lib/record_cache/scope.rb', line 18 def match_current?(model) fields.all? do |field| match?( field, model.send(field) ) end end |
#match_previous?(model) ⇒ Boolean
24 25 26 27 28 |
# File 'lib/record_cache/scope.rb', line 24 def match_previous?(model) fields.all? do |field| match?( field, model.attr_was(field) ) end end |