Sha256: ad5ff81ec81e26de6e2eae673b6d1ca83f20daf7b324baf88cfc7e79b7a6538c

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

module Aggro
  # Public: Adds a DSL creating domain event bindings.
  module BindingDSL
    def bind(ref, filters: default_filters, to: nil, &block)
      if to
        bindings << Aggro.event_bus.subscribe(ref.id, self, to, filters)
      else
        bind_block ref, filters, &block
      end
    end

    private

    def bind_block(ref, filters, namespace = generate_namespace, &block)
      new_methods = BlockHelper.method_definitions(&block)
      event_methods[namespace] = Set.new(new_methods)

      class_eval(&block)
      move_methods_to_namespace(new_methods, namespace)

      bindings << Aggro.event_bus.subscribe(ref.id, self, namespace, filters)
    end

    def bindings
      @bindings ||= []
    end

    def default_filters
      {}
    end

    def generate_namespace
      [*('a'..'z')].sample(8).join
    end

    def move_methods_to_namespace(method_list, namespace)
      class_eval do
        method_list.each do |method|
          alias_method "#{namespace}_#{method}", method
          remove_method method
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aggro-0.0.4 lib/aggro/binding_dsl.rb
aggro-0.0.3 lib/aggro/binding_dsl.rb
aggro-0.0.2 lib/aggro/binding_dsl.rb