Sha256: 1bac6f26d7a31a7a485edcf09186f764c14a01d0999b148826087f2e7a523e80

Contents?: true

Size: 1.25 KB

Versions: 11

Compression:

Stored size: 1.25 KB

Contents

# @api private

module Wisper
  class ObjectRegistration < Registration
    attr_reader :with, :prefix, :allowed_classes, :broadcaster

    def initialize(listener, options)
      super(listener, options)
      @with   = options[:with]
      @prefix = ValueObjects::Prefix.new options[:prefix]
      @allowed_classes = Array(options[:scope]).map(&:to_s).to_set
      @broadcaster = map_broadcaster(options[:async] || options[:broadcaster])
    end

    def broadcast(event, publisher, *args)
      method_to_call = map_event_to_method(event)
      if should_broadcast?(event) && listener.respond_to?(method_to_call) && publisher_in_scope?(publisher)
        broadcaster.broadcast(listener, publisher, method_to_call, args)
      end
    end

    private

    def publisher_in_scope?(publisher)
      allowed_classes.empty? || publisher.class.ancestors.any? { |ancestor| allowed_classes.include?(ancestor.to_s) }
    end

    def map_event_to_method(event)
      prefix + (with || event).to_s
    end

    def map_broadcaster(value)
      return value if value.respond_to?(:broadcast)
      value = :async   if value == true
      value = :default if value == nil
      configuration.broadcasters.fetch(value)
    end

    def configuration
      Wisper.configuration
    end
  end
end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
lotrd-0.1.9 vendor/cache/ruby/2.6.0/gems/wisper-2.0.1/lib/wisper/registration/object.rb
lotrd-0.1.8 vendor/cache/ruby/2.6.0/gems/wisper-2.0.1/lib/wisper/registration/object.rb
lotrd-0.1.6 vendor/cache/ruby/2.6.0/gems/wisper-2.0.1/lib/wisper/registration/object.rb
lotrd-0.1.5 vendor/cache/ruby/2.6.0/gems/wisper-2.0.1/lib/wisper/registration/object.rb
pokedex-terminal-0.2.8 vendor/bundle/ruby/2.7.0/gems/wisper-2.0.1/lib/wisper/registration/object.rb
pokedex-terminal-0.2.7 vendor/bundle/ruby/2.7.0/gems/wisper-2.0.1/lib/wisper/registration/object.rb
pokedex-terminal-0.2.6 vendor/bundle/ruby/2.7.0/gems/wisper-2.0.1/lib/wisper/registration/object.rb
pokedex-terminal-0.2.5 vendor/bundle/ruby/2.7.0/gems/wisper-2.0.1/lib/wisper/registration/object.rb
pokedex-terminal-0.2.4 vendor/bundle/ruby/2.7.0/gems/wisper-2.0.1/lib/wisper/registration/object.rb
wisper-2.0.1 lib/wisper/registration/object.rb
wisper-2.0.0 lib/wisper/registration/object.rb