Class: Invoicing::CachedRecord::ClassInfo

Inherits:
Invoicing::ClassInfo::Base show all
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

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, options)
  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, options)
  ids.map{|id| find_one(id, options) }
end

#listObject

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_cacheObject


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