Class: Invoicing::CachedRecord::ClassInfo
- Inherits:
-
Invoicing::ClassInfo::Base
- Object
- Invoicing::ClassInfo::Base
- Invoicing::CachedRecord::ClassInfo
- Defined in:
- lib/invoicing/cached_record.rb
Overview
Stores state in the ActiveRecord class object, including the cache – a hash which maps ID to model object for all objects of this model object type
Instance Attribute Summary
Attributes inherited from Invoicing::ClassInfo::Base
#all_args, #all_options, #current_args, #current_options, #model_class, #new_args, #previous_info
Instance Method Summary collapse
-
#find_one(id, options) ⇒ Object
Returns one object from the cache, given its ID.
-
#find_some(ids, options) ⇒ Object
Returns a list of objects from the cache, given a list of IDs.
-
#initialize(model_class, previous_info, args) ⇒ ClassInfo
constructor
:nodoc:.
-
#list ⇒ Object
Returns a list of all objects in the cache.
- #reload_cache ⇒ Object
Methods inherited from Invoicing::ClassInfo::Base
#get, #method, #option_defaults, #set
Constructor Details
#initialize(model_class, previous_info, args) ⇒ ClassInfo
:nodoc:
80 81 82 83 |
# File 'lib/invoicing/cached_record.rb', line 80 def initialize(model_class, previous_info, args) super reload_cache end |
Instance Method Details
#find_one(id, options) ⇒ Object
Returns one object from the cache, given its ID.
91 92 93 94 95 96 97 |
# File 'lib/invoicing/cached_record.rb', line 91 def find_one(id, ) if result = @cache[id] result else raise ::ActiveRecord::RecordNotFound, "Couldn't find #{model_class.name} with ID=#{id}" end end |
#find_some(ids, options) ⇒ Object
Returns a list of objects from the cache, given a list of IDs.
100 101 102 |
# File 'lib/invoicing/cached_record.rb', line 100 def find_some(ids, ) ids.map{|id| find_one(id, ) } end |
#list ⇒ Object
Returns a list of all objects in the cache.
105 106 107 |
# File 'lib/invoicing/cached_record.rb', line 105 def list @cache.values end |
#reload_cache ⇒ Object
85 86 87 88 |
# File 'lib/invoicing/cached_record.rb', line 85 def reload_cache @cache = {} model_class.all.each {|obj| @cache[get(obj, :id)] = obj } end |