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, doc);
    }
  }
  QUERY
        
  View.new "#{class_name}_all", map, sum_reduce_func      
end

.by_att_list(kls, *atts) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/relaxdb/views.rb', line 19

def self.by_att_list(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}, doc);
    }
  }
  QUERY
  
  view_name = "#{class_name}_by_" << atts.join("_and_")
  View.new view_name, map, sum_reduce_func
end

.has_n(client_class, relationship, target_class, relationship_to_client) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/relaxdb/views.rb', line 39

def self.has_n(client_class, relationship, target_class, relationship_to_client)
  map = <<-QUERY
    function(doc) {
      if (doc.relaxdb_class == "#{target_class}" && doc.#{relationship_to_client}_id)
        emit(doc.#{relationship_to_client}_id, doc);
    }
  QUERY
  
  view_name = "#{client_class}_#{relationship}"
  View.new view_name, map
end

.kls_check(kls) ⇒ Object



67
68
69
70
# File 'lib/relaxdb/views.rb', line 67

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

.references_many(client_class, relationship, target_class, peers) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/relaxdb/views.rb', line 51

def self.references_many(client_class, relationship, target_class, peers)
  map = <<-QUERY
    function(doc) {
      if (doc.relaxdb_class == "#{target_class}" && doc.#{peers}) {
        var i;
        for(i = 0; i < doc.#{peers}.length; i++) {
          emit(doc.#{peers}[i], doc);
        }
      }
    }
  QUERY
  
  view_name = "#{client_class}_#{relationship}"
  View.new view_name, map
end

.sum_reduce_funcObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/relaxdb/views.rb', line 72

def self.sum_reduce_func
  <<-QUERY
  function(keys, values, rereduce) {
    if (rereduce) {
      return sum(values);
    } else {
      return values.length;
    }
  }
  QUERY
end