Module: Cacheable
- Included in:
- Portal2Inventory, SteamGroup, SteamId, TF2BetaInventory, TF2Inventory
- 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
Sets the time this object has been fetched the last time.
-
#fetched? ⇒ Boolean
Returns whether the data for this object has already been fetched.
-
#initialize(fetch_now = true) ⇒ Object
Creates a new object and fetches the associated data of the object if desired.
Instance Attribute Details
#fetch_time ⇒ Time (readonly)
Returns the time the object’s data has been fetched the last time
90 91 92 |
# File 'lib/steam/community/cacheable.rb', line 90 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 ‘new` method of the including class will be aliased with `create`, 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.
23 24 25 26 27 28 29 30 31 |
# File 'lib/steam/community/cacheable.rb', line 23 def self.included(base) base.extend ClassMethods base.send :class_variable_set, :@@cache, {} base.send :class_variable_set, :@@cache_ids, [] class << base alias_method :create, :new end end |
Instance Method Details
#cache ⇒ Object
Saves this object in the cache
This will use the ID attributes selected for caching
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/steam/community/cacheable.rb', line 106 def cache cache = self.class.send :class_variable_get, :@@cache cache_ids = self.class.send :class_variable_get, :@@cache_ids cache_ids.each do |cache_id| cache_id_value = instance_variable_get('@' + cache_id.to_s) unless cache_id_value.nil? or cache.key?(cache_id_value) cache[cache_id_value] = self end 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. The overriding method should always call ‘super` to have the fetch time up-to-date.
Sets the time this object has been fetched the last time
126 127 128 |
# File 'lib/steam/community/cacheable.rb', line 126 def fetch @fetch_time = Time.now end |
#fetched? ⇒ Boolean
Returns whether the data for this object has already been fetched
133 134 135 |
# File 'lib/steam/community/cacheable.rb', line 133 def fetched? !@fetch_time.nil? end |
#initialize(fetch_now = true) ⇒ Object
The real constructor of cacheable classes is Cacheable::ClassMethods#new.
Creates a new object and fetches the associated data of the object if desired
98 99 100 101 |
# File 'lib/steam/community/cacheable.rb', line 98 def initialize(fetch_now = true) #:notnew: fetch if fetch_now cache end |