class CouchRest::Model::Designs::DesignMapper

github.com/couchrest/couchrest_model/blob/master/lib/couchrest/model/designs/view.rb overwritten

github.com/couchrest/couchrest_model/blob/master/lib/couchrest/model/designs/view.rb overwritten

Public Instance Methods

character_view(klass, name, opts={}) click to toggle source

generate a view to show only ThingTanks of a certain character, define them all in a ThingTank subclass (not in a character)

# File lib/couchrest/extensions/view.rb, line 6
  def character_view(klass, name, opts={})
    name = "#{klass.to_s.underscore.gsub('/', '_')}_#{name}"
    opts ||= {}
    opts[:guards] ||= []
    # there is no "inArray" like function in couchdb, see http://stackoverflow.com/questions/3740464/i-have-to-write-every-function-i-need-for-couchdb
    opts[:guards] << "((doc['characters'] !== undefined) && (function (item,arr) { for(p=0;p<arr.length;p++) if (item == arr[p]) return true; return false;})('#{klass.to_s}',doc['characters']))"
    if opts[:emit]
      # taken from # View#create and modified since there is no support for :emit
      opts[:allow_blank] = opts[:allow_blank].nil? ? true : opts[:allow_blank]
      opts[:guards] ||= []
      opts[:guards].push "(doc['#{model.model_type_key}'] == '#{model.to_s}')"
      opts[:map] = <<-EOF
        function(doc) {
          if (#{opts[:guards].join(' && ')}) {
            #{opts[:emit]}
          }
        }
      EOF
    end  
    view(name, opts)
  end
create_list_method(name) click to toggle source
# File lib/couchrest/extensions/list.rb, line 161
  def create_list_method(name)
    model.class_eval <<-EOS, __FILE__, __LINE__ + 1
      def self.list_#{name}(view_name, opts = {})
        CouchRest::Model::Designs::List.new(self, view_name, opts, '#{name}')
      end
    EOS
  end
list(name, function) click to toggle source
# File lib/couchrest/extensions/list.rb, line 156
def list(name, function)
  CouchRest::Model::Designs::List.create(model, name, function) if model.auto_update_design_doc
  create_list_method(name)
end