Sha256: 5ab176cf73bc9f445af9f9bab7b88ca86f2fc76a7fcbee35c806b2521da1dbd0

Contents?: true

Size: 1.56 KB

Versions: 37

Compression:

Stored size: 1.56 KB

Contents

require 'couch_potato/view/base_view_spec'
require 'couch_potato/view/model_view_spec'
require 'couch_potato/view/properties_view_spec'
require 'couch_potato/view/custom_view_spec'
require 'couch_potato/view/raw_view_spec'


module CouchPotato
  module View
    module CustomViews

      def self.included(base) #:nodoc:
        base.extend ClassMethods
      end

      module ClassMethods
        def views(view_name = nil) #:nodoc:
          if view_name
            _find_view(view_name)
          else
            @views ||= {}
          end
        end

        def execute_view(view_name, view_parameters) #:nodoc:
          view_spec_class(views(view_name)[:type]).new(self, view_name, views(view_name), view_parameters)
        end
        
        # Declare a CouchDB view, for examples on how to use see the *ViewSpec classes in CouchPotato::View
        def view(view_name, options)
          view_name = view_name.to_s
          views[view_name] = options
          method_str = "def #{view_name}(view_parameters = {}); execute_view(\"#{view_name}\", view_parameters); end"
          self.instance_eval(method_str)
        end

        def view_spec_class(type) #:nodoc:
          if type && type.is_a?(Class)
            type
          else
            name = type.nil? ? 'Model' : type.to_s.camelize
            CouchPotato::View.const_get("#{name}ViewSpec")
          end
        end
        
        def _find_view(view) #:nodoc:
          (@views && @views[view]) || (superclass._find_view(view) if superclass.respond_to?(:_find_view))
        end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 3 rubygems

Version Path
couch_potato-1.7.1 lib/couch_potato/view/custom_views.rb
couch_potato-1.7.0 lib/couch_potato/view/custom_views.rb
couch_potato-1.6.5 lib/couch_potato/view/custom_views.rb
couch_potato-1.6.4 lib/couch_potato/view/custom_views.rb
couch_potato-1.6.3 lib/couch_potato/view/custom_views.rb
couch_potato-1.4.0 lib/couch_potato/view/custom_views.rb
couch_potato-1.3.0 lib/couch_potato/view/custom_views.rb
couch_potato-1.2.0 lib/couch_potato/view/custom_views.rb
couch_potato-1.1.4 lib/couch_potato/view/custom_views.rb
couch_potato-1.1.2 lib/couch_potato/view/custom_views.rb
couch_potato-1.1.1 lib/couch_potato/view/custom_views.rb
couch_potato-1.1.0 lib/couch_potato/view/custom_views.rb
couch_potato-1.0.1 lib/couch_potato/view/custom_views.rb
couch_potato-1.0.0 lib/couch_potato/view/custom_views.rb
couch_potato-0.7.1 lib/couch_potato/view/custom_views.rb
couch_potato-0.7.0 lib/couch_potato/view/custom_views.rb
couch_potato-0.7.0.pre.1 lib/couch_potato/view/custom_views.rb
couch_potato-0.6.0 lib/couch_potato/view/custom_views.rb
couch_potato-0.5.7 lib/couch_potato/view/custom_views.rb
couch_potato-rails2-0.5.10 lib/couch_potato/view/custom_views.rb