class Synapse::Mapping::Mapping

Represents a mapping between a payload type and a handler method or block

Mappings are ordered by the depth of the payload type that they handle. Mappings that are for a more specific class are preferred over mappings for an abstract class.

Attributes

handler[R]

@return [Object] Either a method symbol or block

options[R]

@return [Hash] Options specific to the component being mapped

type[R]

@return [Class] The type of payload that a handler is being mapped to

Public Class Methods

new(type, options, handler) click to toggle source

@param [Class] type @param [Hash] options @param [Object] handler Either a method symbol or block @return [undefined]

# File lib/synapse/mapping/mapping.rb, line 21
def initialize(type, options, handler)
  @type = type
  @options = options
  @handler = handler
end

Public Instance Methods

<=>(other) click to toggle source

@param [Mapping] other @return [Integer]

# File lib/synapse/mapping/mapping.rb, line 46
def <=>(other)
  (@type <=> other.type) or 0
end
==(other) click to toggle source

@param [Mapping] other @return [Boolean]

# File lib/synapse/mapping/mapping.rb, line 52
def ==(other)
  self.class === other and
    @type == other.type
end
Also aliased as: eql?
eql?(other)
Alias for: ==
hash() click to toggle source

TODO Is this a good hash function? Probs not @return [Integer]

# File lib/synapse/mapping/mapping.rb, line 61
def hash
  @type.hash
end
invoke(target, *args) click to toggle source

@param [Object] target @param [Object…] args @return [Object] The result of the handler invocation

# File lib/synapse/mapping/mapping.rb, line 30
def invoke(target, *args)
  if @handler.is_a? Symbol
    method = target.method @handler

    if method.arity > args.size || method.arity == 0
      raise ArgumentError, 'Method signature is invalid'
    end

    method.call(*args.slice(0, method.arity))
  else
    target.instance_exec *args, &@handler
  end
end