Class: Eldr::Builder

Inherits:
Rack::Builder
  • Object
show all
Defined in:
lib/eldr/builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Builder.



6
7
8
9
# File 'lib/eldr/builder.rb', line 6

def initialize(default_app = nil, &block)
  @middleware, @map, @run, @warmup = [], nil, default_app, nil
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#middlewareObject

Returns the value of attribute middleware.



5
6
7
# File 'lib/eldr/builder.rb', line 5

def middleware
  @middleware
end

Class Method Details

.app(default_app = nil, &block) ⇒ Object



11
12
13
# File 'lib/eldr/builder.rb', line 11

def self.app(default_app = nil, &block)
  new(default_app, &block).to_app
end

Instance Method Details

#call(env) ⇒ Object



48
49
50
# File 'lib/eldr/builder.rb', line 48

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

#map(path, &block) ⇒ Object



35
36
37
38
# File 'lib/eldr/builder.rb', line 35

def map(path, &block)
  @map ||= {}
  @map[path] = block
end

#run(app) ⇒ Object

rubocop:disable Style/TrivialAccessors



27
28
29
# File 'lib/eldr/builder.rb', line 27

def run(app) # rubocop:disable Style/TrivialAccessors
  @run = app
end

#to_appObject



40
41
42
43
44
45
46
# File 'lib/eldr/builder.rb', line 40

def to_app
  app = @map ? generate_map(@run, @map) : @run
  fail 'missing run or map statement' unless app
  app = @middleware.reverse.inject(app) { |a, e| e[a] }
  @warmup.call(app) if @warmup
  app
end

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



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/eldr/builder.rb', line 15

def use(middleware, *args, &block)
  if middleware.is_a? Array
    @middleware.push(*middleware)
  else
    if @map
      mapping, @map = @map, nil
      @middleware << proc { |app| generate_map app, mapping }
    end
    @middleware << proc { |app| middleware.new(app, *args, &block) }
  end
end

#warmup(prc = nil, &block) ⇒ Object



31
32
33
# File 'lib/eldr/builder.rb', line 31

def warmup(prc = nil, &block)
  @warmup = prc || block
end