Class: Weebl::Fallible

Inherits:
Object
  • Object
show all
Defined in:
lib/weebl/fallible.rb

Direct Known Subclasses

Mongo

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Fallible



4
5
6
7
8
# File 'lib/weebl/fallible.rb', line 4

def initialize(options)
  @options   = options
  @strategy  = Strategy[@options[:retry]].new(self)
  @listeners = Hash.new { |h,k| h[k] = [] }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key) ⇒ Object (private)



30
31
32
33
# File 'lib/weebl/fallible.rb', line 30

def method_missing(key)
  key = [key.to_s, key.to_sym].find(&@options.method(:has_key?))
  key && @options[key]
end

Instance Method Details

#on(event_type, &block) ⇒ Object



17
18
19
# File 'lib/weebl/fallible.rb', line 17

def on(event_type, &block)
  @listeners[event_type] << block
end

#on_failObject



14
15
# File 'lib/weebl/fallible.rb', line 14

def on_fail
end

#trigger(event_type, data) ⇒ Object



21
22
23
24
25
26
# File 'lib/weebl/fallible.rb', line 21

def trigger(event_type, data)
  return unless @listeners.has_key?(event_type)
  @listeners[event_type].each do |listener|
    listener.call(data)
  end
end

#with_connection(&task) ⇒ Object



10
11
12
# File 'lib/weebl/fallible.rb', line 10

def with_connection(&task)
  @strategy.run(&task)
end