Class: RelaxDB::ViewCreator
- Inherits:
-
Object
- Object
- RelaxDB::ViewCreator
- Defined in:
- lib/relaxdb/views.rb
Class Method Summary collapse
- .all(kls) ⇒ Object
- .by_att_list(kls, *atts) ⇒ Object
- .create_by_att_list(emit_val, reduce_func, kls, *atts) ⇒ Object
- .docs_by_att_list(kls, *atts) ⇒ Object
- .kls_check(kls) ⇒ Object
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 = " function(doc) { \n var class_match = \#{kls_check kls}\n if (class_match) {\n emit(doc._id, 1);\n }\n }\n QUERY\n \n View.new \"\#{class_name}_all\", map, \"_sum\"\nend\n" |
.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 = " function(doc) {\n var class_match = \#{kls_check kls}\n if (class_match && \#{prop_check}) {\n emit(\#{key}, \#{emit_val});\n }\n }\n QUERY\n \n view_name = \"\#{class_name}_by_\" << atts.join(\"_and_\")\n View.new view_name, map, reduce_func \nend\n" |
.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 |