Class: Msgr::Route

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

Constant Summary collapse

MATCH_REGEXP =
%r{\A(?<consumer>(?:\w+/)*\w+)#(?<action>\w+)\z}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Route.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/msgr/route.rb', line 8

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

  add key

  result = MATCH_REGEXP.match(opts[:to].strip.to_s)

  unless result
    raise ArgumentError.new \
      "Invalid consumer format: #{opts[:to].strip.to_s.inspect}. " \
      'Must be `name/space/consumer_class#action`.'
  end

  @consumer = "#{result[:consumer].camelize}Consumer"
  @action   = result[:action].underscore
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



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

def action
  @action
end

#consumerObject (readonly)

Returns the value of attribute consumer.



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

def consumer
  @consumer
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

Instance Method Details

#accept?(_key, opts) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/msgr/route.rb', line 41

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

#add(key) ⇒ Object



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

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

  keys << key
end

#keysObject Also known as: routing_keys



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

def keys
  @keys ||= []
end

#nameObject



45
46
47
# File 'lib/msgr/route.rb', line 45

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

#prefetchObject



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

def prefetch
  @opts[:prefetch] || 1
end