Module: Waves::ResponseMixin

Included in:
Controllers::Mixin, Helpers::Basic, Waves::Resources::Paths, Views::Mixin
Defined in:
lib/runtime/response_mixin.rb,
lib/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

Instance Method Details

#appObject



40
# File 'lib/runtime/response_mixin.rb', line 40

def app ; eval(  "::#{app_name.to_s.camel_case}" ) ; end

#app_nameObject

access stuff from an app



39
# File 'lib/runtime/response_mixin.rb', line 39

def app_name ; self.class.rootname.snake_case.to_sym ; end

#attributesObject

Attributes are just the query elements specific to the model associated with the current resource.



46
47
48
# File 'lib/layers/mvc/extensions.rb', line 46

def attributes
  query[ model_name ]
end

#basename(str = nil) ⇒ Object

these take strings or operate on the path by default



46
# File 'lib/runtime/response_mixin.rb', line 46

def basename( str = nil ) ; ( str or path ).sub(/\.([^\.]+)$/,'') ; end

#capturedObject

Elements captured the path



22
# File 'lib/runtime/response_mixin.rb', line 22

def captured ; @captured ||= traits.waves.captured ; end

#extension(str = nil) ⇒ Object



48
49
50
# File 'lib/runtime/response_mixin.rb', line 48

def extension( str = nil )
  ( m = ( str or path ).match(/\.([^\.]+)$/) ) ? m[1] : nil
end

#logObject

Access the Waves::Logger.



37
# File 'lib/runtime/response_mixin.rb', line 37

def log; Waves::Logger; end

#modelObject

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.



35
# File 'lib/layers/mvc/extensions.rb', line 35

def model; app::Models[ model_name.intern ]; end

#model_nameObject

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.



26
# File 'lib/layers/mvc/extensions.rb', line 26

def model_name; self.class.basename.snake_case; end

#paramsObject

Both the query and capture merged together



25
26
27
28
# File 'lib/runtime/response_mixin.rb', line 25

def params 
  query = captured ? request.query.merge( captured.to_h ) : request.query
  @params ||= Waves::Request::Query.new( query ) 
end

#paths(rname = nil) ⇒ Object



41
42
43
# File 'lib/runtime/response_mixin.rb', line 41

def paths( rname = nil )
  ( rname.nil? ? resource.class.paths : app::Resources[ rname ].paths ).new( request )
end

#queryObject

Access to the query string as a object where the keys are accessors You can still access the original query as request.query



39
40
41
42
# File 'lib/layers/mvc/extensions.rb', line 39

def query
  @query ||= Waves::Request::Query.new( 
    Waves::Request::Utilities.destructure( request.query ) )
end

#redirect(location, status = '302') ⇒ Object

Issue a redirect for the given location.



35
# File 'lib/runtime/response_mixin.rb', line 35

def redirect(location, status = '302'); request.redirect(location, status); end

#render(path, assigns = {}) ⇒ Object



52
53
54
# File 'lib/runtime/response_mixin.rb', line 52

def render( path, assigns = {} )
  Waves::Views::Base.new( request ).render( path, assigns )
end

#resourceObject



13
# File 'lib/runtime/response_mixin.rb', line 13

def resource ; traits.waves.resource ; end

#responseObject

Access the response.



11
# File 'lib/runtime/response_mixin.rb', line 11

def response; request.response; end

#traitsObject



15
# File 'lib/runtime/response_mixin.rb', line 15

def traits ; request.traits ; end