Class: ArangoDb::Base

Inherits:
Object
  • Object
show all
Extended by:
Collections::ClassMethods, Indices::ClassMethods, Queries::ClassMethods
Includes:
Properties
Defined in:
lib/arangodb-odm.rb

Overview

Base class for ArangoDB documents. Subclass it to create your own collection specific document representations.

Example:

class ExampleDocument < ArangoDb::Base

collection :examples
skiplist [:a, :b]

end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Collections::ClassMethods

create_collection

Methods included from Queries::ClassMethods

all, attribute, closed, first, left, limit, right, skip, where

Methods included from Indices::ClassMethods

create_skiplist, ensure_indices

Methods included from Properties

included

Constructor Details

#initializeBase

Returns a new instance of Base.



147
148
149
150
151
152
153
# File 'lib/arangodb-odm.rb', line 147

def initialize
  @transport = self.class.transport
  @target = self.class.target.new(self.class.collection, self.class.db_attributes)
  if self.class.respond_to?(:skiplist) and self.class.skiplist
    @index = ArangoDb::Index.new(:skiplist => self.class.skiplist)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)

Delegates all unknown method invocations to the target.



245
246
247
248
249
250
251
252
253
254
# File 'lib/arangodb-odm.rb', line 245

def method_missing(method, *args, &block)
  method = method.to_s
  if method[-1, 1] == '='
    @target.attributes[method.gsub('=', '')] = args.first unless ['_id=', '_rev='].include?(method)
  else
    val = @target.attributes[method]
    val = @target.send(method.to_sym) rescue nil unless val
    val
  end
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



145
146
147
# File 'lib/arangodb-odm.rb', line 145

def index
  @index
end

#targetObject (readonly)

Returns the value of attribute target.



145
146
147
# File 'lib/arangodb-odm.rb', line 145

def target
  @target
end

Class Method Details

.create(attributes = {}) ⇒ Object



166
167
168
169
170
# File 'lib/arangodb-odm.rb', line 166

def self.create(attributes = {})
  document = self.new.build(attributes)
  document.save
  document
end

.find(document_handle) ⇒ Object



155
156
157
158
159
# File 'lib/arangodb-odm.rb', line 155

def self.find(document_handle)
  raise "missing document handle" if document_handle.nil?
  res = transport.get("/_api/document/#{document_handle}")
  res.code == 200 ? self.new.build(res.parsed_response) : nil
end

.keysObject



161
162
163
164
# File 'lib/arangodb-odm.rb', line 161

def self.keys
  res = transport.get("/_api/document?collection=#{collection}")
  res.code == 200 ? res.parsed_response['documents'] : []
end

Instance Method Details

#build(attributes = {}) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/arangodb-odm.rb', line 172

def build(attributes = {})
  attributes.each {|k, v| self.send("#{k}=".to_sym, v) unless ['_id', '_rev'].include?(k)}
  self.target._id = attributes['_id'] if attributes['_id'] 
  self.target._rev = attributes['_rev'] if attributes['_rev']
  self.target.location = "/_api/document/#{self.target._id}" if attributes['_id']
  self
end

#changed?Boolean

Returns:

  • (Boolean)


214
215
216
217
218
219
220
# File 'lib/arangodb-odm.rb', line 214

def changed?
  unless @target.is_new?
    res = @transport.head(@target.location)
    return (self._rev.to_s.gsub("\"", '') != res.headers['etag'].to_s.gsub("\"", ''))
  end
  false
end

#destroyObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/arangodb-odm.rb', line 222

def destroy
  unless is_new?
    if self.respond_to?(:before_destroy) and self.before_destroy
      self.send(self.before_destroy.to_sym)
    end
    res = @transport.delete(@target.location)
    if res.code == 200
      @target.location = nil; @target._id = nil; @target._rev = nil
      if self.respond_to?(:after_destroy) and self.after_destroy
        self.send(self.after_destroy.to_sym)
      end
      return true
    end
  end
  false
end

#saveObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/arangodb-odm.rb', line 180

def save
  if validate
    if @target.is_new?
      if self.respond_to?(:before_create) and self.before_create
        self.send(self.before_create.to_sym)
      end
      res = @transport.post("/_api/document/?collection=#{@target.collection}&createCollection=true", :body => to_json)
      if res.code == 201 || res.code == 202
        @target.location = res.headers["location"]
        @target._id = res.parsed_response["_id"]
        @target._rev = res.headers["etag"]
        if self.respond_to?(:after_create) and self.after_create
          self.send(self.after_create.to_sym)
        end
        return @target._id
      end
    else
      if self.respond_to?(:before_save) and self.before_save
        self.send(self.before_save.to_sym)
      end
      res = @transport.put(@target.location, :body => to_json)
      @target._rev = res.parsed_response['_rev']
      if self.respond_to?(:after_save) and self.after_save
        self.send(self.after_save.to_sym)
      end
      return @target._rev
    end
  end
  nil
end

#to_jsonObject



239
240
241
# File 'lib/arangodb-odm.rb', line 239

def to_json
  @target.to_json
end

#validateObject

Override to run your own validations.



212
# File 'lib/arangodb-odm.rb', line 212

def validate; true; end