Module: SteamCondenser::Community::Cacheable

Included in:
GameInventory, GameItemSchema, GameStats, GameStatsSchema, SteamGame, SteamGroup, SteamId
Defined in:
lib/steam-condenser/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.

Author:

  • Sebastian Staudt

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fetch_timeTime (readonly)

Returns the time the object's data has been fetched the last time

Returns:

  • (Time)

    The time the object has been updated the last time


133
134
135
# File 'lib/steam-condenser/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.

Parameters:

  • base (Class)

    The class to extend with caching functionality

See Also:


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/steam-condenser/community/cacheable.rb', line 32

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

#cacheObject

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-condenser/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

#cache_id_value(id) ⇒ Object (private)

Returns the value for the ID

Parameters:

  • id (Symbol)

    The name of an instance variable

Returns:

  • (Object)

    The value of the given instance variable


195
196
197
# File 'lib/steam-condenser/community/cacheable.rb', line 195

def cache_id_value(id)
  instance_variable_get "@#{id}".to_sym
end

#cache_idsArray<Object, Array<Object>> (private)

Returns a complete list of all values for the cache IDs of the cachable object

Returns:

  • (Array<Object, Array<Object>>)

    The values for the cache IDs

See Also:


183
184
185
186
187
188
189
# File 'lib/steam-condenser/community/cacheable.rb', line 183

def cache_ids
  values = lambda do |id|
    id.is_a?(Array) ? id.map(&values) : cache_id_value(id)
  end

  self.class.send(:cache_ids).map &values
end

#cached_instanceObject (private)

If available, returns the cached instance for the object it is called on

This may be used to either replace an initial object with a completely cached instance of the same ID or to compare a modified object with the copy that was cached before.

See Also:


172
173
174
175
176
# File 'lib/steam-condenser/community/cacheable.rb', line 172

def cached_instance
  ids = cache_ids
  cached = self.class.send(:cache).find { |id, object| ids.include? id}
  cached.nil? ? nil : cached.last
end

#fetchObject

Note:

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-condenser/community/cacheable.rb', line 153

def fetch
end

#fetched?Boolean

Returns whether the data for this object has already been fetched

Returns:

  • (Boolean)

    true if this object's data is available


159
160
161
# File 'lib/steam-condenser/community/cacheable.rb', line 159

def fetched?
  !@fetch_time.nil?
end