dont create unnecessary procs

This commit is contained in:
rick 2011-02-27 09:48:39 -08:00
parent 98883a42e7
commit dddc8697e0
3 changed files with 31 additions and 18 deletions

View File

@ -81,10 +81,11 @@ module Faraday
Faraday::CompositeReadIO.new(*parts.map{|p| p.to_io })
end
def process_to_params(pieces, params, base = nil, &block)
def process_to_params(pieces, params, base = nil)
params.to_a.each do |key, value|
key_str = base ? "#{base}[#{key}]" : key
block = block_given? ? Proc.new : nil
case value
when Array
values = value.inject([]) { |a,v| a << [nil, v] }

View File

@ -9,7 +9,8 @@ module Faraday
class Builder
attr_accessor :handlers
def self.create(&block)
def self.create
block = block_given? ? Proc.new : nil
Builder.new(&block)
end
@ -19,17 +20,17 @@ module Faraday
end
end
def initialize(handlers = [], &block)
def initialize(handlers = [])
@handlers = handlers
build(&block) if block_given?
build(Proc.new) if block_given?
end
def build(options = {}, &block)
def build(options = {})
inner = @handlers.shift
if !options[:keep]
@handlers.clear
end
block.call(self)
yield self if block_given?
run(inner || self.class.inner_app)
end
@ -51,23 +52,28 @@ module Faraday
@handlers[1..-1].inject(inner_app) { |app, middleware| middleware.call(app) }
end
def use(klass, *args, &block)
def use(klass, *args)
block = block_given? ? Proc.new : nil
run(lambda { |app| klass.new(app, *args, &block) })
end
def request(key, *args, &block)
def request(key, *args)
block = block_given? ? Proc.new : nil
use_symbol(Faraday::Request, key, *args, &block)
end
def response(key, *args, &block)
def response(key, *args)
block = block_given? ? Proc.new : nil
use_symbol(Faraday::Response, key, *args, &block)
end
def adapter(key, *args, &block)
def adapter(key, *args)
block = block_given? ? Proc.new : nil
use_symbol(Faraday::Adapter, key, *args, &block)
end
def use_symbol(mod, key, *args, &block)
def use_symbol(mod, key, *args)
block = block_given? ? Proc.new : nil
use(mod.lookup_module(key), *args, &block)
end

View File

@ -17,7 +17,7 @@ module Faraday
# :headers
# :request
# :ssl
def initialize(url = nil, options = {}, &block)
def initialize(url = nil, options = {})
if url.is_a?(Hash)
options = url
url = options[:url]
@ -32,7 +32,8 @@ module Faraday
merge_params @params, options[:params] if options[:params]
merge_headers @headers, options[:headers] if options[:headers]
if block
if block_given?
block = Proc.new
@builder = Builder.new
@builder.build { block.call(self) }
else
@ -60,23 +61,28 @@ module Faraday
@builder.build(options, &block)
end
def get(url = nil, headers = nil, &block)
def get(url = nil, headers = nil)
block = block_given? ? Proc.new : nil
run_request(:get, url, nil, headers, &block)
end
def post(url = nil, body = nil, headers = nil, &block)
def post(url = nil, body = nil, headers = nil)
block = block_given? ? Proc.new : nil
run_request(:post, url, body, headers, &block)
end
def put(url = nil, body = nil, headers = nil, &block)
def put(url = nil, body = nil, headers = nil)
block = block_given? ? Proc.new : nil
run_request(:put, url, body, headers, &block)
end
def head(url = nil, headers = nil, &block)
def head(url = nil, headers = nil)
block = block_given? ? Proc.new : nil
run_request(:head, url, nil, headers, &block)
end
def delete(url = nil, headers = nil, &block)
def delete(url = nil, headers = nil)
block = block_given? ? Proc.new : nil
run_request(:delete, url, nil, headers, &block)
end