Sha256: 5e7953bfad627ef710a9faa9f4ed89d4ca85b087a93a68b9576b2dd8e0303197

Contents?: true

Size: 1.56 KB

Versions: 6

Compression:

Stored size: 1.56 KB

Contents

module Waves
  
  module Resources
    
    module Mixin
      
      def controller( resource = nil )
        resource ||= self.class.basename
        @controller ||= app::Controllers[ resource ].new( @request )
      end
      
      def view( resource = nil )
        resource ||= self.class.basename
        @view ||= app::Views[ resource ].new( @request )
      end
  
    end
    
  end
  
  module ResponseMixin

    # 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.
    def model_name; self.class.basename.snake_case; end

    # 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.
    def model; app::Models[ model_name.intern ]; end

    # MVC Params get automatically destructured with the keys as accessors methods.
    # You can still access the original query by calling request.query
    def query
      @query ||= Waves::Request::Query.new( 
        Waves::Request::Utilities.destructure( request.query ) )
    end

    # Attributes are just the query elements specific to the model associated with
    # the current resource.
    def attributes
      query[ model_name ]
    end

  end

end

Version data entries

6 entries across 6 versions & 4 rubygems

Version Path
dyoder-waves-0.8.0 lib/layers/mvc/extensions.rb
waves-edge-2009.03.10.13.14 lib/layers/mvc/extensions.rb
waves-stable-2009.3.10 lib/layers/mvc/extensions.rb
waves-0.8.0 lib/layers/mvc/extensions.rb
waves-0.8.2 lib/layers/mvc/extensions.rb
waves-0.8.1 lib/layers/mvc/extensions.rb