Sha256: d70284e7ba88662fd0074b641854cc7a8207061595af559220bdc7a4e64b2697

Contents?: true

Size: 1.58 KB

Versions: 8

Compression:

Stored size: 1.58 KB

Contents

require File.dirname(__FILE__) + '/base_view_spec'
require File.dirname(__FILE__) + '/model_view_spec'
require File.dirname(__FILE__) + '/properties_view_spec'
require File.dirname(__FILE__) + '/custom_view_spec'
require File.dirname(__FILE__) + '/raw_view_spec'


module CouchPotato
  module View
    module CustomViews

      def self.included(base)
        base.extend ClassMethods
      end

      module ClassMethods
        # Declare a CouchDB view, for examples on how to use see the *ViewSpec classes in CouchPotato::View
        def views(view_name = nil)
          if view_name
            _find_view(view_name)
          else
            @views ||= {}
          end
        end

        def execute_view(view_name, view_parameters)
          view_spec_class(views(view_name)[:type]).new(self, view_name, views(view_name), view_parameters)
        end

        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)
          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)
          return @views[view] if @views && @views[view]
          superclass._find_view(view) if superclass && superclass.respond_to?(:_find_view)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
couch_potato-0.2.20 lib/couch_potato/view/custom_views.rb
couch_potato-0.2.19 lib/couch_potato/view/custom_views.rb
couch_potato-0.2.18 lib/couch_potato/view/custom_views.rb
couch_potato-0.2.17 lib/couch_potato/view/custom_views.rb
couch_potato-0.2.16 lib/couch_potato/view/custom_views.rb
couch_potato-0.2.15 lib/couch_potato/view/custom_views.rb
couch_potato-0.2.14 lib/couch_potato/view/custom_views.rb
couch_potato-0.2.13 lib/couch_potato/view/custom_views.rb