Class: RelaxDB::ViewCreator
- Inherits:
-
Object
- Object
- RelaxDB::ViewCreator
- Defined in:
- lib/relaxdb/views.rb
Class Method Summary collapse
- .all(target_class) ⇒ Object
- .has_many_through(target_class, peers) ⇒ Object
- .has_n(target_class, relationship_to_client) ⇒ Object
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 = " function(doc) {\n if(doc.class == \"${target_class}\")\n emit(null, doc);\n }\n QUERY\n map.sub!(\"${target_class}\", target_class.to_s)\n \n reduce = <<-QUERY\n function(keys, values, rereduce) {\n if (rereduce) {\n return sum(values);\n } else {\n return values.length;\n }\n }\n QUERY\n \n [map, reduce] \nend\n" |
.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 = " function(doc) {\n if(doc.class == \"${target_class}\" && doc.${peers}) {\n var i;\n for(i = 0; i < doc.${peers}.length; i++) {\n emit(doc.${peers}[i], doc);\n }\n }\n }\n MAP_FUNC\n template.sub!(\"${target_class}\", target_class).gsub!(\"${peers}\", peers)\nend\n" |
.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 = " function(doc) {\n if(doc.class == \"${target_class}\")\n emit(doc.${relationship_to_client}_id, doc);\n }\n MAP_FUNC\n template.sub!(\"${target_class}\", target_class)\n template.sub(\"${relationship_to_client}\", relationship_to_client)\nend\n" |