Class: FirestoreODM::Document
- Inherits:
-
Object
- Object
- FirestoreODM::Document
- Defined in:
- lib/firestore-odm/document.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#changes ⇒ Object
readonly
Returns the value of attribute changes.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#document ⇒ Object
Returns the value of attribute document.
Instance Method Summary collapse
-
#delete ⇒ Object
Deletes the document.
-
#discard_changes ⇒ Object
Discards any changes made to the document.
-
#get(key) ⇒ Object
(also: #[])
Gets a field.
-
#id ⇒ String
Gets the document id.
-
#initialize(document = nil) ⇒ Document
constructor
Constructor.
-
#path ⇒ String
Gets the document path.
-
#save ⇒ Object
Saves any changes made to the document.
-
#set(key, value) ⇒ Object
(also: #[]=)
Sets a field.
-
#to_json(opts = nil) ⇒ String
Converts the document to a JSON string.
Constructor Details
#initialize(document = nil) ⇒ Document
Constructor.
10 11 12 13 14 15 16 17 |
# File 'lib/firestore-odm/document.rb', line 10 def initialize document = nil unless document.nil? @document = document @data = document.get.data end discard_changes end |
Instance Attribute Details
#changes ⇒ Object (readonly)
Returns the value of attribute changes.
6 7 8 |
# File 'lib/firestore-odm/document.rb', line 6 def changes @changes end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
6 7 8 |
# File 'lib/firestore-odm/document.rb', line 6 def data @data end |
#document ⇒ Object
Returns the value of attribute document.
5 6 7 |
# File 'lib/firestore-odm/document.rb', line 5 def document @document end |
Instance Method Details
#delete ⇒ Object
Deletes the document.
46 47 48 |
# File 'lib/firestore-odm/document.rb', line 46 def delete document.delete end |
#discard_changes ⇒ Object
Discards any changes made to the document.
40 41 42 43 |
# File 'lib/firestore-odm/document.rb', line 40 def discard_changes @changes = {} return end |
#get(key) ⇒ Object Also known as: []
Gets a field.
53 54 55 |
# File 'lib/firestore-odm/document.rb', line 53 def get key changes[key] or data[key] end |
#id ⇒ String
Gets the document id.
21 22 23 |
# File 'lib/firestore-odm/document.rb', line 21 def id document.document_id end |
#path ⇒ String
Gets the document path
27 28 29 |
# File 'lib/firestore-odm/document.rb', line 27 def path document.document_path end |
#save ⇒ Object
Saves any changes made to the document.
32 33 34 35 36 37 |
# File 'lib/firestore-odm/document.rb', line 32 def save document.update changes @data = document.get.data discard_changes return end |
#set(key, value) ⇒ Object Also known as: []=
Sets a field.
60 61 62 63 |
# File 'lib/firestore-odm/document.rb', line 60 def set key, value changes[key] = value return end |
#to_json(opts = nil) ⇒ String
Converts the document to a JSON string.
68 69 70 |
# File 'lib/firestore-odm/document.rb', line 68 def to_json opts = nil data.merge(changes).to_json(opts) end |