Class: Spree::Preferences::StoreInstance

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/preferences/store.rb

Direct Known Subclasses

Store

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStoreInstance

Returns a new instance of StoreInstance.



14
15
16
17
18
# File 'app/models/spree/preferences/store.rb', line 14

def initialize
  @cache = Rails.cache
  @persistence = true
  load_preferences
end

Instance Attribute Details

#persistenceObject

Returns the value of attribute persistence.



12
13
14
# File 'app/models/spree/preferences/store.rb', line 12

def persistence
  @persistence
end

Instance Method Details

#delete(key) ⇒ Object



51
52
53
54
# File 'app/models/spree/preferences/store.rb', line 51

def delete(key)
  @cache.delete(key)
  destroy(key)
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'app/models/spree/preferences/store.rb', line 25

def exist?(key)
  @cache.exist?(key) ||
  should_persist? && Spree::Preference.where(:key => key).exists?
end

#get(key) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/spree/preferences/store.rb', line 30

def get(key)
  # return the retrieved value, if it's in the cache
  if (val = @cache.read(key)).present?
    return val
  end

  return nil unless should_persist?

  # If it's not in the cache, maybe it's in the database, but
  # has been cleared from the cache

  # does it exist in the database?
  if preference = Spree::Preference.find_by_key(key)
    # it does exist, so let's put it back into the cache
    @cache.write(preference.key, preference.value)

    # and return the value
    preference.value
  end
end

#set(key, value, type) ⇒ Object



20
21
22
23
# File 'app/models/spree/preferences/store.rb', line 20

def set(key, value, type)
  @cache.write(key, value)
  persist(key, value, type)
end