Class: Kabutops::Extensions::CallbackSupport::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/kabutops/extensions/callback_support.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(allowed = []) ⇒ Manager

Returns a new instance of Manager.



13
14
15
16
# File 'lib/kabutops/extensions/callback_support.rb', line 13

def initialize allowed=[]
  @allowed = allowed
  @map ||= Hashie::Mash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



18
19
20
21
22
23
# File 'lib/kabutops/extensions/callback_support.rb', line 18

def method_missing name, *args, &block
  return super unless block_given? && allowed.include?(name)

  map[name] ||= []
  map[name] << block
end

Instance Attribute Details

#allowedObject (readonly)

Returns the value of attribute allowed.



11
12
13
# File 'lib/kabutops/extensions/callback_support.rb', line 11

def allowed
  @allowed
end

#mapObject (readonly)

Returns the value of attribute map.



11
12
13
# File 'lib/kabutops/extensions/callback_support.rb', line 11

def map
  @map
end

Instance Method Details

#notify(name, *args) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/kabutops/extensions/callback_support.rb', line 25

def notify name, *args
  raise "Not registered as valid callback: #{name}" unless allowed.include?(name)
  return unless map

  (map[name] || []).map do |block|
    block.call(*args)
  end
end