Class: Wrest::Callback
- Inherits:
-
Object
- Object
- Wrest::Callback
- Defined in:
- lib/wrest/callback.rb
Instance Attribute Summary (collapse)
-
- (Object) callback_hash
readonly
Returns the value of attribute callback_hash.
Class Method Summary (collapse)
Instance Method Summary (collapse)
- - (Object) execute(response)
-
- (Callback) initialize(callable)
constructor
A new instance of Callback.
- - (Object) merge(callback)
- - (Object) on(code, &block)
Constructor Details
- (Callback) initialize(callable)
A new instance of Callback
14 15 16 17 18 19 20 21 |
# File 'lib/wrest/callback.rb', line 14 def initialize(callable) if callable.is_a?(Hash) @callback_hash = Callback.ensure_values_are_collections(callable) elsif callable.is_a?(Proc) @callback_hash = {} callable.call(self) end end |
Instance Attribute Details
- (Object) callback_hash (readonly)
Returns the value of attribute callback_hash
12 13 14 |
# File 'lib/wrest/callback.rb', line 12 def callback_hash @callback_hash end |
Class Method Details
+ (Object) ensure_values_are_collections(hash)
57 58 59 60 61 62 63 |
# File 'lib/wrest/callback.rb', line 57 def self.ensure_values_are_collections(hash) result = {} hash.each do |code, block| result[code] = block.is_a?(Array) ? block : [block] end result end |
Instance Method Details
- (Object) execute(response)
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/wrest/callback.rb', line 33 def execute(response) callback_hash.each do |code, callback_list| callback_list.each {|callback| callback.call(response)} if case code when Range code.include?(response.code.to_i) when Fixnum code == response.code.to_i end end end |
- (Object) merge(callback)
23 24 25 26 27 28 29 30 31 |
# File 'lib/wrest/callback.rb', line 23 def merge(callback) merged_callback_hash = callback_hash.clone other_callback_hash = callback.callback_hash other_callback_hash.each do |code, callback_blocks| merged_callback_hash[code] ||= [] merged_callback_hash[code] += callback_blocks end Callback.new(merged_callback_hash) end |
- (Object) on(code, &block)
44 45 46 |
# File 'lib/wrest/callback.rb', line 44 def on(code, &block) @callback_hash[code] ? @callback_hash[code] << block : @callback_hash[code] = [block] end |