Class: Rlyeh::Base

Inherits:
Object
  • Object
show all
Includes:
Dispatcher, Settings
Defined in:
lib/rlyeh/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Settings

included, #settings

Methods included from Dispatcher

#dispatch, included, #trigger

Constructor Details

#initialize(app = nil, options = {}) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:

  • _self (Rlyeh::Base)

    the object that the method was called on



56
57
58
59
60
# File 'lib/rlyeh/base.rb', line 56

def initialize(app = nil, options = {})
  @app = app
  @options = options
  yield self if block_given?
end

Class Method Details

.build(builder, *args, &block) ⇒ Object



37
38
39
40
41
42
# File 'lib/rlyeh/base.rb', line 37

def build(builder, *args, &block)
  setup_default_middlewares builder
  setup_middlewares builder
  builder.run new!(*args, &block)
  builder
end

.middlewaresObject



17
18
19
20
21
22
23
24
# File 'lib/rlyeh/base.rb', line 17

def middlewares
  @middlewares ||= []
  if superclass.respond_to?(:middlewares)
    superclass.middlewares + @middlewares
  else
    @middlewares
  end
end

.new(*args, &block) ⇒ Object



33
34
35
# File 'lib/rlyeh/base.rb', line 33

def new(*args, &block)
  build(Rlyeh::DeepOnes::Builder.new, *args, &block).to_app
end

.setup_default_middlewares(builder) ⇒ Object



44
45
46
47
# File 'lib/rlyeh/base.rb', line 44

def setup_default_middlewares(builder)
  builder.use! Rlyeh::DeepOnes::Closer
  builder.use! Rlyeh::DeepOnes::Parser
end

.setup_middlewares(builder) ⇒ Object



49
50
51
52
53
# File 'lib/rlyeh/base.rb', line 49

def setup_middlewares(builder)
  middlewares.each do |name, args, block|
    builder.use(name, *args, &block)
  end
end

.use(name, *args, &block) ⇒ Object



26
27
28
29
30
# File 'lib/rlyeh/base.rb', line 26

def use(name, *args, &block)
  @middlewares ||= []
  @middlewares << [name, args, block]
  @middlewares = @middlewares.uniq
end

Instance Method Details

#call(env) ⇒ Object



62
63
64
65
# File 'lib/rlyeh/base.rb', line 62

def call(env)
  dispatch env
  @app.call env if @app
end

#haltObject



67
68
69
# File 'lib/rlyeh/base.rb', line 67

def halt
  throw :halt
end

#new!Object



32
# File 'lib/rlyeh/base.rb', line 32

alias new! new