Module: StateFlow::Base::ClassMethods

Defined in:
lib/state_flow/base.rb

Instance Method Summary collapse

Instance Method Details

#process_state(selectable_attr, *keys, &block) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/state_flow/base.rb', line 10

def process_state(selectable_attr, *keys, &block)
  options = {
    :transactional => false, # :each, # :all
  }.update(keys.extract_options!)
  options[:transactional] = :each if options[:transactional] == true
  state_flow = state_flow_for(selectable_attr)
  raise ArgumentError, "state_flow not found: #{selectable_attr.inspect}" unless state_flow
  transaction_if_need(options[:transactional] == :all) do
    keys.each do |key|
      entry = state_flow[key]
      raise ArgumentError, "entry not found: #{key.inspect}" unless entry
      transaction_if_need(options[:transactional] == :each) do
        entry.process(&block)
      end
    end
  end
end

#transaction_if_need(with_transaction, &block) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/state_flow/base.rb', line 28

def transaction_if_need(with_transaction, &block)
  if with_transaction
    self.transaction(&block)
  else
    yield
  end
end