Class: Myxi::Exchange

Inherits:
Object
  • Object
show all
Defined in:
lib/myxi/exchange.rb

Constant Summary collapse

EXCHANGES =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exchange_name, model_name = nil, &block) ⇒ Exchange

Returns a new instance of Exchange.



20
21
22
23
24
# File 'lib/myxi/exchange.rb', line 20

def initialize(exchange_name, model_name = nil, &block)
  @exchange_name = exchange_name.to_sym
  @model_name = model_name
  @permission_block = block
end

Instance Attribute Details

#exchange_nameObject

Returns the value of attribute exchange_name.



6
7
8
# File 'lib/myxi/exchange.rb', line 6

def exchange_name
  @exchange_name
end

#key_typeObject

Returns the value of attribute key_type.



8
9
10
# File 'lib/myxi/exchange.rb', line 8

def key_type
  @key_type
end

#model_nameObject

Returns the value of attribute model_name.



7
8
9
# File 'lib/myxi/exchange.rb', line 7

def model_name
  @model_name
end

Class Method Details

.add(exchange_name, *args, &block) ⇒ Object



10
11
12
# File 'lib/myxi/exchange.rb', line 10

def self.add(exchange_name, *args, &block)
  EXCHANGES[exchange_name.to_sym] = self.new(exchange_name, *args, &block)
end

.declare_allObject



14
15
16
17
18
# File 'lib/myxi/exchange.rb', line 14

def self.declare_all
  EXCHANGES.keys.each do |exch|
    Myxi.channel.direct(exch.to_s)
  end
end

Instance Method Details

#can_subscribe?(routing_key, user) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/myxi/exchange.rb', line 42

def can_subscribe?(routing_key, user)
  if has_model?
    @permission_block.call(model_instance(routing_key), user)
  else
    @permission_block.call(routing_key, user)
  end
end

#has_model?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/myxi/exchange.rb', line 26

def has_model?
  !!@model_name
end

#modelObject



30
31
32
# File 'lib/myxi/exchange.rb', line 30

def model
  has_model? && model_name.constantize
end

#model_instance(id) ⇒ Object



38
39
40
# File 'lib/myxi/exchange.rb', line 38

def model_instance(id)
  has_model? ? model.where(key_type.to_sym => id.to_i).first : nil
end