Class: CouchRest::Model::Designs::DesignMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/couchrest/extensions/list.rb,
lib/couchrest/extensions/view.rb

Overview

Instance Method Summary collapse

Instance Method Details

#character_view(klass, name, opts = {}) ⇒ Object

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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) ⇒ Object



161
162
163
164
165
166
167
# 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) ⇒ Object



156
157
158
159
# 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