Class: RelaxDB::ViewCreator

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

Class Method Summary collapse

Class Method Details

.all(target_class) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/relaxdb/views.rb', line 5

def self.all(target_class)
  map = <<-QUERY
  function(doc) {
    if(doc.class == "${target_class}")
      emit(null, doc);
  }
  QUERY
  map.sub!("${target_class}", target_class.to_s)
  
  reduce = <<-QUERY
  function(keys, values, rereduce) {
    if (rereduce) {
      return sum(values);
    } else {
      return values.length;
    }
  }
  QUERY
  
  [map, reduce]  
end

.has_many_through(target_class, peers) ⇒ Object



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

def self.has_many_through(target_class, peers)
  template = <<-MAP_FUNC
    function(doc) {
      if(doc.class == "${target_class}" && doc.${peers}) {
        var i;
        for(i = 0; i < doc.${peers}.length; i++) {
          emit(doc.${peers}[i], doc);
        }
      }
    }
  MAP_FUNC
  template.sub!("${target_class}", target_class).gsub!("${peers}", peers)
end

.has_n(target_class, relationship_to_client) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/relaxdb/views.rb', line 27

def self.has_n(target_class, relationship_to_client)
  template = <<-MAP_FUNC
  function(doc) {
    if(doc.class == "${target_class}" && doc.${relationship_to_client}_id)
      emit(doc.${relationship_to_client}_id, doc);
  }
  MAP_FUNC
  template.sub!("${target_class}", target_class)
  template.gsub("${relationship_to_client}", relationship_to_client)
end