module HyperStore::DispatchReceiver

Attributes

params[RW]

Public Instance Methods

receives(*args) { |params| ... } click to toggle source
# File lib/hyper-store/dispatch_receiver.rb, line 7
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 Instance Methods

format_callback(args) click to toggle source
# File lib/hyper-store/dispatch_receiver.rb, line 29
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