lib/emittance/action.rb in emittance-0.0.2 vs lib/emittance/action.rb in emittance-0.0.3

- old
+ new

@@ -120,73 +120,87 @@ # Name of the method that will be invoked when the handler class captures an event. HANDLER_METHOD_NAME = "handle_#{EMITTING_METHOD}".to_sym # @private class << self + include Emittance::Helpers::ConstantHelpers + def included(action_klass) handler_klass_name = Emittance::Action.handler_klass_name(action_klass) handler_klass = Emittance::Action.find_or_create_klass(handler_klass_name) - action_klass.class_eval do + setup_action_klass action_klass + setup_handler_klass handler_klass, action_klass + end + + # @private + def handler_klass_name(action_klass) + "#{action_klass}Handler" + end + + # @private + def emitting_event_identifier(action_klass) + Emittance::Event.event_klass_for action_klass + end + + # @private + def find_or_create_klass(klass_name) + unless Object.const_defined? klass_name + set_namespaced_constant_by_name klass_name, Class.new + end + + Object.const_get klass_name + end + + private + + # Class setups + + def setup_action_klass(action_klass) + action_klass.class_eval(&action_klass_blk) + end + + def setup_handler_klass(handler_klass, action_klass) + handler_klass.class_eval(&handler_klass_blk(action_klass)) + end + + # Blocks + + # rubocop:disable Metrics/MethodLength + def action_klass_blk + lambda do |_klass| extend Emittance::Emitter class << self - # @private - def method_added(method_name) + define_method :method_added do |method_name| emitting_method = Emittance::Action::EMITTING_METHOD - emits_on method_name if method_name == emitting_method - super + identifier = Emittance::Action.emitting_event_identifier(self) + emits_on(method_name, identifier: identifier) if method_name == emitting_method + super method_name end end end + end - handler_klass.class_eval do + def handler_klass_blk(action_klass) + lambda do |_klass| attr_reader :action extend Emittance::Watcher - def initialize(action_obj) + define_method :initialize do |action_obj| @action = action_obj end - watch Emittance::Action.emitting_event_name(action_klass) do |event| + watch Emittance::Action.emitting_event_identifier(action_klass) do |event| handler_obj = new(event.emitter) handler_method_name = Emittance::Action::HANDLER_METHOD_NAME if handler_obj.respond_to? handler_method_name handler_obj.send handler_method_name end end end - end - - # @private - def handler_klass_name(action_klass) - "#{action_klass}Handler" - end - - # @private - def emitting_event_name(action_klass) - Emittance::Emitter.emitting_method_event(action_klass, Emittance::Action::EMITTING_METHOD) - end - - # @private - def find_or_create_klass(klass_name) - unless Object.const_defined? klass_name - set_namespaced_constant_by_name klass_name, Class.new - end - - Object.const_get klass_name - end - - private - - def set_namespaced_constant_by_name(const_name, obj) - names = const_name.split('::') - names.shift if names.size > 1 && names.first.empty? - - namespace = names.size == 1 ? Object : Object.const_get(names[0...-1].join('::')) - namespace.const_set names.last, obj end end end end