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 --- activemodel/lib/active_model/callbacks.rb | 2 +- activemodel/lib/active_model/state_machine.rb | 43 ++++++++++++++++++++++ .../lib/active_model/state_machine/machine.rb | 30 +++++++++++++++ .../lib/active_model/state_machine/state.rb | 40 ++++++++++++++++++++ activemodel/lib/active_model/validations.rb | 2 +- 5 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 activemodel/lib/active_model/state_machine.rb create mode 100644 activemodel/lib/active_model/state_machine/machine.rb create mode 100644 activemodel/lib/active_model/state_machine/state.rb (limited to 'activemodel/lib/active_model') diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb index 1dd20156ec..c94f76109f 100644 --- a/activemodel/lib/active_model/callbacks.rb +++ b/activemodel/lib/active_model/callbacks.rb @@ -1,4 +1,4 @@ -require 'active_model/observing' +require 'active_model/core' module ActiveModel module Callbacks diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb new file mode 100644 index 0000000000..bb038f6b7a --- /dev/null +++ b/activemodel/lib/active_model/state_machine.rb @@ -0,0 +1,43 @@ +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 \ No newline at end of file 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 diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 6b692e6a9a..7efe9901ca 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -1,4 +1,4 @@ -require 'active_model/observing' +require 'active_model/core' module ActiveModel module Validations -- cgit v1.2.3