Class: Alice::Builder

Inherits:
Object show all
Defined in:
lib/alice/builder.rb

Overview

Possibly going to extend this a bit.

Alice::Connection.new(:url => ‘sushi.com’) do

request  :yajl     # Alice::Request::Yajl
adapter  :logger   # Alice::Adapter::Logger
response :yajl     # Alice::Response::Yajl

end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Builder

Returns a new instance of Builder.

Yields:

  • (_self)

Yield Parameters:



23
24
25
26
# File 'lib/alice/builder.rb', line 23

def initialize
  @handlers = []
  yield self
end

Instance Attribute Details

#handlersObject

Returns the value of attribute handlers.



10
11
12
# File 'lib/alice/builder.rb', line 10

def handlers
  @handlers
end

Class Method Details

.create_with_inner_app(&block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/alice/builder.rb', line 12

def self.create_with_inner_app(&block)
  inner = lambda do |env| 
    if !env[:parallel_manager]
      env[:response].finish(env)
    else
      env[:response]
    end
  end
  Builder.new(&block).tap { |builder| builder.run(inner) }
end

Instance Method Details

#adapter(key, *args, &block) ⇒ Object



49
50
51
# File 'lib/alice/builder.rb', line 49

def adapter(key, *args, &block)
  use_symbol Alice::Adapter, key, *args, &block
end

#request(key, *args, &block) ⇒ Object



41
42
43
# File 'lib/alice/builder.rb', line 41

def request(key, *args, &block)
  use_symbol Alice::Request, key, *args, &block
end

#response(key, *args, &block) ⇒ Object



45
46
47
# File 'lib/alice/builder.rb', line 45

def response(key, *args, &block)
  use_symbol Alice::Response, key, *args, &block
end

#run(app) ⇒ Object



28
29
30
# File 'lib/alice/builder.rb', line 28

def run app
  @handlers.unshift app
end

#to_appObject



32
33
34
35
# File 'lib/alice/builder.rb', line 32

def to_app
  inner_app = @handlers.first
  @handlers[1..-1].inject(inner_app) { |app, middleware| middleware.call(app) }
end

#use(klass, *args, &block) ⇒ Object



37
38
39
# File 'lib/alice/builder.rb', line 37

def use klass, *args, &block
  @handlers.unshift(lambda { |app| klass.new(app, *args, &block) })
end

#use_symbol(mod, key, *args, &block) ⇒ Object



53
54
55
# File 'lib/alice/builder.rb', line 53

def use_symbol(mod, key, *args, &block)
  use mod.lookup_module(key), *args, &block
end