-
Notifications
You must be signed in to change notification settings - Fork 7
Home
gem install flame
Use run MyProject::Application
or run MyProject::Application.new
Mount controllers in application-class by mount
method.
mount ParentController
will mounting ParrentController-class with all methods (GET requests) at '/parent'
For other path specify it as argument: mount ParentController, '/new_path'
For others HTTP methods use block as route-refines:
module MyProject
class Application < Flame::Application
mount ParentController do # default path will be '/parent'
post '/create', :create_object
put :update_object # default path same as method name
defaults # this method is require for blocks in mount (after refining HTTP methods)
# if not all controller-methods are refining
# it's including REST defaults
# You can mount children controllers
mount ChildController, '/children' # path will be '/parent/children'
end
end
end
You must specify method-arguments in path when refining: get '/hello/:name', :hello
for hello(name)
For method hello(first_name, last_name = nil)
default path will be '/hello/:first_name/:?last_name'
:first_name
- required argument, :?last_name
- not required.
Default available REST:
get '/', :index
post '/', :create
get '/:id', :show
put '/:id', :update
delete '/:id', :delete
:show
, :update
and :delete
methods by default must have id
argument: def show(id)
for example.
Application config available for config
method. Config-object like a Hash.
Root directory of application: config[:root_dir]
Also available :public_dir
, :views_dir
and :config_dir
Config options may be proc and called by #[]
: config[:locales_dir] = proc { File.join(config[:root_dir], 'locales' }
Environment also available: config[:environment] # => ENV['RACK_ENV'] or 'development'
Config-object available from controllers
Public instance methods (not inherited) - your actions.
execute
method is wrapper for any action execute, action-name (method-name) is argument. In this method you can invoke before-hooks, called method (super
) and after-hooks. Also you can handling errors by rescue
(re-raising of exception is recommended).
Get current response body: body
. Set: body 'New body'
.
status
works the same.
Also available as Hash request.headers
, response.headers
, cookies
and session
.
view
or render
receive file-name of template (and options for Tilt), and return rendering file as String.
path_to
method for building path to the controller and it's action: path_to ArticlesController, :show, id: 2
for show(id)
method of ArticlesController.
Return value of called method (action) will be body of response.
module MyProject
class SiteController < Flame::Controller
def index
view :index # or just `view` (default name of template is method-name)
end
end
class APIController < Flame::Controller
include APIHelper # That's out helper module! Described below
def index
{ result: 'success', data: 'Welcome!' }
end
protected
# Method for overriding controller executing (any route)
def execute(method)
# before hooks
check_access!
# You can also check which method is calling (`method` argument)
super # execute calling method
# after hooks
to_json body
rescue => exception # 500 error handling
body render :'errors/500' # `render` is alias for `view`
raise exception # needing for base-controllers or helpers
end
# And 404 handling!
# Attention: hooks will executing (not_found method calling as `super` in `execute` method)
def not_found
view :'error/404'
end
end
end
Helpers is just including modules for Controllers
module MyProject
module APIHelper
# Method for after-hook in controller above
def to_json(content = body)
response.headers[Rack::CONTENT_TYPE] = 'application/json; charset=utf-8'
body JSON.generate(content)
end
end
end
Flame::Render use Tilt engine.
Caching enabled in production and disabled in development (config[:environment]
).
Methods for rendering: view
and render
(receive file-name as String or Symbol, and options).
Layouts are supported.
If file-name begining from _
(_part.erb
) - layout is disabled by default.
All controllers methods are available from templates (controller's scope by default).
No models by Flame. Use what you want. I recommend Sequel.
Good luck!
Reports, issues, pull-requests and other cooperation are welcome.