From 29a31912fcedde9e271af8a20b890f8c62e1ea79 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 15 Nov 2008 21:05:03 -0800 Subject: Lazy-require state machine internals when the module is included --- activemodel/lib/active_model/state_machine.rb | 8 ++------ activemodel/lib/active_model/state_machine/event.rb | 2 ++ activemodel/lib/active_model/state_machine/machine.rb | 5 ++++- 3 files changed, 8 insertions(+), 7 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb index 96df6539ae..89afc4abb6 100644 --- a/activemodel/lib/active_model/state_machine.rb +++ b/activemodel/lib/active_model/state_machine.rb @@ -1,14 +1,10 @@ -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 class InvalidTransition < Exception end def self.included(base) + require 'active_model/state_machine/machine' base.extend ClassMethods end @@ -63,4 +59,4 @@ module ActiveModel end end end -end \ No newline at end of file +end diff --git a/activemodel/lib/active_model/state_machine/event.rb b/activemodel/lib/active_model/state_machine/event.rb index 8acde7fd47..3eb656b6d6 100644 --- a/activemodel/lib/active_model/state_machine/event.rb +++ b/activemodel/lib/active_model/state_machine/event.rb @@ -1,3 +1,5 @@ +require 'active_model/state_machine/state_transition' + module ActiveModel module StateMachine class Event diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb index 170505c0b2..58c2f1b200 100644 --- a/activemodel/lib/active_model/state_machine/machine.rb +++ b/activemodel/lib/active_model/state_machine/machine.rb @@ -1,3 +1,6 @@ +require 'active_model/state_machine/state' +require 'active_model/state_machine/event' + module ActiveModel module StateMachine class Machine @@ -71,4 +74,4 @@ module ActiveModel end end end -end \ No newline at end of file +end -- cgit v1.2.3 From bedea330432444ffcc003b54ae12c5ecc4e4b3eb Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 19 Nov 2008 11:07:19 -0800 Subject: Eliminate method redefinition warning --- activemodel/lib/active_model/state_machine/machine.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb index 58c2f1b200..a5ede021b1 100644 --- a/activemodel/lib/active_model/state_machine/machine.rb +++ b/activemodel/lib/active_model/state_machine/machine.rb @@ -4,7 +4,8 @@ require 'active_model/state_machine/event' module ActiveModel module StateMachine class Machine - attr_accessor :initial_state, :states, :events, :state_index + attr_writer :initial_state + attr_accessor :states, :events, :state_index attr_reader :klass, :name def initialize(klass, name, options = {}, &block) -- cgit v1.2.3 From 0e2d18e415118afed2df148e5d7302ef0361b569 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 22 Nov 2008 18:37:08 -0800 Subject: Extract state query method definition and quiet method redefinition warning. --- activemodel/lib/active_model/state_machine.rb | 6 ++++++ activemodel/lib/active_model/state_machine/state.rb | 7 ++----- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb index 89afc4abb6..bce90fd743 100644 --- a/activemodel/lib/active_model/state_machine.rb +++ b/activemodel/lib/active_model/state_machine.rb @@ -31,6 +31,12 @@ module ActiveModel state_machines[name] ||= Machine.new(self, name) block ? state_machines[name].update(options, &block) : state_machines[name] end + + def define_state_query_method(state_name) + name = "#{state_name}?" + undef_method(name) if method_defined?(name) + class_eval "def #{name}; current_state.to_s == %(#{state_name}) end" + end end def current_state(name = nil, new_state = nil, persist = false) diff --git a/activemodel/lib/active_model/state_machine/state.rb b/activemodel/lib/active_model/state_machine/state.rb index 68eb2aa34a..76916b1d86 100644 --- a/activemodel/lib/active_model/state_machine/state.rb +++ b/activemodel/lib/active_model/state_machine/state.rb @@ -5,11 +5,8 @@ module ActiveModel 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 + if machine = options.delete(:machine) + machine.klass.define_state_query_method(name) end update(options) end -- cgit v1.2.3