Class: QueueBus::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/queue_bus/subscription.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue_name, key, class_name, filters, executor = nil) ⇒ Subscription

Returns a new instance of Subscription.



26
27
28
29
30
31
32
# File 'lib/queue_bus/subscription.rb', line 26

def initialize(queue_name, key, class_name, filters, executor=nil)
  @queue_name = self.class.normalize(queue_name)
  @key        = key.to_s
  @class_name = class_name.to_s
  @matcher    = Matcher.new(filters)
  @executor   = executor
end

Instance Attribute Details

#app_keyObject

dyanmically set on return from subscription_matches



24
25
26
# File 'lib/queue_bus/subscription.rb', line 24

def app_key
  @app_key
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



23
24
25
# File 'lib/queue_bus/subscription.rb', line 23

def class_name
  @class_name
end

#executorObject (readonly)

Returns the value of attribute executor.



23
24
25
# File 'lib/queue_bus/subscription.rb', line 23

def executor
  @executor
end

#keyObject (readonly)

Returns the value of attribute key.



23
24
25
# File 'lib/queue_bus/subscription.rb', line 23

def key
  @key
end

#matcherObject (readonly)

Returns the value of attribute matcher.



23
24
25
# File 'lib/queue_bus/subscription.rb', line 23

def matcher
  @matcher
end

#queue_nameObject (readonly)

Returns the value of attribute queue_name.



23
24
25
# File 'lib/queue_bus/subscription.rb', line 23

def queue_name
  @queue_name
end

Class Method Details

.from_redis(hash) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/queue_bus/subscription.rb', line 9

def from_redis(hash)
  queue_name = hash["queue_name"].to_s
  key        = hash["key"].to_s
  class_name = hash["class"].to_s
  matcher    = hash["matcher"]
  return nil if key.length == 0 || queue_name.length == 0
  Subscription.new(queue_name, key, class_name, matcher, nil)
end

.normalize(val) ⇒ Object



18
19
20
# File 'lib/queue_bus/subscription.rb', line 18

def normalize(val)
  val.to_s.gsub(/\W/, "_").downcase
end

.register(queue, key, class_name, matcher, block) ⇒ Object



5
6
7
# File 'lib/queue_bus/subscription.rb', line 5

def register(queue, key, class_name, matcher, block)
  Subscription.new(queue, key, class_name, matcher, block)
end

Instance Method Details

#execute!(attributes) ⇒ Object



34
35
36
37
38
39
# File 'lib/queue_bus/subscription.rb', line 34

def execute!(attributes)
  attributes = attributes.with_indifferent_access if attributes.respond_to?(:with_indifferent_access)
  ::QueueBus.with_global_attributes(attributes) do
    executor.call(attributes)
  end
end

#matches?(attributes) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/queue_bus/subscription.rb', line 41

def matches?(attributes)
  @matcher.matches?(attributes)
end

#to_redisObject



45
46
47
48
49
50
51
52
# File 'lib/queue_bus/subscription.rb', line 45

def to_redis
  out = {}
  out["queue_name"] = queue_name
  out["key"]        = key
  out["class"]      = class_name
  out["matcher"]    = matcher.to_redis
  out
end