Sha256: 5aacfa59d5d42cdd7c73b155aa84517486a3b299927c78e92cb14c4bf75eecbc
Contents?: true
Size: 1.65 KB
Versions: 3
Compression:
Stored size: 1.65 KB
Contents
module SimpleStates class States < Module class << self def init(object) object.singleton_class.send(:include, proxy_for(object.class)) if object.singleton_class.respond_to?(:after_initialize) object.singleton_class.after_initialize do self.state = self.class.initial_state if attributes.keys.include?('state') && state.blank? end else object.state = object.class.initial_state end end def proxy_for(klass) args = [:States].concat(klass.method(:const_defined?).arity != 1 ? [false] : []) klass.const_defined?(*args) ? klass::States : klass.const_set(:States, new(klass.events)) end end attr_reader :events def initialize(events) @events = merge_events(events) setup end protected def setup events.each do |event| define_event(event) end end def define_event(event) define_method(event.name) do |*args| event.send(:call, self, *args) do super(*args) if self.class.method_defined?(event.name) end end define_method(:"#{event.name}!") do |*args| event.saving do send(event.name, *args) end end end def merge_events(events) if merge_ix = events.index { |event| event.name == :all } merge = events.slice!(merge_ix) events.each_with_index do |event, ix| # method = ? :append : :prepend event.merge(merge, ix < merge_ix) end merge_events(events) else events end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
simple_states-0.0.9 | lib/simple_states/states.rb |
simple_states-0.0.8 | lib/simple_states/states.rb |
simple_states-0.0.7 | lib/simple_states/states.rb |