Class: RelaxDB::ViewCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxdb/views.rb

Class Method Summary collapse

Class Method Details

.all(kls) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/relaxdb/views.rb', line 5

def self.all(kls)
  class_name = kls[0]
  map = <<-QUERY
  function(doc) {        
    var class_match = #{kls_check kls}
    if (class_match) {
      emit(doc._id, 1);
    }
  }
  QUERY
        
  View.new "#{class_name}_all", map, "_sum"
end

.by_att_list(kls, *atts) ⇒ Object



23
24
25
# File 'lib/relaxdb/views.rb', line 23

def self.by_att_list(kls, *atts)
  create_by_att_list 1, "_sum", kls, *atts
end

.create_by_att_list(emit_val, reduce_func, kls, *atts) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/relaxdb/views.rb', line 27

def self.create_by_att_list emit_val, reduce_func, kls, *atts
  class_name = kls[0]
  key = atts.map { |a| "doc.#{a}" }.join(", ")
  key = atts.size > 1 ? key.sub(/^/, "[").sub(/$/, "]") : key
  prop_check = atts.map { |a| "doc.#{a} !== undefined" }.join(" && ")

  map = <<-QUERY
  function(doc) {
    var class_match = #{kls_check kls}
    if (class_match && #{prop_check}) {
      emit(#{key}, #{emit_val});
    }
  }
  QUERY
  
  view_name = "#{class_name}_by_" << atts.join("_and_")
  View.new view_name, map, reduce_func      
end

.docs_by_att_list(kls, *atts) ⇒ Object



19
20
21
# File 'lib/relaxdb/views.rb', line 19

def self.docs_by_att_list(kls, *atts)
  create_by_att_list "doc", "_count", kls, *atts
end

.kls_check(kls) ⇒ Object



46
47
48
49
# File 'lib/relaxdb/views.rb', line 46

def self.kls_check kls
  kls_names = kls.map{ |k| %Q("#{k}") }.join(",")
  "[#{kls_names}].indexOf(doc.relaxdb_class) >= 0;"
end