Class: Bindan::Provider::Firestore

Inherits:
Object
  • Object
show all
Defined in:
lib/bindan/providers/firestore.rb

Defined Under Namespace

Classes: ColOrDocNotExist

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection = nil, project_id: nil, raise_error: false, sdk: ::Google::Cloud::Firestore, **kwargs) ⇒ Firestore

Returns a new instance of Firestore.

Parameters:

  • collection (String) (defaults to: nil)
  • raise_error (bool) (defaults to: false)
  • sdk (Google::Cloud::Firestore) (defaults to: ::Google::Cloud::Firestore)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bindan/providers/firestore.rb', line 40

def initialize(collection = nil, project_id: nil, raise_error: false, sdk: ::Google::Cloud::Firestore, **kwargs)
  if ENV["FIRESTORE_EMULATOR_HOST"]
    project_id ||= "project"
  end

  @_options = {}
  @collection = collection

  @_options[:raise_error] = raise_error
  @project_id = project_id

  @_firestore = sdk.new(project_id: project_id, **kwargs)
end

Instance Attribute Details

#project_id ⇒ Object (readonly)

Returns the value of attribute project_id.



53
54
55
# File 'lib/bindan/providers/firestore.rb', line 53

def project_id
  @project_id
end

Instance Method Details

#[](key) ⇒ Hash

Parameters:

  • key (String)

Returns:

  • (Hash)

Raises:

  • ColOrDocNotExist



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bindan/providers/firestore.rb', line 69

def [](key)
  doc_path =
    if @collection.nil? && key.include?("/")
      key
    else
      [@collection, key].join("/")
    end

  doc = @_firestore.doc(doc_path).get

  if @_options[:raise_error] && !doc.data
    raise ColOrDocNotExist.new doc_path
  else
    doc.data
  end
end

#_prepare(pair) ⇒ Object

Parameters:

  • pair (Hash)


58
59
60
61
62
# File 'lib/bindan/providers/firestore.rb', line 58

def _prepare(pair)
  path, doc = pair.to_a.flatten

  @_firestore.doc(path).create(doc)
end