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:
77 78 79 80 |
# File 'lib/invoicing/cached_record.rb', line 77 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.
88 89 90 91 92 93 94 |
# File 'lib/invoicing/cached_record.rb', line 88 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.
97 98 99 |
# File 'lib/invoicing/cached_record.rb', line 97 def find_some(ids, ) ids.map{|id| find_one(id, ) } end |
#list ⇒ Object
Returns a list of all objects in the cache.
102 103 104 |
# File 'lib/invoicing/cached_record.rb', line 102 def list @cache.values end |
#reload_cache ⇒ Object
82 83 84 85 |
# File 'lib/invoicing/cached_record.rb', line 82 def reload_cache @cache = {} model_class.find(:all).each {|obj| @cache[get(obj, :id)] = obj } end |