Class: StateFlow::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/state_flow/base.rb

Defined Under Namespace

Modules: ClientClassMethods, ClientInstanceMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, attr_name, attr_key_name) ⇒ Base

Returns a new instance of Base.



50
51
52
53
# File 'lib/state_flow/base.rb', line 50

def initialize(klass, attr_name, attr_key_name)
  @klass, @attr_name, @attr_key_name = klass, attr_name, attr_key_name
  @status_keys = klass.send(@attr_key_name.to_s.pluralize).map{|s| s.to_sym}
end

Instance Attribute Details

#attr_key_nameObject (readonly)

Returns the value of attribute attr_key_name.



49
50
51
# File 'lib/state_flow/base.rb', line 49

def attr_key_name
  @attr_key_name
end

#attr_nameObject (readonly)

Returns the value of attribute attr_name.



49
50
51
# File 'lib/state_flow/base.rb', line 49

def attr_name
  @attr_name
end

#klassObject (readonly)

Returns the value of attribute klass.



49
50
51
# File 'lib/state_flow/base.rb', line 49

def klass
  @klass
end

#status_keysObject (readonly)

Returns the value of attribute status_keys.



49
50
51
# File 'lib/state_flow/base.rb', line 49

def status_keys
  @status_keys
end

Instance Method Details

#all_statesObject



72
73
74
75
76
77
78
79
80
# File 'lib/state_flow/base.rb', line 72

def all_states
  unless @all_states
    @all_states = states.map{|state| state.descendants}.flatten.inject({}) do |dest, state|
      dest[state.name] = state
      dest
    end
  end
  @all_states
end

#concrete_statesObject



82
83
84
85
86
87
88
89
90
# File 'lib/state_flow/base.rb', line 82

def concrete_states
  unless @concrete_states
    @concrete_states = {}
    all_states.each do |name, state| 
      @concrete_states[state.name] = state if state.concrete?
    end
  end
  @concrete_states
end

#inspectObject



153
154
155
156
157
# File 'lib/state_flow/base.rb', line 153

def inspect
  result = "<#{self.class.name} @attr_name=#{@attr_name.inspect} @attr_key_name=#{@attr_key_name.inspect}"
  result << " @klass=\"#{@klass.name}\""
  result << '>'
end

#origin(value = nil) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/state_flow/base.rb', line 92

def origin(value = nil)
  if value
    @origin_name = value
  else
    @origin ||= all_states[@origin_name]
  end
end

#prepare_context(record, context_or_options = nil) ⇒ Object



100
101
102
103
104
# File 'lib/state_flow/base.rb', line 100

def prepare_context(record, context_or_options = nil)
  context_or_options.is_a?(StateFlow::Context) ?
    context_or_options :
    StateFlow::Context.new(self, record, context_or_options)
end

#process(context) ⇒ Object

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
# File 'lib/state_flow/base.rb', line 106

def process(context)
  current_key = context.current_attr_key
  raise ArgumentError, "current_key not found for: #{context.inspect}" unless current_key
  state = concrete_states[current_key]
  raise ArgumentError, "status not found for: #{current_key.inspect}" unless state
  state.process(context)
  context
end

#process_with_log(record, success_key, failure_key) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/state_flow/base.rb', line 115

def process_with_log(record, success_key, failure_key)
  origin_state = record.send(attr_name)
  origin_state_key = record.send(attr_key_name)
  begin
    yield(record) if block_given?
    # success_keyが指定されない場合、その変更はアクションとして指定されたメソッドに依存します。
    if success_key 
      record.send("#{attr_key_name}=", success_key)
      record.save!
    end
  rescue Exception => error
    log_attrs = {
      :target => record,
      :origin_state => origin_state,
      :origin_state_key => origin_state_key ? origin_state_key.to_s : nil,
      :dest_state => self.state_cd_by_key(success_key),
      :dest_state_key => success_key ? success_key.to_s : nil
    }
    StateFlow::Log.error(error, log_attrs)
    if failure_key
      retry_count = 0
      begin
        record.send("#{attr_key_name}=", failure_key)
        record.save!
      rescue Exception => fatal_error
        if retry_count == 0
          retry_count += 1
          record.attributes = record.class.find(record.id).attributes
          retry
        end
        StateFlow::Log.fatal(fatal_error, log_attrs)
        raise fatal_error
      end
    end
    raise error
  end
end

#state(name, &block) ⇒ Object Also known as: group, state_group



60
61
62
63
64
# File 'lib/state_flow/base.rb', line 60

def state(name, &block)
  result = State.new(self, name, &block)
  states << result
  result
end

#state_cd_by_key(key) ⇒ Object



55
56
57
58
# File 'lib/state_flow/base.rb', line 55

def state_cd_by_key(key)
  @state_cd_by_key_method_name ||= "#{klass.enum_base_name(attr_name)}_id_by_key"
  klass.send(@state_cd_by_key_method_name, key)
end

#statesObject



68
69
70
# File 'lib/state_flow/base.rb', line 68

def states
  @states ||= []
end