Sha256: d0ad7d8d32fb4fbe40647d7523bbaa15162cfaffaed13630a52ffdfadfd8c316

Contents?: true

Size: 903 Bytes

Versions: 12

Compression:

Stored size: 903 Bytes

Contents

module HyperStore
  module DispatchReceiver
    class InvalidOperationError < StandardError; end

    attr_accessor :params

    def receives(*args, &block)
      # Format the callback to be Proc or Nil
      callback = format_callback(args)

      if args.empty?
        message = 'At least one operation must be passed in to the \'receives\' macro'
        raise InvalidOperationError, message
      end

      # Loop through receivers and call callback and block on dispatch
      args.each do |operation|
        operation.on_dispatch do |params|
          @params = params

          callback.call if callback
          yield params if block
        end
      end
    end

    private

    def format_callback(args)
      if args.last.is_a?(Symbol)
        method_name = args.pop
        -> { send(:"#{method_name}") }
      elsif args.last.is_a?(Proc)
        args.pop
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
hyper-store-0.99.6 lib/hyper-store/dispatch_receiver.rb
hyper-store-0.99.5 lib/hyper-store/dispatch_receiver.rb
hyper-store-0.99.4 lib/hyper-store/dispatch_receiver.rb
hyper-store-0.99.3 lib/hyper-store/dispatch_receiver.rb
hyper-store-0.99.2 lib/hyper-store/dispatch_receiver.rb
hyper-store-0.99.1 lib/hyper-store/dispatch_receiver.rb
hyper-store-0.99.0 lib/hyper-store/dispatch_receiver.rb
hyper-store-1.0.0.lap28 lib/hyper-store/dispatch_receiver.rb
hyper-store-0.2.3 lib/hyper-store/dispatch_receiver.rb
hyper-store-0.2.2 lib/hyper-store/dispatch_receiver.rb
hyper-store-0.2.1 lib/hyper-store/dispatch_receiver.rb
hyper-store-0.2.0 lib/hyper-store/dispatch_receiver.rb