Sha256: 82455e58109daa31c9c9a4e7b1f68c9614598dc8da480d50bb0b274bc9e1baf5
Contents?: true
Size: 1.4 KB
Versions: 6
Compression:
Stored size: 1.4 KB
Contents
module SimpleStates class States < Module class << self def init(object) object.singleton_class.send(:include, proxy_for(object.class)) object.init_state unless object.singleton_class.respond_to?(:after_initialize) end def proxy_for(klass) args = [:StatesProxy].concat(klass.method(:const_defined?).arity != 1 ? [false] : []) klass.const_defined?(*args) ? klass::StatesProxy : klass.const_set(:StatesProxy, 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| event.merge(merge, ix < merge_ix) end merge_events(events) else events end end end end
Version data entries
6 entries across 6 versions & 1 rubygems