1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activemodel/lib/active_model/state_machine.rb

43 lines
1.1 KiB
Ruby
Raw Normal View History

Dir[File.dirname(__FILE__) + "/state_machine/*.rb"].sort.each do |path|
filename = File.basename(path)
require "active_model/state_machine/#{filename}"
end
module ActiveModel
module StateMachine
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def inherited(klass)
super
klass.state_machines = state_machines
end
def state_machines
@state_machines ||= {}
end
def state_machines=(value)
@state_machines = value ? value.dup : nil
end
def state_machine(name = nil, options = {}, &block)
if name.is_a?(Hash)
options = name
name = nil
end
name ||= :default
state_machines[name] ||= Machine.new(self, name)
block ? state_machines[name].update(options, &block) : state_machines[name]
end
end
def current_state(name = nil)
sm = self.class.state_machine(name)
ivar = "@#{sm.name}_current_state"
instance_variable_get(ivar) || instance_variable_set(ivar, sm.initial_state)
end
end
end