Class: Flame::Application

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

Overview

Core class, like Framework::Application

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) ⇒ Application

Returns a new instance of Application.



132
133
134
# File 'lib/flame/application.rb', line 132

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

Class Method Details

.call(env) ⇒ Object

Make available run Application without .new for rackup



46
47
48
49
# File 'lib/flame/application.rb', line 46

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

.inherited(app) ⇒ Object

Remember root directory when inherited



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

def inherited(app)
	super
	app.root_dir = File.dirname caller(2..2).first.split(':')[0]
end

.path_to(ctrl, action = :index, args = {}) ⇒ String

Build a path to the given controller and action

Examples:

Path for show(id) method of ArticlesController

path_to ArticlesController, :show, id: 2 # => "/articles/show/2"

Path for new method of ArticlesController with query

path_to ArticlesController, :new, author_id: 1
# => "/articles/new?author_id=1"

Parameters:

  • ctrl (Flame::Controller)

    class of controller

  • action (Symbol) (defaults to: :index)

    method of controller

  • args (Hash) (defaults to: {})

    parameters for method of controller

Returns:

  • (String)

    path for requested method, controller and parameters

Raises:



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/flame/application.rb', line 64

def path_to(ctrl, action = :index, args = {})
	path = router.path_of(ctrl, action)

	raise Errors::RouteNotFoundError.new(ctrl, action) unless path

	args = args.deep_dup
	path = path.assign_arguments(args)
	path = '/' if path.empty?
	query = Rack::Utils.build_nested_query args unless args.empty?
	Addressable::URI.new(path: path, query: query).to_s
end

.require_dirs(dirs, ignore: []) ⇒ Object

Require project directories, exclude executable files )

Examples:

Regular require of project

Flame::Application.require_dirs(
  %w[config lib models helpers mailers services controllers]

Parameters:

  • dirs (Array<String>)

    Array of directories names



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

def require_dirs(dirs, ignore: [])
	dirs.each do |dir|
		require_dir File.join(root_dir, dir), ignore: ignore
	end
end

.routerObject

Router for routing



25
26
27
# File 'lib/flame/application.rb', line 25

memoize def router
	Flame::Router.new(self)
end

Instance Method Details

#call(env) ⇒ Object

Request receiving method



137
138
139
140
# File 'lib/flame/application.rb', line 137

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