Method: BBFlow::Persistent#persistent

Defined in:
lib/bb_flow/persistent.rb

#persistent(args) ⇒ Object

Parameters:

  • args (Array<Config, Symbol>)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bb_flow/persistent.rb', line 26

def persistent(args)
  store, method_name = args

  module_with_persistent_method = Module.new do
    define_method(method_name) do
      prefix = name.gsub(/^.*::/, '').downcase
      instance_variable_name = "@#{method_name}_memo"

      accessor = proc do |hash|
        hash[prefix] ||= {}
        hash[prefix][method_name.to_s] ||= super()
      end

      instance_variable_get(instance_variable_name) ||
      instance_variable_set(instance_variable_name, (store.instance_variable_get('@lock').locked? ? accessor.call(store) : store.transaction(&accessor)))
    end
  end

  prepend module_with_persistent_method
end