Class: Ashikawa::Core::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/ashikawa-core/document.rb

Overview

A certain Document within a certain Collection

Direct Known Subclasses

Edge

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, raw_document, additional_data = {}) ⇒ Document

Initialize a Document with the database and raw data

Examples:

Create a document

document = Ashikawa::Core::Document.new(database, raw_document)

Parameters:

  • database (Database)
  • raw_document (Hash)
  • additional_data (Hash) (defaults to: {})
  • _additional_data (Hash)

    a customizable set of options



53
54
55
56
57
58
59
# File 'lib/ashikawa-core/document.rb', line 53

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

Instance Attribute Details

#graphGraph (readonly)

The optional graph this document belongs to

Returns:

  • (Graph)

    The Graph instance the document was fetched from



42
43
44
# File 'lib/ashikawa-core/document.rb', line 42

def graph
  @graph
end

#idString (readonly)

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

Examples:

Get the ID for a Document

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

Returns:

  • (String)


18
19
20
# File 'lib/ashikawa-core/document.rb', line 18

def id
  @id
end

#keyString (readonly)

The key of the document (No collection prefix)

Examples:

Get the key for a Document

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

Returns:

  • (String)


27
28
29
# File 'lib/ashikawa-core/document.rb', line 27

def key
  @key
end

#revisionString (readonly)

The current revision of the document

Examples:

Get the Revision for a Document

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

Returns:

  • (String)


36
37
38
# File 'lib/ashikawa-core/document.rb', line 36

def revision
  @revision
end

Instance Method Details

#[](attribute_name) ⇒ Object

Get the value of an attribute of the document

Examples:

Get the name attribute of a document

document = Ashikawa::Core::Document.new(database, raw_document)
document['name'] #=> 'Lebowski'

Parameters:

  • attribute_name (String)

Returns:

  • (Object)

    The value of the attribute



81
82
83
# File 'lib/ashikawa-core/document.rb', line 81

def [](attribute_name)
  @content[attribute_name]
end

#[]=(attribute_name, value) ⇒ Object

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

Examples:

Change the name attribute of a document

document = Ashikawa::Core::Document.new(database, raw_document)
document['name'] = 'The dude'

Parameters:

  • attribute_name (String)
  • value (Object)

Returns:

  • (Object)

    The value



106
107
108
# File 'lib/ashikawa-core/document.rb', line 106

def []=(attribute_name, value)
  @content[attribute_name] = value
end

#check_if_persistedObject

Raises an exception if the document is not persisted

Examples:

Check if the document is persisted

document = Ashikawa::Core::Document.new(database, raw_document)
document.check_if_persisted

Returns:

  • nil

Raises:



69
70
71
# File 'lib/ashikawa-core/document.rb', line 69

def check_if_persisted
  raise DocumentNotFoundException if @id == :not_persisted
end

#deleteHash

Remove the document from the database

Examples:

Delete a document

document = Ashikawa::Core::Document.new(database, raw_document)
document.delete

Returns:

  • (Hash)

    parsed JSON response from the server



92
93
94
95
# File 'lib/ashikawa-core/document.rb', line 92

def delete
  check_if_persisted
  send_request_for_document(delete: {})
end

#refreshObject

Get a fresh version of this document from the database

Examples:

Refresh the document

document = Ashikawa::Core::Document.new(database, raw_document)
document.refresh

Returns:

  • self



141
142
143
# File 'lib/ashikawa-core/document.rb', line 141

def refresh
  parse_raw_document(send_request_for_document)
end

#saveHash

Save the changes to the database

Examples:

Save changes to a document

document = Ashikawa::Core::Document.new(database, raw_document)
document['occupation'] = 'Not occupied'
document.save

Returns:

  • (Hash)

    parsed JSON response from the server



129
130
131
132
# File 'lib/ashikawa-core/document.rb', line 129

def save
  check_if_persisted
  send_request_for_document(put: @content)
end

#to_hHash

Convert the document into a hash

Examples:

Get the hash representation of a document

document = Ashikawa::Core::Document.new(database, raw_document)
document.to_h #=> { :name => 'Lebowski", :occupation => "Not occupied' }

Returns:

  • (Hash)


117
118
119
# File 'lib/ashikawa-core/document.rb', line 117

def to_h
  @content
end