module BBFlow::Persistent
A bit of meta programming that allows to replace
def trello_key @trello_key ||= Config.global.transaction do |store| store['trello'] ||= {} store['trello']['key'] ||= Printer.ask('Go to https://trello.com/app-key and paste Key: ') end end
with
class Trello extend Persistent persistent global def key Printer.ask('Go to https://trello.com/app-key and paste Key: ') end end
The method with be memoized both within and across executions.
Public Instance Methods
persistent(args)
click to toggle source
@param [Array<Config, Symbol>] args
Calls superclass method
# 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