Class: Flame::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/flame/application.rb

Overview

Core class, like Framework::Application

Defined Under Namespace

Classes: Config

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) ⇒ Application

Returns a new instance of Application.



28
29
30
# File 'lib/flame/application.rb', line 28

def initialize(app = nil)
  @app = app
end

Class Attribute Details

.configObject

Returns the value of attribute config.



8
9
10
# File 'lib/flame/application.rb', line 8

def config
  @config
end

Class Method Details

.call(env) ⇒ Object

Make available ‘run Application` without `.new` for `rackup`



39
40
41
42
# File 'lib/flame/application.rb', line 39

def self.call(env)
  @app ||= new
  @app.call env
end

.default_config_dirs(root_dir:) ⇒ Object

Initialize default for config directories



72
73
74
75
76
77
78
# File 'lib/flame/application.rb', line 72

def self.default_config_dirs(root_dir:)
  result = { root_dir: File.realpath(root_dir) }
  i(public views config tmp).each do |key|
    result[:"#{key}_dir"] = proc { File.join(config[:root_dir], key.to_s) }
  end
  result
end

.inherited(app) ⇒ Object

Generating application config when inherited



17
18
19
20
21
22
23
24
25
26
# File 'lib/flame/application.rb', line 17

def self.inherited(app)
  app.config = Config.new(
    app,
    default_config_dirs(
      root_dir: File.dirname(caller[0].split(':')[0])
    ).merge(
      environment: ENV['RACK_ENV'] || 'development'
    )
  )
end

.mount(ctrl, path = nil) { ... } ⇒ Object

Mount controller in application class

Examples:

Mount controller with defaults

mount ArticlesController

Mount controller with specific path

mount HomeController, '/welcome'

Mount controller with specific path of methods

mount HomeController do
  get '/bye', :goodbye
  post '/greetings', :new
  defaults
end

Parameters:

  • ctrl (Flame::Controller)

    the mounted controller class

  • path (String, nil) (defaults to: nil)

    root path for the mounted controller

Yields:

  • refine defaults pathes for a methods of the mounted controller



58
59
60
# File 'lib/flame/application.rb', line 58

def self.mount(ctrl, path = nil, &block)
  router.add_controller(ctrl, path, block)
end

.routerObject

Router for routing



63
64
65
# File 'lib/flame/application.rb', line 63

def self.router
  @router ||= Flame::Router.new(self)
end

Instance Method Details

#call(env) ⇒ Object

Request recieving method



33
34
35
36
# File 'lib/flame/application.rb', line 33

def call(env)
  @app.call(env) if @app.respond_to? :call
  Flame::Dispatcher.new(self, env).run!
end

#configObject

Framework configuration



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

def config
  self.class.config
end

#routerObject



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

def router
  self.class.router
end