Class: Opts::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, &block) ⇒ Builder

Returns a new instance of Builder.



5
6
7
8
9
10
11
12
13
14
# File 'lib/opts/builder.rb', line 5

def initialize(app=nil, &block)
  @app = app
  @ops = []
  
  if block and block.arity == -1
    instance_eval(&block)
  elsif block_given?
    yield(self)
  end
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



3
4
5
# File 'lib/opts/builder.rb', line 3

def app
  @app
end

Instance Method Details

#call(env, args) ⇒ Object



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

def call(env, args)
  to_app.call(env, args)
end

#run(app) ⇒ Object



20
21
22
# File 'lib/opts/builder.rb', line 20

def run(app)
  @app = app
end

#to_appObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/opts/builder.rb', line 24

def to_app
  @target ||= begin
    @ops.reverse.inject(@app) do |app, (klass, args, block)|
      if Class === klass
        klass.new(app, *args, &block)
      else
        klass.app = app
        klass
      end
    end
  end
end

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



16
17
18
# File 'lib/opts/builder.rb', line 16

def use(klass, *args, &block)
  @ops << [klass, args, block]
end