Class: Ashikawa::Core::Document
- Inherits:
-
Object
- Object
- Ashikawa::Core::Document
- Defined in:
- lib/ashikawa-core/document.rb
Overview
A certain Document within a certain Collection
Direct Known Subclasses
Instance Attribute Summary collapse
-
#graph ⇒ Graph
readonly
The optional graph this document belongs to.
-
#id ⇒ String
readonly
The ID of the document (this includes the Collection prefix).
-
#key ⇒ String
readonly
The key of the document (No collection prefix).
-
#revision ⇒ String
readonly
The current revision of the document.
Instance Method Summary collapse
-
#[](attribute_name) ⇒ Object
Get the value of an attribute of the document.
-
#[]=(attribute_name, value) ⇒ Object
Update the value of an attribute (Does not write to database).
-
#check_if_persisted ⇒ Object
Raises an exception if the document is not persisted.
-
#delete ⇒ Hash
Remove the document from the database.
-
#initialize(database, raw_document, additional_data = {}) ⇒ Document
constructor
Initialize a Document with the database and raw data.
-
#refresh ⇒ Object
Get a fresh version of this document from the database.
-
#save ⇒ Hash
Save the changes to the database.
-
#to_h ⇒ Hash
Convert the document into a hash.
Constructor Details
#initialize(database, raw_document, additional_data = {}) ⇒ Document
Initialize a Document with the database and raw data
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
#graph ⇒ Graph (readonly)
The optional graph this document belongs to
42 43 44 |
# File 'lib/ashikawa-core/document.rb', line 42 def graph @graph end |
#id ⇒ String (readonly)
The ID of the document (this includes the Collection prefix)
18 19 20 |
# File 'lib/ashikawa-core/document.rb', line 18 def id @id end |
#key ⇒ String (readonly)
The key of the document (No collection prefix)
27 28 29 |
# File 'lib/ashikawa-core/document.rb', line 27 def key @key end |
#revision ⇒ String (readonly)
The current revision of the document
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
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)
106 107 108 |
# File 'lib/ashikawa-core/document.rb', line 106 def []=(attribute_name, value) @content[attribute_name] = value end |
#check_if_persisted ⇒ Object
Raises an exception if the document is not persisted
69 70 71 |
# File 'lib/ashikawa-core/document.rb', line 69 def check_if_persisted raise DocumentNotFoundException if @id == :not_persisted end |
#delete ⇒ Hash
Remove the document from the database
92 93 94 95 |
# File 'lib/ashikawa-core/document.rb', line 92 def delete check_if_persisted send_request_for_document(delete: {}) end |
#refresh ⇒ Object
Get a fresh version of this document from the database
141 142 143 |
# File 'lib/ashikawa-core/document.rb', line 141 def refresh parse_raw_document(send_request_for_document) end |
#save ⇒ Hash
Save the changes to the database
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_h ⇒ Hash
Convert the document into a hash
117 118 119 |
# File 'lib/ashikawa-core/document.rb', line 117 def to_h @content end |