aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/state_machine/machine.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel/lib/active_model/state_machine/machine.rb')
-rw-r--r--activemodel/lib/active_model/state_machine/machine.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb
new file mode 100644
index 0000000000..75ed8f8b65
--- /dev/null
+++ b/activemodel/lib/active_model/state_machine/machine.rb
@@ -0,0 +1,30 @@
+module ActiveModel
+ module StateMachine
+ class Machine
+ attr_accessor :initial_state, :states, :event
+ attr_reader :klass, :name
+
+ def initialize(klass, name)
+ @klass, @name, @states, @events = klass, name, [], {}
+ end
+
+ def states_for_select
+ states.map { |st| [st.display_name, st.name.to_s] }
+ end
+
+ def state(name, options = {})
+ @states << State.new(self, name, options)
+ end
+
+ def initial_state
+ @initial_state ||= (states.first ? states.first.name : nil)
+ end
+
+ def update(options = {}, &block)
+ @initial_state = options[:initial]
+ instance_eval(&block)
+ self
+ end
+ end
+ end
+end \ No newline at end of file