lib/simple_states.rb in simple_states-1.0.2 vs lib/simple_states.rb in simple_states-1.1.0.pre

- old
+ new

@@ -4,51 +4,59 @@ class TransitionException < RuntimeError; end autoload :Event, 'simple_states/event' autoload :States, 'simple_states/states' - def self.included(base) - base.extend SimpleStates::ClassMethods - # Add class level attribute accessors - added_class_accessors = [ :state_names, :initial_state, :events ] - base.singleton_class.send :attr_accessor, *added_class_accessors - base.public_class_method *added_class_accessors - base.public_class_method *added_class_accessors.map{|att| "#{att}=".to_sym } - # default states - base.after_initialize :init_state if base.respond_to?(:after_initialize) - base.initial_state = :created - base.events = [] + class << self + def included(base) + base.extend(SimpleStates::ClassMethods) + define_accessors(base, :state_names, :state_options, :initial_state, :events) + set_defaults(base) + end + + def define_accessors(base, *names) + base.singleton_class.send(:attr_accessor, *names) + base.public_class_method(*names + names.map { |name| "#{name}=".to_sym }) + end + + def set_defaults(base) + base.after_initialize(:init_state) if base.respond_to?(:after_initialize) + base.initial_state = :created + base.state_names = [] + base.state_options = {} + base.events = [] + end end module ClassMethods - def new(*) super.tap { |object| States.init(object) } end def allocate super.tap { |object| States.init(object) } end def states(*args) - if args.empty? - self.state_names ||= add_states(self.initial_state) - else - options = args.last.is_a?(Hash) ? args.pop : {} - self.initial_state = options[:initial].to_sym if options.key?(:initial) - add_states(*[self.initial_state].concat(args)) - end + options = args.last.is_a?(Hash) ? args.pop : {} + self.initial_state = options.delete(:initial).to_sym if options.key?(:initial) + self.state_options = options + add_states(*[self.initial_state].concat(args)) end - def add_states(*states) - self.state_names = (self.state_names || []).concat(states.compact.map(&:to_sym)).uniq + def add_states(*names) + self.state_names = self.state_names.concat(names.compact.map(&:to_sym)).uniq end def event(name, options = {}) add_states(options[:to], *options[:from]) self.events += [Event.new(name, options)] end + + def states_module + const_defined?(*args) ? self::StatesProxy : const_set(:StatesProxy, Module.new) + end end attr_reader :past_states def init_state @@ -64,15 +72,6 @@ end def was_state?(state) past_states.concat([self.state.try(:to_sym)]).compact.include?(state.to_sym) end - - def respond_to?(method, include_private = false) - method.to_s =~ /(was_|^)(#{self.class.states.join('|')})\?$/ && self.class.state_names.include?($2.to_sym) || super - end - - def method_missing(method, *args, &block) - method.to_s =~ /(was_|^)(#{self.class.states.join('|')})\?$/ ? send(:"#{$1}state?", $2, *args) : super - end end -