Class: Msgr::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/msgr/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, opts = {}) ⇒ Route

Returns a new instance of Route.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/msgr/route.rb', line 6

def initialize(key, opts = {})
  @opts = opts
  raise ArgumentError.new 'Missing `to` options.' unless @opts[:to]

  add key

  if (match = /\A(?<consumer>\w+)#(?<action>\w+)\z/.match(opts[:to].strip.to_s))
    @consumer = "#{match[:consumer].camelize}Consumer"
    @action   = match[:action].underscore
  else
    raise ArgumentError.new "Invalid consumer format: #{opts[:to].strip.to_s.inspect}. Must be `consumer_class#action`."
  end
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



4
5
6
# File 'lib/msgr/route.rb', line 4

def action
  @action
end

#consumerObject (readonly)

Returns the value of attribute consumer.



4
5
6
# File 'lib/msgr/route.rb', line 4

def consumer
  @consumer
end

#optsObject (readonly)

Returns the value of attribute opts.



4
5
6
# File 'lib/msgr/route.rb', line 4

def opts
  @opts
end

Instance Method Details

#accept?(key, opts) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/msgr/route.rb', line 31

def accept?(key, opts)
  self.opts == opts
end

#add(key) ⇒ Object



25
26
27
28
29
# File 'lib/msgr/route.rb', line 25

def add(key)
  raise ArgumentError.new 'Routing key required.' unless key.present?

  keys << key
end

#keysObject Also known as: routing_keys



20
21
22
# File 'lib/msgr/route.rb', line 20

def keys
  @keys ||= []
end

#nameObject



35
36
37
# File 'lib/msgr/route.rb', line 35

def name
  "msgr.consumer.#{consumer}.#{action}"
end