module MinistryOfState
Constants
- VERSION
Public Instance Methods
check_state(state)
click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 139 def check_state(state) column = states[state].column method = new_record? ? column : "#{column}_was" send(method) == state.to_s end
check_transitions?(t_current_state, opts)
click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 192 def check_transitions?(t_current_state, opts) unless opts[:from].include?(t_current_state) raise InvalidState.new("Invalid from state '#{t_current_state}' for target state '#{opts[:to]}'") end end
current_state(column)
click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 135 def current_state(column) send("#{column}_was").to_sym end
fire(event)
click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 145 def fire(event) options = events[event].opts to_state = options[:to] column = events[event].column raise TransitionNotAllowed.new("Invalid 'to' state '#{to_state}'") unless states[to_state] check_guard = options[:guard] ? invoke_callback(options[:guard]) : true return unless check_guard before_event_callback = options[:before] after_event_callback = options[:after] enter = states[to_state].opts[:enter] after = states[to_state].opts[:after] exit = states[to_state].opts[:exit] invoke_callback(before_event_callback) if before_event_callback invoke_callback(enter) if enter begin transaction do self.lock! t_current_state = send( state_machine_column(column) ).try(:to_sym) check_transitions?(t_current_state, options) write_attribute( state_machine_column(column), to_state.to_s ) save end invoke_callback(exit) if exit invoke_callback(after) if after invoke_callback(after_event_callback) if after_event_callback self.errors.empty? rescue StandardError => e errors.add(:base, e.to_s) false end end
invoke_callback(callback)
click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 198 def invoke_callback(callback) callback.is_a?(Proc) ? callback.call(self) : send(callback) end
run_initial_state_actions()
click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 120 def run_initial_state_actions states.each do |name, state| next unless state.initial? begin after = state.opts[:after] invoke_callback(enter) if after true rescue StandardError => e errors.add(:base, e.to_s) false end end end
set_initial_states()
click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 98 def set_initial_states states.each do |name, state| next unless state.initial? begin enter = state.opts[:enter] invoke_callback(enter) if enter true rescue StandardError => e errors.add(:base, e.to_s) false end default_state = read_attribute( state_machine_column(state.column) ) write_attribute(state_machine_column(state.column), state.name) unless default_state end end
set_initial_states_for_new_record()
click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 116 def set_initial_states_for_new_record set_initial_states if new_record? end
state_machine_column(column)
click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 184 def state_machine_column(column) if(self.class.column_names.include?(column)) column else raise InvalidStateColumn.new("State column '#{column}' does not exist in table '#{self.class.table_name}'") end end