class Ashikawa::Core::Document

A certain Document within a certain Collection

Attributes

graph[R]

The optional graph this document belongs to

@return [Graph] The Graph instance the document was fetched from @api public

id[R]

The ID of the document (this includes the Collection prefix)

@return [String] @api public @example Get the ID for a Document

document = Ashikawa::Core::Document.new(database, raw_document)
document.id # => 'my_fancy_collection/2345678'
key[R]

The key of the document (No collection prefix)

@return [String] @api public @example Get the key for a Document

document = Ashikawa::Core::Document.new(database, raw_document)
document.key # => '2345678'
revision[R]

The current revision of the document

@return [String] @api public @example Get the Revision for a Document

document = Ashikawa::Core::Document.new(database, raw_document)
document.revision # => 3456789

Public Class Methods

new(database, raw_document, additional_data = {}) click to toggle source

Initialize a Document with the database and raw data

@param [Database] database @param [Hash] raw_document @param [Hash] additional_data @option _additional_data [Graph] graph The graph this document is associated with @api public @example Create a document

document = Ashikawa::Core::Document.new(database, raw_document)
# File lib/ashikawa-core/document.rb, line 52
def initialize(database, raw_document, additional_data = {})
  @database = database
  @graph    = additional_data.delete(:graph)

  raw_document.merge!(clean_up_additional_data(additional_data))
  parse_raw_document(raw_document)
end

Public Instance Methods

[](attribute_name) click to toggle source

Get the value of an attribute of the document

@param [String] attribute_name @return [Object] The value of the attribute @api public @example Get the name attribute of a document

document = Ashikawa::Core::Document.new(database, raw_document)
document['name'] #=> 'Lebowski'
# File lib/ashikawa-core/document.rb, line 80
def [](attribute_name)
  @content[attribute_name]
end
[]=(attribute_name, value) click to toggle source

Update the value of an attribute (Does not write to database)

@param [String] attribute_name @param [Object] value @return [Object] The value @api public @example Change the name attribute of a document

document = Ashikawa::Core::Document.new(database, raw_document)
document['name'] = 'The dude'
# File lib/ashikawa-core/document.rb, line 105
def []=(attribute_name, value)
  @content[attribute_name] = value
end
check_if_persisted() click to toggle source

Raises an exception if the document is not persisted

@raise [DocumentNotFoundException] @return nil @api semipublic @example Check if the document is persisted

document = Ashikawa::Core::Document.new(database, raw_document)
document.check_if_persisted
# File lib/ashikawa-core/document.rb, line 68
def check_if_persisted
  raise DocumentNotFoundException if @id == :not_persisted
end
delete() click to toggle source

Remove the document from the database

@return [Hash] parsed JSON response from the server @api public @example Delete a document

document = Ashikawa::Core::Document.new(database, raw_document)
document.delete
# File lib/ashikawa-core/document.rb, line 91
def delete
  check_if_persisted
  send_request_for_document(delete: {})
end
refresh() click to toggle source

Get a fresh version of this document from the database

@return self @api public @example Refresh the document

document = Ashikawa::Core::Document.new(database, raw_document)
document.refresh
# File lib/ashikawa-core/document.rb, line 140
def refresh
  parse_raw_document(send_request_for_document)
end
save() click to toggle source

Save the changes to the database

@return [Hash] parsed JSON response from the server @api public @example Save changes to a document

document = Ashikawa::Core::Document.new(database, raw_document)
document['occupation'] = 'Not occupied'
document.save
# File lib/ashikawa-core/document.rb, line 128
def save
  check_if_persisted
  send_request_for_document(put: @content)
end
to_h() click to toggle source

Convert the document into a hash

@return [Hash] @api public @example Get the hash representation of a document

document = Ashikawa::Core::Document.new(database, raw_document)
document.to_h #=> { :name => 'Lebowski", :occupation => "Not occupied' }
# File lib/ashikawa-core/document.rb, line 116
def to_h
  @content
end

Protected Instance Methods

clean_up_additional_data(raw_data) click to toggle source

Clean up the raw data hash to have string keys

@param [Hash] raw_data @return [Hash] Cleaned up hash @api private

# File lib/ashikawa-core/document.rb, line 177
def clean_up_additional_data(raw_data)
  raw_data.reduce({}) do |result, (key, value)|
    result[key.to_s] = value
    result
  end
end
parse_raw_document(raw_document) click to toggle source

Parse information returned from the server

@param [Hash] raw_document @return self @api private

# File lib/ashikawa-core/document.rb, line 151
def parse_raw_document(raw_document)
  @id       = raw_document['_id'] || :not_persisted
  @key      = raw_document['_key']
  @revision = raw_document['_rev'] || :not_persisted
  @content  = raw_document.delete_if { |key| key.start_with?('_') }
  self
end
send_request_for_document(opts = {}) click to toggle source

Send a request for this document with the given opts

@param [Hash] opts Options for this request @return [Hash] The parsed response from the server @api private

# File lib/ashikawa-core/document.rb, line 164
def send_request_for_document(opts = {})
  if graph
    @database.send_request("gharial/#{graph.name}/vertex/#{@id}", opts)
  else
    @database.send_request("document/#{@id}", opts)
  end
end