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



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)



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

def handler
  @handler
end

#optionsHash (readonly)



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

def options
  @options
end

#typeClass (readonly)



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

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ 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?



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



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

def hash
  @type.hash
end

#invoke(target, *args) ⇒ Object



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