Class: Myxi::Exchange
- Inherits:
-
Object
- Object
- Myxi::Exchange
- Defined in:
- lib/myxi/exchange.rb
Constant Summary collapse
- EXCHANGES =
{}
Instance Attribute Summary collapse
-
#exchange_name ⇒ Object
Returns the value of attribute exchange_name.
-
#key_type ⇒ Object
Returns the value of attribute key_type.
-
#model_name ⇒ Object
Returns the value of attribute model_name.
Class Method Summary collapse
Instance Method Summary collapse
- #can_subscribe?(routing_key, user) ⇒ Boolean
- #has_model? ⇒ Boolean
-
#initialize(exchange_name, model_name = nil, &block) ⇒ Exchange
constructor
A new instance of Exchange.
- #model ⇒ Object
- #model_instance(id) ⇒ Object
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_name ⇒ Object
Returns the value of attribute exchange_name.
6 7 8 |
# File 'lib/myxi/exchange.rb', line 6 def exchange_name @exchange_name end |
#key_type ⇒ Object
Returns the value of attribute key_type.
8 9 10 |
# File 'lib/myxi/exchange.rb', line 8 def key_type @key_type end |
#model_name ⇒ Object
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 |
Instance Method Details
#can_subscribe?(routing_key, user) ⇒ 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
26 27 28 |
# File 'lib/myxi/exchange.rb', line 26 def has_model? !!@model_name end |
#model ⇒ Object
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 |