From b7c6ceff9a31cc478c4bc89d57980900a775fbed Mon Sep 17 00:00:00 2001 From: rick Date: Fri, 27 Jun 2008 23:29:47 -0700 Subject: tweak activemodel load order a bit --- activemodel/lib/active_model.rb | 20 ++++---------------- activemodel/lib/active_model/base.rb | 4 ++++ activemodel/lib/active_model/callbacks.rb | 2 ++ activemodel/lib/active_model/core.rb | 7 +++++++ activemodel/lib/active_model/observing.rb | 18 ++++++++---------- activemodel/lib/active_model/validations.rb | 2 ++ 6 files changed, 27 insertions(+), 26 deletions(-) create mode 100644 activemodel/lib/active_model/core.rb (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 369c7fed33..4ed7b0889d 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -1,17 +1,5 @@ -$LOAD_PATH << File.join(File.dirname(__FILE__), '..', '..', 'activesupport', 'lib') - -# premature optimization? -require 'active_support/inflector' -require 'active_support/core_ext/string/inflections' -String.send :include, ActiveSupport::CoreExtensions::String::Inflections - -require 'active_model/base' require 'active_model/observing' -require 'active_model/callbacks' -require 'active_model/validations' - -ActiveModel::Base.class_eval do - include ActiveModel::Observing - include ActiveModel::Callbacks - include ActiveModel::Validations -end \ No newline at end of file +# disabled until they're tested +# require 'active_model/callbacks' +# require 'active_model/validations' +require 'active_model/base' \ No newline at end of file diff --git a/activemodel/lib/active_model/base.rb b/activemodel/lib/active_model/base.rb index 1141156da4..a500adfdf1 100644 --- a/activemodel/lib/active_model/base.rb +++ b/activemodel/lib/active_model/base.rb @@ -1,4 +1,8 @@ module ActiveModel class Base + include Observing + # disabled, until they're tested + # include Callbacks + # include Validations end end \ No newline at end of file diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb index 0114fc386b..1dd20156ec 100644 --- a/activemodel/lib/active_model/callbacks.rb +++ b/activemodel/lib/active_model/callbacks.rb @@ -1,3 +1,5 @@ +require 'active_model/observing' + module ActiveModel module Callbacks diff --git a/activemodel/lib/active_model/core.rb b/activemodel/lib/active_model/core.rb new file mode 100644 index 0000000000..47b968e121 --- /dev/null +++ b/activemodel/lib/active_model/core.rb @@ -0,0 +1,7 @@ +# This file is required by each major ActiveModel component for the core requirements. This allows you to +# load individual pieces of ActiveModel as needed. +$LOAD_PATH << File.join(File.dirname(__FILE__), '..', '..', '..', 'activesupport', 'lib') + +# premature optimization? +# So far, we only need the string inflections and not the rest of ActiveSupport. +require 'active_support/inflector' \ No newline at end of file diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb index db758f5185..9e99d7472c 100644 --- a/activemodel/lib/active_model/observing.rb +++ b/activemodel/lib/active_model/observing.rb @@ -1,4 +1,6 @@ require 'observer' +require 'singleton' +require 'active_model/core' module ActiveModel module Observing @@ -73,7 +75,7 @@ module ActiveModel # Start observing the declared classes and their subclasses. def initialize self.observed_classes = self.class.models if self.class.models - observed_classes.each { |klass| add_observer! klass } + observed_classes.each { |klass| klass.add_observer(self) } end # Send observed_method(object) if the method exists. @@ -85,16 +87,12 @@ module ActiveModel # Passes the new subclass. def observed_class_inherited(subclass) #:nodoc: self.class.observe(observed_classes + [subclass]) - add_observer!(subclass) + subclass.add_observer(self) end - protected - def observed_classes - @observed_classes ||= [self.class.observed_class] - end - - def add_observer!(klass) - klass.add_observer(self) - end + protected + def observed_classes + @observed_classes ||= [self.class.observed_class] + end end end \ No newline at end of file diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 34ef3b8f6e..6b692e6a9a 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -1,3 +1,5 @@ +require 'active_model/observing' + module ActiveModel module Validations def self.included(base) # :nodoc: -- cgit v1.2.3 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') 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 From 74cb05698684f237a7eb91afadec0020d8910c70 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 28 Jun 2008 09:19:44 -0700 Subject: add basic events and transitions. still more tests to convert --- activemodel/lib/active_model/state_machine.rb | 11 +++- .../lib/active_model/state_machine/event.rb | 67 ++++++++++++++++++++++ .../lib/active_model/state_machine/machine.rb | 44 ++++++++++---- .../lib/active_model/state_machine/state.rb | 18 ++++-- .../active_model/state_machine/state_transition.rb | 40 +++++++++++++ 5 files changed, 162 insertions(+), 18 deletions(-) create mode 100644 activemodel/lib/active_model/state_machine/event.rb create mode 100644 activemodel/lib/active_model/state_machine/state_transition.rb (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb index bb038f6b7a..2a5ac95a3e 100644 --- a/activemodel/lib/active_model/state_machine.rb +++ b/activemodel/lib/active_model/state_machine.rb @@ -5,6 +5,9 @@ end module ActiveModel module StateMachine + class InvalidTransition < Exception + end + def self.included(base) base.extend ClassMethods end @@ -34,10 +37,14 @@ module ActiveModel end end - def current_state(name = nil) + def current_state(name = nil, new_state = nil) sm = self.class.state_machine(name) ivar = "@#{sm.name}_current_state" - instance_variable_get(ivar) || instance_variable_set(ivar, sm.initial_state) + if name && new_state + instance_variable_set(ivar, new_state) + else + instance_variable_get(ivar) || instance_variable_set(ivar, sm.initial_state) + end end end end \ No newline at end of file diff --git a/activemodel/lib/active_model/state_machine/event.rb b/activemodel/lib/active_model/state_machine/event.rb new file mode 100644 index 0000000000..cc7d563214 --- /dev/null +++ b/activemodel/lib/active_model/state_machine/event.rb @@ -0,0 +1,67 @@ +module ActiveModel + module StateMachine + class Event + attr_reader :name, :success + + def initialize(name, options = {}, &block) + @name, @transitions = name, [] + machine = options.delete(:machine) + if machine + machine.klass.send(:define_method, "#{name.to_s}!") do |*args| + machine.fire_event(name, self, true, *args) + end + + machine.klass.send(:define_method, "#{name.to_s}") do |*args| + machine.fire_event(name, self, false, *args) + end + end + update(options, &block) + end + + def fire(obj, to_state = nil, *args) + transitions = @transitions.select { |t| t.from == obj.current_state } + raise InvalidTransition if transitions.size == 0 + + next_state = nil + transitions.each do |transition| + next if to_state && !Array(transition.to).include?(to_state) + if transition.perform(obj) + next_state = to_state || Array(transition.to).first + transition.execute(obj, *args) + break + end + end + next_state + end + + def transitions_from_state?(state) + @transitions.any? { |t| t.from? state } + end + + def success? + !!@success + end + + def ==(event) + if event.is_a? Symbol + name == event + else + name == event.name + end + end + + def update(options = {}, &block) + if options.key?(:success) then @success = options[:success] end + if block then instance_eval(&block) end + self + end + + private + def transitions(trans_opts) + Array(trans_opts[:from]).each do |s| + @transitions << StateTransition.new(trans_opts.merge({:from => s.to_sym})) + end + end + end + end +end diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb index 75ed8f8b65..1da48290c5 100644 --- a/activemodel/lib/active_model/state_machine/machine.rb +++ b/activemodel/lib/active_model/state_machine/machine.rb @@ -1,29 +1,49 @@ module ActiveModel module StateMachine class Machine - attr_accessor :initial_state, :states, :event + attr_accessor :initial_state, :states, :events, :state_index attr_reader :klass, :name - def initialize(klass, name) - @klass, @name, @states, @events = klass, name, [], {} + def initialize(klass, name, options = {}, &block) + @klass, @name, @states, @state_index, @events = klass, name, [], {}, {} + update(options, &block) + end + + def initial_state + @initial_state ||= (states.first ? states.first.name : nil) + end + + def update(options = {}, &block) + if options.key?(:initial) then @initial_state = options[:initial] end + if block then instance_eval(&block) end + self + end + + def fire_event(name, record, persist, *args) + state_index[record.current_state].call_action(:exit, record) + if new_state = @events[name].fire(record, *args) + state_index[new_state].call_action(:enter, record) + record.current_state(@name, new_state) + else + false + end 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) + def events_for(state) + events = @events.values.select { |event| event.transitions_from_state?(state) } + events.map! { |event| event.name } end - - def initial_state - @initial_state ||= (states.first ? states.first.name : nil) + private + def state(name, options = {}) + @states << (state_index[name] ||= State.new(name, :machine => self)).update(options) end - def update(options = {}, &block) - @initial_state = options[:initial] - instance_eval(&block) - self + def event(name, options = {}, &block) + (@events[name] ||= Event.new(name, :machine => self)).update(options, &block) end end end diff --git a/activemodel/lib/active_model/state_machine/state.rb b/activemodel/lib/active_model/state_machine/state.rb index 5851a6fb79..68eb2aa34a 100644 --- a/activemodel/lib/active_model/state_machine/state.rb +++ b/activemodel/lib/active_model/state_machine/state.rb @@ -3,11 +3,15 @@ module ActiveModel 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 + def initialize(name, options = {}) + @name = name + machine = options.delete(:machine) + if machine + machine.klass.send(:define_method, "#{name}?") do + current_state.to_s == name.to_s + end end + update(options) end def ==(state) @@ -35,6 +39,12 @@ module ActiveModel def for_select [display_name, name.to_s] end + + def update(options = {}) + if options.key?(:display) then @display_name = options.delete(:display) end + @options = options + self + end end end end diff --git a/activemodel/lib/active_model/state_machine/state_transition.rb b/activemodel/lib/active_model/state_machine/state_transition.rb new file mode 100644 index 0000000000..f9df998ea4 --- /dev/null +++ b/activemodel/lib/active_model/state_machine/state_transition.rb @@ -0,0 +1,40 @@ +module ActiveModel + module StateMachine + class StateTransition + attr_reader :from, :to, :options + + def initialize(opts) + @from, @to, @guard, @on_transition = opts[:from], opts[:to], opts[:guard], opts[:on_transition] + @options = opts + end + + def perform(obj) + case @guard + when Symbol, String + obj.send(@guard) + when Proc + @guard.call(obj) + else + true + end + end + + def execute(obj, *args) + case @on_transition + when Symbol, String + obj.send(@on_transition, *args) + when Proc + @on_transition.call(obj, *args) + end + end + + def ==(obj) + @from == obj.from && @to == obj.to + end + + def from?(value) + @from == value + end + end + end +end -- cgit v1.2.3 From a9d9ca16c739ec39a192d29c62f760e51040fc6e Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 28 Jun 2008 11:01:40 -0700 Subject: converted tests for more complex state transitions --- .../lib/active_model/state_machine/event.rb | 7 +++--- .../lib/active_model/state_machine/machine.rb | 26 ++++++++++++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/state_machine/event.rb b/activemodel/lib/active_model/state_machine/event.rb index cc7d563214..ea4df343de 100644 --- a/activemodel/lib/active_model/state_machine/event.rb +++ b/activemodel/lib/active_model/state_machine/event.rb @@ -3,9 +3,8 @@ module ActiveModel class Event attr_reader :name, :success - def initialize(name, options = {}, &block) - @name, @transitions = name, [] - machine = options.delete(:machine) + def initialize(machine, name, options = {}, &block) + @machine, @name, @transitions = machine, name, [] if machine machine.klass.send(:define_method, "#{name.to_s}!") do |*args| machine.fire_event(name, self, true, *args) @@ -19,7 +18,7 @@ module ActiveModel end def fire(obj, to_state = nil, *args) - transitions = @transitions.select { |t| t.from == obj.current_state } + transitions = @transitions.select { |t| t.from == obj.current_state(@machine ? @machine.name : nil) } raise InvalidTransition if transitions.size == 0 next_state = nil diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb index 1da48290c5..53ce71794f 100644 --- a/activemodel/lib/active_model/state_machine/machine.rb +++ b/activemodel/lib/active_model/state_machine/machine.rb @@ -19,12 +19,21 @@ module ActiveModel self end - def fire_event(name, record, persist, *args) - state_index[record.current_state].call_action(:exit, record) - if new_state = @events[name].fire(record, *args) + def fire_event(event, record, persist, *args) + state_index[record.current_state(@name)].call_action(:exit, record) + if new_state = @events[event].fire(record, *args) state_index[new_state].call_action(:enter, record) + + if record.respond_to?(event_fired_callback) + record.send(event_fired_callback, record.current_state, new_state) + end + record.current_state(@name, new_state) else + if record.respond_to?(event_failed_callback) + record.send(event_failed_callback, event) + end + false end end @@ -37,13 +46,22 @@ module ActiveModel events = @events.values.select { |event| event.transitions_from_state?(state) } events.map! { |event| event.name } end + private def state(name, options = {}) @states << (state_index[name] ||= State.new(name, :machine => self)).update(options) end def event(name, options = {}, &block) - (@events[name] ||= Event.new(name, :machine => self)).update(options, &block) + (@events[name] ||= Event.new(self, name)).update(options, &block) + end + + def event_fired_callback + @event_fired_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_fired' + end + + def event_failed_callback + @event_failed_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_failed' end end end -- cgit v1.2.3 From c9e366e997c6f3a383cfaa6351fa847e92de7fe4 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 28 Jun 2008 11:33:50 -0700 Subject: all aasm tests without activerecord moved over and passing --- activemodel/lib/active_model/state_machine.rb | 22 +++++++++++++++++++--- .../lib/active_model/state_machine/event.rb | 4 ---- .../lib/active_model/state_machine/machine.rb | 8 +++++++- 3 files changed, 26 insertions(+), 8 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb index 2a5ac95a3e..96df6539ae 100644 --- a/activemodel/lib/active_model/state_machine.rb +++ b/activemodel/lib/active_model/state_machine.rb @@ -37,13 +37,29 @@ module ActiveModel end end - def current_state(name = nil, new_state = nil) + def current_state(name = nil, new_state = nil, persist = false) sm = self.class.state_machine(name) - ivar = "@#{sm.name}_current_state" + ivar = sm.current_state_variable if name && new_state + if persist && respond_to?(:write_state) + write_state(sm, new_state) + end + + if respond_to?(:write_state_without_persistence) + write_state_without_persistence(sm, new_state) + end + instance_variable_set(ivar, new_state) else - instance_variable_get(ivar) || instance_variable_set(ivar, sm.initial_state) + instance_variable_set(ivar, nil) unless instance_variable_defined?(ivar) + value = instance_variable_get(ivar) + return value if value + + if respond_to?(:read_state) + value = instance_variable_set(ivar, read_state(sm)) + end + + value || sm.initial_state end end end diff --git a/activemodel/lib/active_model/state_machine/event.rb b/activemodel/lib/active_model/state_machine/event.rb index ea4df343de..e8bc8ebdb7 100644 --- a/activemodel/lib/active_model/state_machine/event.rb +++ b/activemodel/lib/active_model/state_machine/event.rb @@ -37,10 +37,6 @@ module ActiveModel @transitions.any? { |t| t.from? state } end - def success? - !!@success - end - def ==(event) if event.is_a? Symbol name == event diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb index 53ce71794f..170505c0b2 100644 --- a/activemodel/lib/active_model/state_machine/machine.rb +++ b/activemodel/lib/active_model/state_machine/machine.rb @@ -28,7 +28,9 @@ module ActiveModel record.send(event_fired_callback, record.current_state, new_state) end - record.current_state(@name, new_state) + record.current_state(@name, new_state, persist) + record.send(@events[event].success) if @events[event].success + true else if record.respond_to?(event_failed_callback) record.send(event_failed_callback, event) @@ -47,6 +49,10 @@ module ActiveModel events.map! { |event| event.name } end + def current_state_variable + "@#{@name}_current_state" + end + private def state(name, options = {}) @states << (state_index[name] ||= State.new(name, :machine => self)).update(options) -- cgit v1.2.3