Class: Kagaribi::Collection
- Inherits:
-
Object
- Object
- Kagaribi::Collection
- Defined in:
- lib/kagaribi/collection.rb
Overview
Manage a Firestore collection
Constant Summary collapse
- MAX_RETRY_COUNT =
5
Instance Attribute Summary collapse
- #collection_name ⇒ String readonly
- #database_id ⇒ String? readonly
- #logger ⇒ Logger readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#delete(doc_key) ⇒ Object
Delete document in collection.
-
#exists?(doc_key) ⇒ Boolean
Whether document is exists in collection.
-
#get(doc_key) ⇒ Hash<Symbol,Object>
Get document from collection.
-
#initialize(collection_name, database_id: nil, logger: nil) ⇒ Collection
constructor
A new instance of Collection.
-
#set(doc_key, data) ⇒ Object
Save a document to collection.
-
#update(doc_key, data) ⇒ Object
Update a document that has already been saved.
Constructor Details
#initialize(collection_name, database_id: nil, logger: nil) ⇒ Collection
Returns a new instance of Collection.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/kagaribi/collection.rb', line 43 def initialize(collection_name, database_id: nil, logger: nil) @collection_name = collection_name @database_id = database_id @logger = if logger logger else Logger.new($stdout) end end |
Instance Attribute Details
#collection_name ⇒ String (readonly)
30 31 32 |
# File 'lib/kagaribi/collection.rb', line 30 def collection_name @collection_name end |
#database_id ⇒ String? (readonly)
34 35 36 |
# File 'lib/kagaribi/collection.rb', line 34 def database_id @database_id end |
#logger ⇒ Logger (readonly)
38 39 40 |
# File 'lib/kagaribi/collection.rb', line 38 def logger @logger end |
Class Method Details
.sanitize_key(key) ⇒ String
104 105 106 |
# File 'lib/kagaribi/collection.rb', line 104 def self.sanitize_key(key) key.tr("/", "-") end |
Instance Method Details
#delete(doc_key) ⇒ Object
Delete document in collection
97 98 99 100 |
# File 'lib/kagaribi/collection.rb', line 97 def delete(doc_key) ref = firestore.doc(full_doc_key(doc_key)) ref.delete end |
#exists?(doc_key) ⇒ Boolean
Whether document is exists in collection
87 88 89 90 91 92 93 |
# File 'lib/kagaribi/collection.rb', line 87 def exists?(doc_key) with_retry("Kagaribi::Collection#exists?") do ref = firestore.doc(full_doc_key(doc_key)) snap = ref.get snap&.exists? end end |
#get(doc_key) ⇒ Hash<Symbol,Object>
Get document from collection
76 77 78 79 80 81 82 |
# File 'lib/kagaribi/collection.rb', line 76 def get(doc_key) with_retry("Kagaribi::Collection#get") do ref = firestore.doc(full_doc_key(doc_key)) snap = ref.get snap&.data || {} end end |
#set(doc_key, data) ⇒ Object
Note:
If a document with the same key exists, it is overwritten.
Save a document to collection
59 60 61 62 |
# File 'lib/kagaribi/collection.rb', line 59 def set(doc_key, data) ref = firestore.doc(full_doc_key(doc_key)) ref.set(data) end |
#update(doc_key, data) ⇒ Object
Note:
If a document with the same key exists, it is merged.
Update a document that has already been saved
68 69 70 71 |
# File 'lib/kagaribi/collection.rb', line 68 def update(doc_key, data) ref = firestore.doc(full_doc_key(doc_key)) ref.update(data) end |