Class: Synapse::Mapping::Mapping
- Inherits:
-
Object
- Object
- Synapse::Mapping::Mapping
- 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
-
#handler ⇒ Object
readonly
Either a method symbol or block.
-
#options ⇒ Hash
readonly
Options specific to the component being mapped.
-
#type ⇒ Class
readonly
The type of payload that a handler is being mapped to.
Instance Method Summary collapse
- #<=>(other) ⇒ Integer
- #==(other) ⇒ Boolean (also: #eql?)
-
#hash ⇒ Integer
TODO Is this a good hash function? Probs not.
- #initialize(type, options, handler) ⇒ undefined constructor
-
#invoke(target, *args) ⇒ Object
The result of the handler invocation.
Constructor Details
#initialize(type, options, handler) ⇒ undefined
21 22 23 24 25 |
# File 'lib/synapse/mapping/mapping.rb', line 21 def initialize(type, , handler) @type = type @options = @handler = handler end |
Instance Attribute Details
#handler ⇒ Object (readonly)
Returns Either a method symbol or block.
12 13 14 |
# File 'lib/synapse/mapping/mapping.rb', line 12 def handler @handler end |
#options ⇒ Hash (readonly)
Returns Options specific to the component being mapped.
15 16 17 |
# File 'lib/synapse/mapping/mapping.rb', line 15 def @options end |
#type ⇒ Class (readonly)
Returns 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
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 |
#hash ⇒ Integer
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
Returns 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 |