Module: StateFlow::Base::ClientClassMethods

Defined in:
lib/state_flow/base.rb

Instance Method Summary collapse

Instance Method Details

#state_flow(selectable_attr, options = nil, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/state_flow/base.rb', line 20

def state_flow(selectable_attr, options = nil, &block)
  options = {
    :attr_key_name => "#{self.enum_base_name(selectable_attr)}_key".to_sym
  }.update(options || {})
  flow = Base.new(self, selectable_attr, options[:attr_key_name])
  flow.instance_eval(&block)
  @state_flows ||= []
  @state_flows << flow
  module_eval(<<-EOS, __FILE__, __LINE__)
    def process_#{selectable_attr}(context_or_options = nil)
      flow = self.class.state_flow_for(:#{flow.attr_name})
      context = flow.prepare_context(self, context_or_options)
      state_flow_contexts[:#{selectable_attr}] = context
      context.process(flow)
    end
  EOS
  NamedEvent.build_event_methods(flow)
  flow
end

#state_flow_for(selectable_attr) ⇒ Object



15
16
17
18
# File 'lib/state_flow/base.rb', line 15

def state_flow_for(selectable_attr)
  return nil unless @state_flows
  @state_flows.detect{|flow| flow.attr_name == selectable_attr}
end

#transaction_if_need(with_transaction, &block) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/state_flow/base.rb', line 40

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