Class Wrest::Callback

  1. lib/wrest/callback.rb
Parent: Object

Methods

public class

  1. ensure_values_are_collections
  2. new

public instance

  1. execute
  2. merge
  3. on

Attributes

callback_hash [R]

Public class methods

ensure_values_are_collections (hash)
[show source]
    # File lib/wrest/callback.rb, line 57
57:     def self.ensure_values_are_collections(hash)
58:       result = {}
59:       hash.each do |code, block|
60:         result[code] = block.is_a?(Array) ? block : [block]
61:       end
62:       result
63:     end
new (callable)
[show source]
    # File lib/wrest/callback.rb, line 14
14:     def initialize(callable)
15:       if callable.is_a?(Hash)
16:         @callback_hash = Callback.ensure_values_are_collections(callable)
17:       elsif callable.is_a?(Proc)
18:         @callback_hash = {}
19:         callable.call(self)
20:       end
21:     end

Public instance methods

execute (response)
[show source]
    # File lib/wrest/callback.rb, line 33
33:     def execute(response)
34:       callback_hash.each do |code, callback_list|
35:         callback_list.each {|callback| callback.call(response)} if case code
36:         when Range
37:           code.include?(response.code.to_i)
38:         when Fixnum
39:           code == response.code.to_i
40:         end
41:       end
42:     end
merge (callback)
[show source]
    # File lib/wrest/callback.rb, line 23
23:     def merge(callback)
24:       merged_callback_hash = callback_hash.clone
25:       other_callback_hash = callback.callback_hash
26:       other_callback_hash.each do |code, callback_blocks|
27:         merged_callback_hash[code] ||= [] 
28:         merged_callback_hash[code] += callback_blocks
29:       end
30:       Callback.new(merged_callback_hash)
31:     end
on (code, &block)
[show source]
    # File lib/wrest/callback.rb, line 44
44:     def on(code, &block)
45:       @callback_hash[code] ? @callback_hash[code] << block : @callback_hash[code] = [block]
46:     end