Module: Cacheable
- Included in:
- SteamGroup, SteamId, TF2Inventory
- Defined in:
- lib/steam/community/cacheable.rb
Overview
This module implements caching functionality to be used in any object using a fetch
method to fetch data, e.g. using a HTTP download.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#fetch_time ⇒ Object
readonly
Returns the value of attribute fetch_time.
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
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 SteamID has already been fetched.
-
#initialize(fetch_now = true) ⇒ Object
Creates a new object for the given
id
, either numeric or the custom URL specified by the user.
Instance Attribute Details
#fetch_time ⇒ Object (readonly)
Returns the value of attribute fetch_time.
57 58 59 |
# File 'lib/steam/community/cacheable.rb', line 57 def fetch_time @fetch_time end |
Class Method Details
.included(base) ⇒ Object
:nodoc:
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/steam/community/cacheable.rb', line 10 def self.included(base) #:nodoc: base.extend ClassMethods base.class_eval 'class_variable_set :@@cache, {}' base.class_eval 'class_variable_set :@@cache_ids, {}' class << base alias_method :create, :new end end |
Instance Method Details
#cache ⇒ Object
Saves this object in the cache
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/steam/community/cacheable.rb', line 69 def cache cache = self.class.class_eval 'class_variable_get :@@cache' cache_ids = self.class.class_eval '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
Sets the time this object has been fetched the last time
84 85 86 |
# File 'lib/steam/community/cacheable.rb', line 84 def fetch @fetch_time = Time.now end |
#fetched? ⇒ Boolean
Returns whether the data for this SteamID has already been fetched
89 90 91 |
# File 'lib/steam/community/cacheable.rb', line 89 def fetched? !@fetch_time.nil? end |
#initialize(fetch_now = true) ⇒ Object
Creates a new object for the given id
, either numeric or the custom URL specified by the user. If fetch_now
is true
(default), fetch is used to load data into the object. This method is overridden by Cacheable::ClassMethods#new.
63 64 65 66 |
# File 'lib/steam/community/cacheable.rb', line 63 def initialize(fetch_now = true) #:notnew: fetch if fetch_now cache end |