Module: Aqua::Store::CouchDB::StorageMethods::ClassMethods
- Defined in:
- lib/aqua/store/couch_db/storage_methods.rb
Instance Attribute Summary collapse
-
#parent_class ⇒ Object
(also: #design_name)
private
Accessor for maintaining connection to aquatic class.
Instance Method Summary collapse
-
#attachment(document_id, attachment_id) ⇒ Tempfile
Retrieves an attachment when provided the document id and attachment id, or the combined id.
- #average(index, opts = {}) ⇒ Object (also: #avg)
- #count(index, opts = {}) ⇒ Object
-
#create(hash) ⇒ Aqua::Storage, false
Initializes a new storage document and saves it without raising any errors.
-
#create!(hash) ⇒ Aqua::Storage
Initializes a new storage document and saves it raising any errors.
-
#database ⇒ Aqua::Database
Sets default database for class.
-
#database=(db) ⇒ Aqua::Database
Setter for the database per class.
-
#design_document(reload = false) ⇒ Object
Finds or creates design document based on aqua parent class name.
-
#find_or_create(id) ⇒ Hash
(also: #get!)
Will find a document by id, or create it if it doesn’t exist.
-
#get(id) ⇒ Hash
Gets a document from the database based on id.
-
#index_on(field, opts = {}) ⇒ Object
Stores stores a map name for a given index, allowing the same map to be used for various reduce functions.
-
#index_on_ivar(field) ⇒ Object
private
This is an aqua specific indexer whereas index_on is a more generic CouchDB indexer.
-
#indexes ⇒ Object
A list of index names that can be used to build other reduce functions.
- #max(index, opts = {}) ⇒ Object (also: #maximum)
- #min(index, opts = {}) ⇒ Object (also: #minimum)
- #query(index, opts = {}) ⇒ Object
- #reduced_query(reduce_type, index, opts) ⇒ Object
- #sum(index, opts = {}) ⇒ Object
- #views ⇒ Object
Instance Attribute Details
#parent_class ⇒ Object Also known as: design_name
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.
Accessor for maintaining connection to aquatic class
105 106 107 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 105 def parent_class @parent_class end |
Instance Method Details
#attachment(document_id, attachment_id) ⇒ Tempfile
Retrieves an attachment when provided the document id and attachment id, or the combined id
99 100 101 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 99 def ( document_id, ) new( :id => document_id )..get!( ) end |
#average(index, opts = {}) ⇒ Object Also known as: avg
209 210 211 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 209 def average( index, opts={} ) sum(index, opts) / count(index, opts).to_f end |
#count(index, opts = {}) ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 186 def count( index, opts={} ) opts = Mash.new(opts) opts[:reduce] = " function (key, values, rereduce) { return sum(values); }" unless opts[:reduce] reduced_query(:count, index, opts) end |
#create(hash) ⇒ Aqua::Storage, false
Initializes a new storage document and saves it without raising any errors
29 30 31 32 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 29 def create( hash ) doc = new( hash ) doc.save end |
#create!(hash) ⇒ Aqua::Storage
Initializes a new storage document and saves it raising any errors.
41 42 43 44 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 41 def create!( hash ) doc = new( hash ) doc.save! end |
#database ⇒ Aqua::Database
Look to CouchDB database strategy to determine if there is a database per class or just one big database for all classes
Sets default database for class. This can be overwritten by individual documents
53 54 55 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 53 def database @database ||= Database.create # defaults to 'aqua' end |
#database=(db) ⇒ Aqua::Database
Setter for the database per class. Used to override default per class or default strategy.
62 63 64 65 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 62 def database=( db ) db = Database.create( db ) if db.class == String @database = db end |
#design_document(reload = false) ⇒ Object
Finds or creates design document based on aqua parent class name
111 112 113 114 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 111 def design_document( reload=false ) @design_document = nil if reload @design_document ||= design_name ? DesignDocument.find_or_create( design_name ) : nil end |
#find_or_create(id) ⇒ Hash Also known as: get!
Will find a document by id, or create it if it doesn’t exist. Alias is :get!
84 85 86 87 88 89 90 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 84 def find_or_create( id ) begin get( id ) rescue create!( :id => id ) end end |
#get(id) ⇒ Hash
Gets a document from the database based on id
71 72 73 74 75 76 77 78 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 71 def get( id ) resource = begin # this is just in case the developer has already escaped the name CouchDB.get( "#{database.uri}/#{CGI.escape(id)}" ) rescue CouchDB.get( "#{database.uri}/#{id}" ) end new( resource ) end |
#index_on(field, opts = {}) ⇒ Object
Stores stores a map name for a given index, allowing the same map to be used for various reduce functions. This means only one index is created.
124 125 126 127 128 129 130 131 132 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 124 def index_on( field, opts={} ) opts = Mash.new( opts ) design_document(true).add!( opts.merge!(:name => field) ) unless indexes.include?( field ) indexes << field.to_sym indexes << field.to_s end self end |
#index_on_ivar(field) ⇒ 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.
This is an aqua specific indexer whereas index_on is a more generic CouchDB indexer. This method seeks out the designated ivar in an Aqua structured document. Future iterations should go deeper into the ivar to reduce the overall size of the index, and make for more usable searches.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 140 def index_on_ivar( field ) index_on( field, :map => " function(doc) { if( doc['class'] == '#{parent_class}' && doc['ivars'] && doc['ivars']['@#{field}'] ){ emit( doc['ivars']['@#{field}'], 1 ); } } " ) end |
#indexes ⇒ Object
A list of index names that can be used to build other reduce functions.
155 156 157 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 155 def indexes @indexes ||= [] end |
#max(index, opts = {}) ⇒ Object Also known as: maximum
230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 230 def max( index, opts={} ) opts = Mash.new(opts) opts[:reduce] = " function (keys, values, rereduce) { var key_values = [] keys.forEach( function(key) { key_values[key_values.length] = key[0] }); return Math.max.apply( Math, key_values ); ; }" unless opts[:reduce] reduced_query(:max, index, opts) end |
#min(index, opts = {}) ⇒ Object Also known as: minimum
215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 215 def min( index, opts={} ) opts = Mash.new(opts) opts[:reduce] = " function (keys, values, rereduce) { var key_values = [] keys.forEach( function(key) { key_values[key_values.length] = key[0] }); return Math.min.apply( Math, key_values ); ; }" unless opts[:reduce] reduced_query(:min, index, opts) end |
#query(index, opts = {}) ⇒ Object
160 161 162 163 164 165 166 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 160 def query( index, opts={} ) raise ArgumentError, 'Index not found' unless views.include?( index.to_s ) opts = Mash.new(opts) opts.merge!(:document_class => self) unless opts[:document_class] opts.merge!(:reduced => design_document.views[index][:reduce] ? true : false ) design_document.query( index, opts ) end |
#reduced_query(reduce_type, index, opts) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 168 def reduced_query( reduce_type, index, opts) view = "#{index}_#{reduce_type}" unless views.include?( view ) design_document(true).add!( :name => view, :map => design_document.views[ index.to_s ][:map], :reduce => opts[:reduce] ) end query( view, opts.merge!( :select => "index only" ) ) end |
#sum(index, opts = {}) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 196 def sum( index, opts={} ) opts = Mash.new(opts) opts[:reduce] = " function (keys, values, rereduce) { var key_values = [] keys.forEach( function(key) { key_values[key_values.length] = key[0] }); return sum( key_values ); }" unless opts[:reduce] reduced_query(:sum, index, opts) end |
#views ⇒ Object
181 182 183 |
# File 'lib/aqua/store/couch_db/storage_methods.rb', line 181 def views design_document.views.keys end |