Class: CouchRest::Design
- Defined in:
- lib/couchrest/design.rb
Instance Attribute Summary
Attributes inherited from Document
Attributes inherited from Response
Instance Method Summary collapse
- #name ⇒ Object
- #name=(newname) ⇒ Object
- #save ⇒ Object
-
#view(view_name, query = {}, &block) ⇒ Object
Dispatches to any named view.
- #view_by(*keys) ⇒ Object
-
#view_on(db, view_name, query = {}, &block) ⇒ Object
Dispatches to any named view in a specific database.
Methods inherited from Document
#copy, #destroy, #id, #id=, #new?, #rev, #uri, use_database
Methods included from InheritableAttributes
#couchrest_inheritable_accessor, #couchrest_inheritable_reader, #couchrest_inheritable_writer
Methods included from Attachments
#delete_attachment, #fetch_attachment, #put_attachment
Methods inherited from Response
#[], #[]=, #delete, #initialize
Constructor Details
This class inherits a constructor from CouchRest::Response
Instance Method Details
#name ⇒ Object
52 53 54 |
# File 'lib/couchrest/design.rb', line 52 def name id.sub('_design/','') if id end |
#name=(newname) ⇒ Object
56 57 58 |
# File 'lib/couchrest/design.rb', line 56 def name= newname self['_id'] = "_design/#{newname}" end |
#save ⇒ Object
60 61 62 63 |
# File 'lib/couchrest/design.rb', line 60 def save raise ArgumentError, "_design docs require a name" unless name && name.length > 0 super end |
#view(view_name, query = {}, &block) ⇒ Object
Dispatches to any named view. (using the database where this design doc was saved)
40 41 42 |
# File 'lib/couchrest/design.rb', line 40 def view view_name, query={}, &block view_on database, view_name, query, &block end |
#view_by(*keys) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/couchrest/design.rb', line 4 def view_by *keys opts = keys.pop if keys.last.is_a?(Hash) opts ||= {} self['views'] ||= {} method_name = "by_#{keys.join('_and_')}" if opts[:map] view = {} view['map'] = opts.delete(:map) if opts[:reduce] view['reduce'] = opts.delete(:reduce) opts[:reduce] = false end self['views'][method_name] = view else doc_keys = keys.collect{|k|"doc['#{k}']"} # this is where :require => 'doc.x == true' would show up key_emit = doc_keys.length == 1 ? "#{doc_keys.first}" : "[#{doc_keys.join(', ')}]" guards = opts.delete(:guards) || [] guards += doc_keys.map{|k| "(#{k} != null)"} map_function = <<-JAVASCRIPT function(doc) { if (#{guards.join(' && ')}) { emit(#{key_emit}, null); } } JAVASCRIPT self['views'][method_name] = { 'map' => map_function } end self['views'][method_name]['couchrest-defaults'] = opts unless opts.empty? method_name end |
#view_on(db, view_name, query = {}, &block) ⇒ Object
Dispatches to any named view in a specific database
45 46 47 48 49 50 |
# File 'lib/couchrest/design.rb', line 45 def view_on db, view_name, query={}, &block view_name = view_name.to_s view_slug = "#{name}/#{view_name}" defaults = (self['views'][view_name] && self['views'][view_name]["couchrest-defaults"]) || {} db.view(view_slug, defaults.merge(query), &block) end |