Module: Cacheable

Included in:
Portal2Inventory, 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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fetch_timeObject (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.send :class_variable_set, :@@cache, {}
  base.send :class_variable_set, :@@cache_ids, []

  class << base
    alias_method :create, :new
  end

end

Instance Method Details

#cacheObject

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.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

#fetchObject

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

Returns:

  • (Boolean)


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