Module: Cacheable
- Included in:
- GameInventory, GameItemSchema, SteamGame, SteamGroup, SteamId
- Defined in:
- lib/steam/community/cacheable.rb
Overview
This module implements caching functionality to be used in any object class having one or more unique object identifier (i.e. ID) and using a ‘fetch` method to fetch data, e.g. using a HTTP download.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#fetch_time ⇒ Time
readonly
Returns the time the object’s data has been fetched the last time.
Class Method Summary collapse
-
.included(base) ⇒ Object
When this module is included in another class it is initialized to make use of caching.
Instance Method Summary collapse
-
#cache ⇒ Object
Saves this object in the cache.
-
#fetch ⇒ Object
Fetches the object from some data source.
-
#fetched? ⇒ Boolean
Returns whether the data for this object has already been fetched.
Instance Attribute Details
#fetch_time ⇒ Time (readonly)
Returns the time the object’s data has been fetched the last time
133 134 135 |
# File 'lib/steam/community/cacheable.rb', line 133 def fetch_time @fetch_time end |
Class Method Details
.included(base) ⇒ Object
When this module is included in another class it is initialized to make use of caching
The original ‘initialize` method of the including class will be wrapped, relaying all instantiations to the `new` method defined in ClassMethods. Additionally the class variable to save the attributes to cache (i.e. cache IDs) and the cache class variable itself are initialized.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/steam/community/cacheable.rb', line 30 def self.included(base) base.extend ClassMethods base.send :class_variable_set, :@@cache, {} base.send :class_variable_set, :@@cache_ids, [] class << base def method_added(name) if name == :fetch && !(@@in_method_added ||= false) @@in_method_added = true alias_method :original_fetch, :fetch define_method :fetch do original_fetch @fetch_time = Time.now end @@in_method_added = false end end end end |
Instance Method Details
#cache ⇒ Object
Saves this object in the cache
This will use the ID attributes selected for caching
138 139 140 141 142 143 144 145 |
# File 'lib/steam/community/cacheable.rb', line 138 def cache cache = self.class.send :cache cache_ids.each do |cache_id| cache[cache_id] = self if cache_id end true end |
#fetch ⇒ Object
This method should be overridden in cacheable object classes and should implement the logic to retrieve the object’s data. Updating the time is handled dynamically and does not need to be implemented separately.
Fetches the object from some data source
153 154 |
# File 'lib/steam/community/cacheable.rb', line 153 def fetch end |
#fetched? ⇒ Boolean
Returns whether the data for this object has already been fetched
159 160 161 |
# File 'lib/steam/community/cacheable.rb', line 159 def fetched? !@fetch_time.nil? end |