Module: HTTPrb::Delegator

Defined in:
lib/httprb/delegate.rb

Class Method Summary collapse

Class Method Details

.collides?(method) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/httprb/delegate.rb', line 42

def self.collides?(method)
  unless Object.const_defined?(:IRB) and IRB.respond_to?(:conf)
    eval "      module ::IRB\n        def IRB.binding\n          \#{binding}\n        end\n        def IRB.workspace; IRB; end\n        def IRB.conf; {:MAIN_CONTEXT => IRB}; end\n      end\n    RUBY\n  end\n  ::IRB::InputCompletor::CompletionProc.call(method.to_s).include?(method.to_s)\nend\n"

.delegate(*methods) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/httprb/delegate.rb', line 57

def self.delegate(*methods)
  methods.each do |method_name|
    if self.collides?(method_name)
      self.delegate_collision(method_name)
    else
      eval "        def \#{method_name}(*args, &b)\n          ::HTTPrb.send(\#{method_name.inspect}, *args, &b)\n        end\n        private \#{method_name.inspect}\n      RUBY\n    end\n  end\nend\n", binding, '(__DELEGATE__)', 1

.delegate_collision(method) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/httprb/delegate.rb', line 20

def self.delegate_collision(method)
  ENV['HTTPRB_COLLISION_PREFIX'] ||= 'client_'
  ENV['HTTPRB_IGNORE_COLLISIONS'] ||= 'true'
  eval "    alias_method \#{method.inspect}_original, \#{method.inspect}\n    \n    def \#{method}(*args, &b)\n      if ENV['HTTPRB_IGNORE_COLLISIONS'] == 'true'\n        puts \"WARNING: Namespace collision detected\"\n        puts \"HTTPrb::\#{method} has been renamed to \#{ENV['HTTPRB_COLLISION_PREFIX']}\#{method}\"\n        puts \"To disable this message, set HTTPRB_IGNORE_COLLISIONS to false\"\n      end\n      \#{method}_original(*args, &b)\n    end\n    \n    def \#{ENV['HTTPRB_COLLISION_PREFIX']}\#{method}(*args, &b)\n      ::HTTPrb.send(\#{method.inspect}, *args, &b)\n    end\n    private \#{method.inspect}\n  RUBY\nend\n", binding, '(__DELEGATE__)', 1