Module: Kasket::WriteMixin::InstanceMethods

Defined in:
lib/kasket/write_mixin.rb

Instance Method Summary collapse

Instance Method Details

#clear_kasket_indicesObject



60
61
62
63
64
# File 'lib/kasket/write_mixin.rb', line 60

def clear_kasket_indices
  kasket_keys.each do |key|
    Kasket.cache.delete(key)
  end
end

#default_kasket_cacheable?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/kasket/write_mixin.rb', line 29

def default_kasket_cacheable?
  true
end

#kasket_keyObject



25
26
27
# File 'lib/kasket/write_mixin.rb', line 25

def kasket_key
  @kasket_key ||= new_record? ? nil : self.class.kasket_key_for_id(id)
end

#kasket_keysObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kasket/write_mixin.rb', line 39

def kasket_keys
  attribute_sets = [attributes.symbolize_keys]

  if changed?
    old_attributes = Hash[*changes.map {|attribute, values| [attribute, values[0]]}.flatten].symbolize_keys
    attribute_sets << old_attributes.reverse_merge(attribute_sets[0])
  end

  keys = []
  self.class.kasket_indices.each do |index|
    keys += attribute_sets.map do |attribute_set|
      key = self.class.kasket_key_for(index.map { |attribute| [attribute, attribute_set[attribute]]})
      index.include?(:id) ? key : [key, key + '/first']
    end
  end

  keys.flatten!
  keys.uniq!
  keys
end

#reload_with_kasket_clearing(*args) ⇒ Object



66
67
68
69
# File 'lib/kasket/write_mixin.rb', line 66

def reload_with_kasket_clearing(*args)
  Kasket.clear_local
  reload_without_kasket_clearing(*args)
end

#store_in_kasketObject



33
34
35
36
37
# File 'lib/kasket/write_mixin.rb', line 33

def store_in_kasket
  if kasket_cacheable? && kasket_key
    Kasket.cache.write(kasket_key, @attributes.dup)
  end
end