Class: Emque::Consuming::Router::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/emque/consuming/router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapping, &block) ⇒ Mapping

Returns a new instance of Mapping.



65
66
67
68
69
70
71
72
73
74
# File 'lib/emque/consuming/router.rb', line 65

def initialize(mapping, &block)
  self.topic = mapping.keys.first
  self.workers = mapping.fetch(:workers, 1)
  self.consumer = mapping.values.first
  self.mapping = {}
  self.middleware = []

  mapping.fetch(:middleware, []).map(&:use)
  self.instance_eval(&block)
end

Instance Attribute Details

#consumerObject

Returns the value of attribute consumer.



63
64
65
# File 'lib/emque/consuming/router.rb', line 63

def consumer
  @consumer
end

#middlewareObject

Returns the value of attribute middleware.



63
64
65
# File 'lib/emque/consuming/router.rb', line 63

def middleware
  @middleware
end

#topicObject

Returns the value of attribute topic.



63
64
65
# File 'lib/emque/consuming/router.rb', line 63

def topic
  @topic
end

#workersObject

Returns the value of attribute workers.



63
64
65
# File 'lib/emque/consuming/router.rb', line 63

def workers
  @workers
end

Instance Method Details

#map(map) ⇒ Object



76
77
78
# File 'lib/emque/consuming/router.rb', line 76

def map(map)
  mapping.merge!(map)
end

#middleware?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/emque/consuming/router.rb', line 80

def middleware?
  middleware.count > 0
end

#route(type) ⇒ Object



84
85
86
# File 'lib/emque/consuming/router.rb', line 84

def route(type)
  mapping[type]
end

#use(callable) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/emque/consuming/router.rb', line 88

def use(callable)
  unless callable.respond_to?(:call) and callable.arity == 1
    raise(
      ConfigurationError,
      "#{self.class.name}#use must receive a callable object with an " +
      "arity of one."
    )
  end

  middleware << callable
end