Class: Praxis::Application

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/praxis/application.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/praxis/application.rb', line 28

def initialize
  @controllers = Set.new
  @resource_definitions = Set.new

  @error_handler = ErrorHandler.new

  @router = Router.new(self)

  @builder = Rack::Builder.new
  @app = nil

  @bootloader = Bootloader.new(self)
  @file_layout = nil
  @plugins = Hash.new
  @loaded_files = Set.new
  @config = Config.new
  @root = nil
  @logger = Logger.new(STDOUT)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



12
13
14
# File 'lib/praxis/application.rb', line 12

def app
  @app
end

#bootloaderObject

Returns the value of attribute bootloader.



15
16
17
# File 'lib/praxis/application.rb', line 15

def bootloader
  @bootloader
end

#builderObject (readonly)

Returns the value of attribute builder.



13
14
15
# File 'lib/praxis/application.rb', line 13

def builder
  @builder
end

#controllersObject (readonly)

Returns the value of attribute controllers.



10
11
12
# File 'lib/praxis/application.rb', line 10

def controllers
  @controllers
end

#error_handlerObject

Returns the value of attribute error_handler.



21
22
23
# File 'lib/praxis/application.rb', line 21

def error_handler
  @error_handler
end

#file_layoutObject

Returns the value of attribute file_layout.



16
17
18
# File 'lib/praxis/application.rb', line 16

def file_layout
  @file_layout
end

#loaded_filesObject

Returns the value of attribute loaded_files.



17
18
19
# File 'lib/praxis/application.rb', line 17

def loaded_files
  @loaded_files
end

#loggerObject

Returns the value of attribute logger.



18
19
20
# File 'lib/praxis/application.rb', line 18

def logger
  @logger
end

#pluginsObject

Returns the value of attribute plugins.



19
20
21
# File 'lib/praxis/application.rb', line 19

def plugins
  @plugins
end

#resource_definitionsObject (readonly)

Returns the value of attribute resource_definitions.



11
12
13
# File 'lib/praxis/application.rb', line 11

def resource_definitions
  @resource_definitions
end

#rootObject

Returns the value of attribute root.



20
21
22
# File 'lib/praxis/application.rb', line 20

def root
  @root
end

#routerObject (readonly)

Returns the value of attribute router.



9
10
11
# File 'lib/praxis/application.rb', line 9

def router
  @router
end

Class Method Details

.configure {|self.instance| ... } ⇒ Object

Yields:

  • (self.instance)


24
25
26
# File 'lib/praxis/application.rb', line 24

def self.configure
  yield(self.instance)
end

Instance Method Details

#call(env) ⇒ Object



71
72
73
74
75
76
# File 'lib/praxis/application.rb', line 71

def call(env)
  response = []
  Notifications.instrument 'rack.request.all'.freeze, response: response do
    response.push(*@app.call(env))
  end
end

#config(key = nil, type = Attributor::Struct, **opts, &block) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/praxis/application.rb', line 82

def config(key=nil, type=Attributor::Struct, **opts, &block)
  if block_given? || (type==Attributor::Struct && !opts.empty? )
    @config.define(key, type, opts, &block)
  else
    @config.get
  end
end

#config=(config) ⇒ Object



90
91
92
# File 'lib/praxis/application.rb', line 90

def config=(config)
  @config.set(config)
end

#layout(&block) ⇒ Object



78
79
80
# File 'lib/praxis/application.rb', line 78

def layout(&block)
  self.file_layout = FileGroup.new(self.root, &block)
end

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



67
68
69
# File 'lib/praxis/application.rb', line 67

def middleware(middleware, *args, &block)
  @builder.use(middleware, *args, &block)
end

#setup(root: '.') ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/praxis/application.rb', line 48

def setup(root: '.')
  @root = Pathname.new(root).expand_path

  @bootloader.setup!

  @builder.run(@router)
  @app = @builder.to_app

  Notifications.subscribe 'rack.request.all'.freeze do |name, start, finish, _id, payload|
    duration = (finish - start) * 1000
    Stats.timing(name, duration)

    status, _, _ = payload[:response]
    Stats.increment "rack.request.#{status}"
  end

  self
end