Class: Aqua::Store::CouchDB::DesignDocument

Inherits:
Mash
  • Object
show all
Includes:
StorageMethods
Defined in:
lib/aqua/store/couch_db/design_document.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StorageMethods

included

Constructor Details

#initialize(hash = {}) ⇒ DesignDocument

Returns a new instance of DesignDocument.



31
32
33
34
35
# File 'lib/aqua/store/couch_db/design_document.rb', line 31

def initialize( hash={} )
  hash = Mash.new( hash ) unless hash.empty?
  self.id = hash.delete(:name) if hash[:name]
  document_initialize( hash )  # TODO: can't this just be a call to super?
end

Class Method Details

.get(name) ⇒ Aqua::Store::CouchDB::DesignDocument

Gets a design document by name.

Parameters:

  • Id/Name (String)

    of design document

Returns:



66
67
68
69
# File 'lib/aqua/store/couch_db/design_document.rb', line 66

def self.get( name )
  design = CouchDB.get( "#{database.uri}/_design/#{CGI.escape(name)}" )
  new( design )
end

Instance Method Details

#<<(arg) ⇒ Mash Also known as: add

Adds or updates a view with the given options

Examples:

design_doc << 'attribute_name'
design_doc << {:name => 'attribute_name', :map => 'function(doc){ ... }'}

Parameters:

  • Name (String, Hash)

    of the view, or options hash

  • arg (Hash)

    a customizable set of options

Options Hash (arg):

  • :name (String)

    The view name, required

  • :map (String)

    Javascript map function, optional

  • :reduce (String)

    Javascript reduce function, optional

Returns:

  • (Mash)

    Map/Reduce mash of javascript functions



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/aqua/store/couch_db/design_document.rb', line 94

def <<( arg )
  # handle different argument options
  if [String, Symbol].include?( arg.class )
    view_name = arg
    opts = {}
  elsif arg.class.ancestors.include?( Hash )
    opts = Mash.new( arg )
    view_name = opts.delete( :name )
    raise ArgumentError, 'Option must include a :name that is the view\'s name' unless view_name
  else
    raise ArgumentError, "Must be a string or Hash like object of options"    
  end
  
  # build the map/reduce query  
  map =     opts[:map]
  reduce =  opts[:reduce]
  views # to initialize self[:views]
  self[:views][view_name] = { 
    :map => map || build_map( view_name, opts[:class_constraint] ), 
  }
  self[:views][view_name][:reduce] = reduce if reduce
  self[:views][view_name]
end

#add!(arg) ⇒ Object



120
121
122
123
# File 'lib/aqua/store/couch_db/design_document.rb', line 120

def add!( arg ) 
  self << arg 
  save!
end

#do_rev(hash) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/aqua/store/couch_db/design_document.rb', line 37

def do_rev( hash )
  # TODO: This is a temp hack to deal with loading the right revision number so a design doc
  # can be updated from the document. Without this hack, the rev is nil, and there is a conflict.

  hash.delete(:rev)   # This is omited to aleviate confusion
  # hash.delete(:_rev)  # CouchDB determines _rev attribute
end

#document_initializeObject



29
# File 'lib/aqua/store/couch_db/design_document.rb', line 29

alias :document_initialize :initialize

#nameString

In the design document the name is the same as the id. That way initialization can include a name parameter, which will change the id, and therefore the address of the document. This method returns the id.

Returns:

  • (String)

    id for document



17
18
19
# File 'lib/aqua/store/couch_db/design_document.rb', line 17

def name
  id
end

#name=(n) ⇒ String

Sets the id and is an alias for id=.

Parameters:

  • Unique (String)

    identifier

Returns:

  • (String)

    Escaped identifier



25
26
27
# File 'lib/aqua/store/couch_db/design_document.rb', line 25

def name=( n )
  self.id = ( n )
end

#query(view_name, opts = {}) ⇒ Object

group=true Version 0.8.0 and forward group_level=int reduce=false Trunk only (0.9)



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/aqua/store/couch_db/design_document.rb', line 150

def query( view_name, opts={} )
  opts = Mash.new( opts ) unless opts.empty? 
  doc_class = opts[:document_class] 
  
  params = []
  params << 'include_docs=true' unless (opts[:select] && opts[:select] != 'all')
  # TODO: this is according to couchdb really inefficent with large sets of data.
  # A better way would involve, using start and end keys with limit. But this 
  # is a really hard one to figure with jumping around to different pages
  params << "skip=#{opts[:offset]}" if opts[:offset]
  params << "limit=#{opts[:limit]}" if opts[:limit]
  params << "key=#{opts[:equals]}" if opts[:equals] 
  if opts[:order].to_s == 'desc' || opts[:order].to_s == 'descending'
    desc = true
    params << "descending=true"
  end 
  if opts[:range] && opts[:range].size == 2
    params << "startkey=#{opts[:range][desc == true ? 1 : 0 ]}"  
    params << "endkey=#{opts[:range][desc == true ? 0 : 1]}"   
  end   
  
  query_uri = "#{uri}/_view/#{CGI.escape(view_name.to_s)}?"
  query_uri << params.join('&')
  
  result = CouchDB.get( query_uri )
  opts[:reduced] ? result['rows'].first['value'] : ResultSet.new( result, doc_class )
end

#update_version(result) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Updates the id and rev after a design document is successfully saved. The _design/ portion of the id has to be stripped.

Parameters:

  • Result (Hash)

    returned by CouchDB document save



57
58
59
60
# File 'lib/aqua/store/couch_db/design_document.rb', line 57

def update_version( result ) 
  self.id     = result['id'].gsub(/\A_design\//, '')
  self.rev    = result['rev']
end

#uriString

couchdb database url for the design document

Returns:

  • (String)

    representing CouchDB uri for document

Raises:

  • (ArgumentError)


48
49
50
51
# File 'lib/aqua/store/couch_db/design_document.rb', line 48

def uri
  raise ArgumentError, 'DesignDocument must have a name' if name.nil? || name.empty?
  database.uri + '/_design/' + name
end

#viewsArray

An array of indexed views for the design document.

Returns:



76
77
78
# File 'lib/aqua/store/couch_db/design_document.rb', line 76

def views
  self[:views] ||= Mash.new
end