Class: Synapse::Mapping::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse/mapping/mapping.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, options, handler) ⇒ undefined

Parameters:

  • type (Class)
  • options (Hash)
  • handler (Object)

    Either a method symbol or block



21
22
23
24
25
# File 'lib/synapse/mapping/mapping.rb', line 21

def initialize(type, options, handler)
  @type = type
  @options = options
  @handler = handler
end

Instance Attribute Details

#handlerObject (readonly)

Returns Either a method symbol or block.

Returns:

  • (Object)

    Either a method symbol or block



12
13
14
# File 'lib/synapse/mapping/mapping.rb', line 12

def handler
  @handler
end

#optionsHash (readonly)

Returns Options specific to the component being mapped.

Returns:

  • (Hash)

    Options specific to the component being mapped



15
16
17
# File 'lib/synapse/mapping/mapping.rb', line 15

def options
  @options
end

#typeClass (readonly)

Returns The type of payload that a handler is being mapped to.

Returns:

  • (Class)

    The type of payload that a handler is being mapped to



9
10
11
# File 'lib/synapse/mapping/mapping.rb', line 9

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Integer

Parameters:

Returns:

  • (Integer)


46
47
48
# File 'lib/synapse/mapping/mapping.rb', line 46

def <=>(other)
  (@type <=> other.type) or 0
end

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/synapse/mapping/mapping.rb', line 52

def ==(other)
  self.class === other and
    @type == other.type
end

#hashInteger

TODO Is this a good hash function? Probs not

Returns:

  • (Integer)


61
62
63
# File 'lib/synapse/mapping/mapping.rb', line 61

def hash
  @type.hash
end

#invoke(target, *args) ⇒ Object

Returns The result of the handler invocation.

Parameters:

  • target (Object)
  • args (Object...)

Returns:

  • (Object)

    The result of the handler invocation



30
31
32
33
34
35
36
37
38
39
40
41
42
# 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