Module: Waves::Resources::Mixin

Included in:
Base
Defined in:
lib/waves/resources/mixin.rb,
lib/waves/layers/mvc/extensions.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



7
8
9
# File 'lib/waves/resources/mixin.rb', line 7

def request
  @request
end

Class Method Details

.included(resource) ⇒ Object

this is necessary because you can’t define functors within a module because the functor attempts to incorporate the superclass functor table into it’s own



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/waves/resources/mixin.rb', line 65

def self.included( resource )

  resource.module_eval do

    include ResponseMixin, Functor::Method ; extend ClassMethods

    def initialize( request ); @request = request ; end

    # define defaults for all the functors, providing the analog
    # of "not implemented" behaviors. this avoids complicating
    # the error handling with having to distinguish between
    # functor match-related errors and actual application errors
    
    # by default, don't do anything in the wrapper methods
    before {} ; after {} ; always {}

    # if we get here, this is a 404
    %w( post get put delete head ).each do | method |
      on( method ) { not_found }
    end
    
    # default handler is just to propagate the exception
    handler( Exception ) { |e| raise( e ) }

    def process
      begin
        before ;  rval = send( request.method ) ; after
      rescue Exception => e
        e.call( response ) if e.respond_to?( :call )
        rval = handler( e )
      ensure
        always
      end
      # note: the dispatcher decides what to do with the
      # return value; all we care about is returning the
      # value from the appropriate application block
      return rval
    end

    def to( resource )
      resource = case resource
      when Base
        resource
      when Symbol, String
        Waves.main::Resources[ resource ]
      end
      r = traits.waves.resource = resource.new( request )
      r.process
    end

    def redirect( path ) ; request.redirect( path ) ; end

    # override for resources that may have long-running requests. this helps servers
    # determine how to handle the request
    def deferred? ; false ; end

  end

end