aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/state_machine
diff options
context:
space:
mode:
authorrick <technoweenie@gmail.com>2008-06-28 00:55:02 -0700
committerrick <technoweenie@gmail.com>2008-06-28 00:55:02 -0700
commitb9528ad3c5379896b00772cb44faf1db0fd882d7 (patch)
tree422ae423e519f744d51698d45a6fb931172b9cb2 /activemodel/lib/active_model/state_machine
parentb7c6ceff9a31cc478c4bc89d57980900a775fbed (diff)
downloadrails-b9528ad3c5379896b00772cb44faf1db0fd882d7.tar.gz
rails-b9528ad3c5379896b00772cb44faf1db0fd882d7.tar.bz2
rails-b9528ad3c5379896b00772cb44faf1db0fd882d7.zip
initial statemachine machine and state classes
Diffstat (limited to 'activemodel/lib/active_model/state_machine')
-rw-r--r--activemodel/lib/active_model/state_machine/machine.rb30
-rw-r--r--activemodel/lib/active_model/state_machine/state.rb40
2 files changed, 70 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
diff --git a/activemodel/lib/active_model/state_machine/state.rb b/activemodel/lib/active_model/state_machine/state.rb
new file mode 100644
index 0000000000..5851a6fb79
--- /dev/null
+++ b/activemodel/lib/active_model/state_machine/state.rb
@@ -0,0 +1,40 @@
+module ActiveModel
+ module StateMachine
+ class State
+ attr_reader :name, :options
+
+ def initialize(machine, name, options={})
+ @machine, @name, @options, @display_name = machine, name, options, options.delete(:display)
+ machine.klass.send(:define_method, "#{name}?") do
+ current_state.to_s == name.to_s
+ end
+ end
+
+ def ==(state)
+ if state.is_a? Symbol
+ name == state
+ else
+ name == state.name
+ end
+ end
+
+ def call_action(action, record)
+ action = @options[action]
+ case action
+ when Symbol, String
+ record.send(action)
+ when Proc
+ action.call(record)
+ end
+ end
+
+ def display_name
+ @display_name ||= name.to_s.gsub(/_/, ' ').capitalize
+ end
+
+ def for_select
+ [display_name, name.to_s]
+ end
+ end
+ end
+end