Class: CouchRest::Design

Inherits:
Document show all
Defined in:
lib/couchrest/design.rb

Instance Attribute Summary

Attributes inherited from Document

#database

Instance Method Summary (collapse)

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 included from Attributes

#[], #[]=, #as_couch_json, #clone, #delete, #dup, #freeze, #has_key?, #initialize, #inspect, #to_hash

Instance Method Details

- (Boolean) can_reduce_view?(name)

Check if the view has a reduce method defined.

Returns:

  • (Boolean)


80
81
82
# File 'lib/couchrest/design.rb', line 80

def can_reduce_view?(name)
  has_view?(name) && !self['views'][name.to_s]['reduce'].to_s.empty?
end

- (Boolean) has_view?(name)

Returns true or false if the view is available.

Returns:

  • (Boolean)


75
76
77
# File 'lib/couchrest/design.rb', line 75

def has_view?(name)
  !self['views'][name.to_s].nil?
end

- (Object) name



55
56
57
# File 'lib/couchrest/design.rb', line 55

def name
  id.sub('_design/','') if id
end

- (Object) name=(newname)



59
60
61
# File 'lib/couchrest/design.rb', line 59

def name= newname
  self['_id'] = "_design/#{newname}"
end

- (Object) save

Raises:

  • (ArgumentError)


63
64
65
66
# File 'lib/couchrest/design.rb', line 63

def save
  raise ArgumentError, "_design docs require a name" unless name && name.length > 0
  super
end

- (Object) view(view_name, query = {}, &block)

Dispatches to any named view. (using the database where this design doc was saved)



38
39
40
# File 'lib/couchrest/design.rb', line 38

def view view_name, query={}, &block
  view_on database, view_name, query, &block
end

- (Object) view_by(*keys)



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
# 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)
    view['reduce'] = opts.delete(:reduce) if opts[:reduce]
    self['views'][method_name] = view
  else
    doc_keys = keys.collect{|k| "doc['#{k}']"}
    key_emit = doc_keys.length == 1 ? "#{doc_keys.first}" : "[#{doc_keys.join(', ')}]"
    guards = opts.delete(:guards) || []
    guards += doc_keys.map{|k| "(#{k} != null)"} unless opts.delete(:allow_nil)
    guards << 'true' if guards.empty?
    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

- (Object) view_defaults(name)

Return the hash of default values to include in all queries sent to a view from couchrest.



70
71
72
# File 'lib/couchrest/design.rb', line 70

def view_defaults(name)
  (self['views'][name.to_s] && self['views'][name.to_s]["couchrest-defaults"]) || {}
end

- (Object) view_on(db, view_name, query = {}, &block)

Dispatches to any named view in a specific database

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/couchrest/design.rb', line 43

def view_on db, view_name, query = {}, &block
  raise ArgumentError, "View query options must be set as symbols!" if query.keys.find{|k| k.is_a?(String)}
  view_name = view_name.to_s
  view_slug = "#{name}/#{view_name}"
  # Set the default query options
  query = view_defaults(view_name).merge(query)
  # Ensure reduce is set if dealing with a reduceable view
  # This is a requirement of CouchDB.
  query[:reduce] ||= false if can_reduce_view?(view_name)
  db.view(view_slug, query, &block)
end