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

Instance Method Details

#appObject



36
# File 'lib/waves/response/response_mixin.rb', line 36

def app ; self.class.root ; end

#attributesObject

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

#capturedObject

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

#logObject



34
# File 'lib/waves/response/response_mixin.rb', line 34

def log; Waves::Logger; end

#mainObject



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_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.



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

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

#modified?(last_modified) ⇒ Boolean

Returns:

  • (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_foundObject

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_modifiedObject



64
65
66
# File 'lib/waves/response/response_mixin.rb', line 64

def not_modified
  raise Waves::Response::Redirects::NotModified.new
end

#paramsObject

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

#queryObject

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

#resourceObject



13
# File 'lib/waves/response/response_mixin.rb', line 13

def resource; traits.waves.resource || ( self if self.kind_of? Waves::Resources::Mixin ) ; end

#responseObject

Access the response.



11
# File 'lib/waves/response/response_mixin.rb', line 11

def response; request.response; end

#traitsObject



15
# File 'lib/waves/response/response_mixin.rb', line 15

def traits ; request.traits ; end

#view(resource = nil) ⇒ Object



36
37
38
39
40
# File 'lib/waves/layers/mvc/extensions.rb', line 36

def view( resource = nil )
  resource ||= self.class.basename.snake_case
  @view ||= {}
  @view[resource] ||= app::Views[ resource ].new( @request )
end