Class: FirestoreODM::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/firestore-odm/document.rb

Direct Known Subclasses

Model

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document = nil) ⇒ Document

Constructor.

Parameters:



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

#changesObject (readonly)

Returns the value of attribute changes.



6
7
8
# File 'lib/firestore-odm/document.rb', line 6

def changes
  @changes
end

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/firestore-odm/document.rb', line 6

def data
  @data
end

#documentObject

Returns the value of attribute document.



5
6
7
# File 'lib/firestore-odm/document.rb', line 5

def document
  @document
end

Instance Method Details

#deleteObject

Deletes the document.



46
47
48
# File 'lib/firestore-odm/document.rb', line 46

def delete
  document.delete
end

#discard_changesObject

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.

Parameters:

  • key (String, Symbol)

    the field’s name.

Returns:

  • the field’s value.



53
54
55
# File 'lib/firestore-odm/document.rb', line 53

def get key
  changes[key] or data[key]
end

#idString

Gets the document id.

Returns:

  • (String)

    the document id.



21
22
23
# File 'lib/firestore-odm/document.rb', line 21

def id
  document.document_id
end

#pathString

Gets the document path

Returns:

  • (String)

    the document path.



27
28
29
# File 'lib/firestore-odm/document.rb', line 27

def path
  document.document_path
end

#saveObject

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.

Parameters:

  • key (String, Symbol)

    the field’s name.

  • value

    the field’s value.



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.

Parameters:

  • opts (defaults to: nil)

    options to be passed to JSON#generate.

Returns:

  • (String)

    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