Module: Arango::Document::ClassMethods
- Defined in:
- lib/arango/document/class_methods.rb
Overview
Arango Document ClassMethods
Instance Method Summary collapse
- #all(offset: 0, limit: nil, batch_size: nil, collection:) ⇒ Object
- #create_documents(documents, wait_for_sync: nil, collection:) ⇒ Object
- #delete(key: nil, attributes: {}, ignore_revs: false, wait_for_sync: nil, collection:) ⇒ Object
- #delete_documents(documents, ignore_revs: false, wait_for_sync: nil, collection:) ⇒ Object
- #exists?(key: nil, attributes: {}, match_rev: nil, collection:) ⇒ Boolean
- #get(key: nil, attributes: {}, collection: nil, database: nil) ⇒ Object
-
#get_documents(attributes, collection:) ⇒ Object
get documents by attribute(s).
- #list(offset: 0, limit: nil, batch_size: nil, collection:) ⇒ Object
- #replace_documents(documents, ignore_revs: false, wait_for_sync: nil, collection:) ⇒ Object
- #update_documents(documents, ignore_revs: false, wait_for_sync: nil, merge_objects: nil, collection:) ⇒ Object
Instance Method Details
#all(offset: 0, limit: nil, batch_size: nil, collection:) ⇒ Object
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 37 38 39 40 |
# File 'lib/arango/document/class_methods.rb', line 9 def all (offset: 0, limit: nil, batch_size: nil, collection:) bind_vars = {} query = "FOR doc IN #{collection.name}" if limit && offset query << "\n LIMIT @offset, @limit" bind_vars[:offset] = offset bind_vars[:limit] = limit end raise Arango::Error.new "offset must be used with limit" if offset > 0 && !limit query << "\n RETURN doc" body = { query: query } unless bind_vars.empty? body[:bind_vars] = bind_vars end if batch_size body[:batch_size] = batch_size end result = Arango::Requests::Cursor::Create.execute(server: collection.server, body: body) result_proc = ->(result) { result.result.map { |doc_attr| Arango::Document::Base.new(attributes: doc_attr, collection: collection) }} final_result = result_proc.call(result) if result[:has_more] collection.instance_variable_set(:@cursor, result) collection.instance_variable_set(:@batch_proc, result_proc) unless batch_size while collection.has_more? b = collection.next_batch final_result += b if b end end end final_result end |
#create_documents(documents, wait_for_sync: nil, collection:) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/arango/document/class_methods.rb', line 93 def create_documents (documents, wait_for_sync: nil, collection:) documents = [documents] unless documents.is_a? Array documents = documents.map{ |d| _attributes_from_arg(d) } params = { returnNew: true } params[:waitForSync] = wait_for_sync unless wait_for_sync.nil? args = { collection: collection.name } result = Arango::Requests::Document::CreateMultiple.execute(server: collection.server, args: args, params: params, body: documents) result.map do |doc| # returnNew does not work for 'multiple' Arango::Document::Base.get(key: doc[:_key], collection: collection) end end |
#delete(key: nil, attributes: {}, ignore_revs: false, wait_for_sync: nil, collection:) ⇒ Object
199 200 201 202 203 204 205 206 207 208 |
# File 'lib/arango/document/class_methods.rb', line 199 def delete (key: nil, attributes: {}, ignore_revs: false, wait_for_sync: nil, collection:) document = _attributes_from_arg(attributes) document[:_key] = key if key params = { } params[:waitForSync] = wait_for_sync unless wait_for_sync.nil? headers = nil headers = { "If-Match": document[:_rev] } if !ignore_revs && document.key?(:_rev) args = {collection: collection.name, key: document[:_key]} Arango::Requests::Document::Delete.execute(server: collection.server, args: args, headers: headers, params: params) end |
#delete_documents(documents, ignore_revs: false, wait_for_sync: nil, collection:) ⇒ Object
210 211 212 213 214 215 216 217 |
# File 'lib/arango/document/class_methods.rb', line 210 def delete_documents (documents, ignore_revs: false, wait_for_sync: nil, collection:) documents = [documents] unless documents.is_a? Array documents = documents.map{ |d| _attributes_from_arg(d) } params = { } params[:waitForSync] = wait_for_sync unless wait_for_sync.nil? args = {collection: collection.name } Arango::Requests::Document::DeleteMultiple.execute(server: collection.server, args: args, params: params, body: documents) end |
#exists?(key: nil, attributes: {}, match_rev: nil, collection:) ⇒ Boolean
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/arango/document/class_methods.rb', line 75 def exists? (key: nil, attributes: {}, match_rev: nil, collection:) document = _attributes_from_arg(attributes) document[:_key] = key if key headers = { } if document.key?(:_key) && document.key?(:_rev) && match_rev == true headers[:'If-Match'] = document[:_rev] elsif document.key?(:_key) && document.key?(:_rev) && match_rev == false headers[:'If-None-Match'] = document[:_rev] end args = { collection: collection.name, key: document[:_key] } begin Arango::Requests::Document::Head.execute(server: collection.server, args: args, headers: headers) rescue Error return false end true end |
#get(key: nil, attributes: {}, collection: nil, database: nil) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/arango/document/class_methods.rb', line 106 def get (key: nil, attributes: {}, collection: nil, database: nil) document = _attributes_from_arg(attributes) document[:_key] = key if key if document.key?(:_key) args = {collection: collection.name, key: document[:_key] } result = Arango::Requests::Document::Get.execute(server: collection.server, args: args) Arango::Document::Base.new(attributes: result, collection: collection) else bind_vars = {} query = "FOR doc IN #{collection.name}" i = 0 document.each do |k,v| i += 1 query << "\n FILTER doc.@key#{i} == @value#{i}" bind_vars["key#{i}"] = k.to_s bind_vars["value#{i}"] = v end query << "\n LIMIT 1" query << "\n RETURN doc" database = collection.database unless database aql = AQL.new(query: query, database: database, bind_vars: bind_vars, block: ->(_, result) do Arango::Document::Base.new(attributes: result.result.first, collection: collection) if result.result.first end ) aql.request end end |
#get_documents(attributes, collection:) ⇒ Object
get documents by attribute(s)
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/arango/document/class_methods.rb', line 137 def get_documents (attributes, collection:) attributes = [attributes] unless attributes.is_a? Array attributes = attributes.map{ |a| _attributes_from_arg(a) } results = [] args = { collection: collection.name } attributes.each do |attribute| if attribute.key?(:_key) args[:key] = attribute[:_key] result = Arango::Requests::Document::Get.execute(server: collection.server, args: args) results << Arango::Document::Base.new(attributes: result, collection: collection) else bind_vars = {} query = "FOR doc IN #{collection.name}" i = 0 attribute.each do |k,v| i += 1 query << "\n FILTER doc.@key#{i} == @value#{i}" bind_vars["key#{i}"] = k.to_s bind_vars["value#{i}"] = v end query << "\n RETURN doc" aql = AQL.new(query: query, database: collection.database, bind_vars: bind_vars, block: ->(_, result) do documents = [] result.result.each do |r| documents << Arango::Document::Base.new(attributes: r, collection: collection) end documents end ) results += aql.request end end results end |
#list(offset: 0, limit: nil, batch_size: nil, collection:) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/arango/document/class_methods.rb', line 42 def list (offset: 0, limit: nil, batch_size: nil, collection:) bind_vars = {} query = "FOR doc IN #{collection.name}" if limit && offset query << "\n LIMIT @offset, @limit" bind_vars[:offset] = offset bind_vars[:limit] = limit end raise Arango::Error.new "offset must be used with limit" if offset && offset.to_i > 0 && !limit query << "\n RETURN doc._key" args = { db: collection.database.name } body = { query: query } unless bind_vars.empty? body[:bind_vars] = bind_vars end if batch_size body[:batch_size] = batch_size end result = Arango::Requests::Cursor::Create.execute(server: collection.server, args: args, body: body) result_proc = ->(b) { b.result } final_result = result_proc.call(result) if result[:has_more] collection.instance_variable_set(:@cursor, result) collection.instance_variable_set(:@batch_proc, result_proc) unless batch_size while result[:has_more] final_result += collection.next_batch end end end final_result end |
#replace_documents(documents, ignore_revs: false, wait_for_sync: nil, collection:) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/arango/document/class_methods.rb', line 174 def replace_documents (documents, ignore_revs: false, wait_for_sync: nil, collection:) documents = [documents] unless documents.is_a? Array documents = documents.map{ |d| _attributes_from_arg(d) } params = { returnNew: true, ignoreRevs: ignore_revs } params[:waitForSync] = wait_for_sync unless wait_for_sync.nil? args = { collection: collection.name } result = Arango::Requests::Document::ReplaceMultiple.execute(server: @server, args: args, params: params, body: documents) result.map do |doc| Arango::Document::Base.new(attributes: doc[:new], collection: collection) end end |
#update_documents(documents, ignore_revs: false, wait_for_sync: nil, merge_objects: nil, collection:) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/arango/document/class_methods.rb', line 186 def update_documents (documents, ignore_revs: false, wait_for_sync: nil, merge_objects: nil, collection:) documents = [documents] unless documents.is_a? Array documents = documents.map{ |d| _attributes_from_arg(d) } params = { returnNew: true, ignoreRevs: ignore_revs } params[:waitForSync] = wait_for_sync unless wait_for_sync.nil? params[:mergeObjects] = merge_objects unless merge_objects.nil? args = { collection: collection.name } result = Arango::Requests::Document::UpdateMultiple.execute(server: @server, args: args, params: params, body: documents) result.map do |doc| Arango::Document::Base.new(attributes: doc[:new], collection: collection) end end |