Module: Waves::ResponseMixin
- Included in:
- Controllers::Mixin, Foundations::REST::Resource, Views::Mixin
- Defined in:
- lib/waves/response/response_mixin.rb,
lib/waves/layers/mvc/extensions.rb
Overview
Defines a set of methods that simplify accessing common request and response methods. These include methods not necessarily associated with the Waves::Request and Waves::Response objects, but which may still be useful for constructing a response. This mixin assumes that a @request@ accessor already exists.
Instance Method Summary collapse
- #app ⇒ Object
-
#attributes ⇒ Object
Attributes are just the query elements specific to the model associated with the current resource.
-
#captured ⇒ Object
Elements captured the path.
- #controller(resource = nil) ⇒ Object
- #http_cache(last_modified) ⇒ Object
- #log ⇒ Object
- #main ⇒ Object
-
#model(resource = nil) ⇒ Object
Returns the model corresponding to this controller by naively assuming that
model_name
must be correct. -
#model_name ⇒ Object
Returns the name of the model corresponding to this controller by taking the basename of the module and converting it to snake case.
- #modified?(last_modified) ⇒ Boolean
-
#not_found ⇒ Object
Raise a not found exception.
- #not_modified ⇒ Object
-
#params ⇒ Object
Both the query and capture merged together.
- #paths(rname = nil) ⇒ Object
-
#query ⇒ Object
Access to the query string as a object where the keys are accessors You can still access the original query as request.query.
-
#redirect(path) ⇒ Object
Issue a redirect for the given path.
- #resource ⇒ Object
-
#response ⇒ Object
Access the response.
- #traits ⇒ Object
- #view(resource = nil) ⇒ Object
Instance Method Details
#app ⇒ Object
36 |
# File 'lib/waves/response/response_mixin.rb', line 36 def app ; self.class.root ; end |
#attributes ⇒ Object
Attributes are just the query elements specific to the model associated with the current resource.
58 59 60 |
# File 'lib/waves/layers/mvc/extensions.rb', line 58 def attributes query[ model_name ] end |
#captured ⇒ Object
Elements captured the path
22 |
# File 'lib/waves/response/response_mixin.rb', line 22 def captured ; @captured ||= traits.waves.captured ; end |
#controller(resource = nil) ⇒ Object
30 31 32 33 34 |
# File 'lib/waves/layers/mvc/extensions.rb', line 30 def controller( resource = nil ) resource ||= self.class.basename.snake_case @controller ||= {} @controller[resource] ||= app::Controllers[ resource ].new( @request ) end |
#http_cache(last_modified) ⇒ Object
44 45 46 47 |
# File 'lib/waves/response/response_mixin.rb', line 44 def http_cache( last_modified ) response.last_modified = last_modified modified?( last_modified ) ? yield : not_modified end |
#main ⇒ Object
38 |
# File 'lib/waves/response/response_mixin.rb', line 38 def main ; Waves.main ; end |
#model(resource = nil) ⇒ Object
Returns the model corresponding to this controller by naively assuming that model_name
must be correct. This allows you to write generic controller methods such as:
model.find( name )
to find an instance of a given model. Again, the plurality of the controller and model must be the same for this to work.
25 26 27 28 |
# File 'lib/waves/layers/mvc/extensions.rb', line 25 def model( resource = nil ) resource ||= self.class.basename.snake_case app::Models[ resource ] end |
#model_name ⇒ Object
Returns the name of the model corresponding to this controller by taking the basename of the module and converting it to snake case. If the model plurality is different than the controller, this will not, in fact, be the model name.
16 |
# File 'lib/waves/layers/mvc/extensions.rb', line 16 def model_name; self.class.basename.snake_case; end |
#modified?(last_modified) ⇒ Boolean
49 50 51 52 |
# File 'lib/waves/response/response_mixin.rb', line 49 def modified?( last_modified ) request.if_modified_since.nil? || last_modified > request.if_modified_since end |
#not_found ⇒ Object
Raise a not found exception.
55 56 57 |
# File 'lib/waves/response/response_mixin.rb', line 55 def not_found raise Waves::Response::ClientErrors::NotFound.new end |
#not_modified ⇒ Object
64 65 66 |
# File 'lib/waves/response/response_mixin.rb', line 64 def not_modified raise Waves::Response::Redirects::NotModified.new end |
#params ⇒ Object
Both the query and capture merged together
50 51 52 53 54 |
# File 'lib/waves/layers/mvc/extensions.rb', line 50 def params @params ||= Waves::Request::Query.new( captured ? Waves::Request::Utilities.destructure( request.query ).merge( captured.to_h ) : Waves::Request::Utilities.destructure( request.query ) ) end |
#paths(rname = nil) ⇒ Object
40 41 42 |
# File 'lib/waves/response/response_mixin.rb', line 40 def paths( rname = nil ) ( rname ? app::Resources[ rname ].paths : resource.class.paths ).new end |
#query ⇒ Object
Access to the query string as a object where the keys are accessors You can still access the original query as request.query
44 45 46 47 |
# File 'lib/waves/layers/mvc/extensions.rb', line 44 def query @query ||= Waves::Request::Query.new( Waves::Request::Utilities.destructure( request.query ) ) end |
#redirect(path) ⇒ Object
Issue a redirect for the given path.
60 61 62 |
# File 'lib/waves/response/response_mixin.rb', line 60 def redirect( path ) raise Waves::Response::Redirects::Found.new( path ) end |
#resource ⇒ Object
13 |
# File 'lib/waves/response/response_mixin.rb', line 13 def resource; traits.waves.resource || ( self if self.kind_of? Waves::Resources::Mixin ) ; end |
#response ⇒ Object
Access the response.
11 |
# File 'lib/waves/response/response_mixin.rb', line 11 def response; request.response; end |
#traits ⇒ Object
15 |
# File 'lib/waves/response/response_mixin.rb', line 15 def traits ; request.traits ; end |