From b9528ad3c5379896b00772cb44faf1db0fd882d7 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 28 Jun 2008 00:55:02 -0700 Subject: initial statemachine machine and state classes --- .../lib/active_model/state_machine/state.rb | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 activemodel/lib/active_model/state_machine/state.rb (limited to 'activemodel/lib/active_model/state_machine/state.rb') 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 -- cgit v1.2.3