lib/icarus/mod/firestore.rb in Icarus-Mod-Tools-2.0 vs lib/icarus/mod/firestore.rb in Icarus-Mod-Tools-2.0.1
- old
+ new
@@ -11,14 +11,19 @@
attr_reader :client, :collections
def initialize
@client = Google::Cloud::Firestore.new(credentials: Config.firebase.credentials.to_h)
@collections = Config.firebase.collections
+ @repositories = repositories
+ @modinfo = modinfo
+ @toolinfo = toolinfo
+ @mods = mods
+ @tools = tools
end
- def repos
- @repos ||= list(:repositories)
+ def repositories
+ @repositories ||= list(:repositories)
end
def modinfo
@modinfo ||= list(:modinfo)
end
@@ -34,41 +39,13 @@
def tools
@tools ||= list(:tools)
end
def find_by_type(type:, name:, author:)
- list(type).find { |obj| obj.name == name && obj.author == author }
+ instance_variable_get("@#{type}").find { |obj| obj.name == name && obj.author == author }
end
- def get_list(type)
- raise "Invalid type: #{type} - unknown collection" unless collections.respond_to?(type)
-
- @client.doc(collections.send(type)).get[:list]
- end
-
- def list(type)
- case type.to_sym
- when :modinfo, :toolinfo, :repositories
- get_list(type)
- when :mods, :tools
- @client.col(collections.send(type)).get.map do |doc|
- klass = type == :mods ? Icarus::Mod::Tools::Modinfo : Icarus::Mod::Tools::Toolinfo
- klass.new(doc.data, id: doc.document_id, created: doc.create_time, updated: doc.update_time)
- end
- else
- raise "Invalid type: #{type}"
- end
- end
-
- def update_or_create(type, payload, merge:)
- doc_id = payload.id || find_by_type(type:, name: payload.name, author: payload.author)&.id
-
- return @client.doc("#{collections.send(type)}/#{doc_id}").set(payload.to_h, merge:) if doc_id
-
- @client.col(collections.send(type)).add(payload.to_h)
- end
-
def update(type, payload, merge: false)
raise "You must specify a payload to update" if payload&.empty? || payload.nil?
case type.to_sym
when :modinfo, :toolinfo
@@ -95,9 +72,31 @@
response.is_a?(Google::Cloud::Firestore::CommitResponse::WriteResult)
end
private
+
+ def list(type)
+ case type.to_sym
+ when :modinfo, :toolinfo, :repositories
+ @client.doc(collections.send(type)).get[:list]
+ when :mods, :tools
+ @client.col(collections.send(type)).get.map do |doc|
+ klass = type == :mods ? Icarus::Mod::Tools::Modinfo : Icarus::Mod::Tools::Toolinfo
+ klass.new(doc.data, id: doc.document_id, created: doc.create_time, updated: doc.update_time)
+ end
+ else
+ raise "Invalid type: #{type}"
+ end
+ end
+
+ def update_or_create(type, payload, merge:)
+ doc_id = payload.id || find_by_type(type:, name: payload.name, author: payload.author)&.id
+
+ return @client.doc("#{collections.send(type)}/#{doc_id}").set(payload.to_h, merge:) if doc_id
+
+ @client.col(collections.send(type)).add(payload.to_h)
+ end
def pluralize(type)
type.to_s.end_with?("s") ? type.to_s : "#{type}s"
end
end