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 <<-RUBY
      module ::IRB
        def IRB.binding
          #{binding}
        end
        def IRB.workspace; IRB; end
        def IRB.conf; {:MAIN_CONTEXT => IRB}; end
      end
    RUBY
  end
  ::IRB::InputCompletor::CompletionProc.call(method.to_s).include?(method.to_s)
end

.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 <<-RUBY, binding, '(__DELEGATE__)', 1
        def #{method_name}(*args, &b)
          ::HTTPrb.send(#{method_name.inspect}, *args, &b)
        end
        private #{method_name.inspect}
      RUBY
    end
  end
end

.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 <<-RUBY, binding, '(__DELEGATE__)', 1
    alias_method #{method.inspect}_original, #{method.inspect}
    
    def #{method}(*args, &b)
      if ENV['HTTPRB_IGNORE_COLLISIONS'] == 'true'
        puts "WARNING: Namespace collision detected"
        puts "HTTPrb::#{method} has been renamed to #{ENV['HTTPRB_COLLISION_PREFIX']}#{method}"
        puts "To disable this message, set HTTPRB_IGNORE_COLLISIONS to false"
      end
      #{method}_original(*args, &b)
    end
    
    def #{ENV['HTTPRB_COLLISION_PREFIX']}#{method}(*args, &b)
      ::HTTPrb.send(#{method.inspect}, *args, &b)
    end
    private #{method.inspect}
  RUBY
end