Sha256: f2cdd7e8989c62c171023a6e98dce634061323af3dc3b37514e7eb22caaf9b49

Contents?: true

Size: 1.68 KB

Versions: 44

Compression:

Stored size: 1.68 KB

Contents

require "singleton"
require "securerandom"
module EtFakeCcd
  class DocumentStoreService
    include Singleton
    def self.store_file(filename:, type:, file:, classification:)
      instance.store_file(filename: filename, type: type, file: file, classification: classification)
    end

    def self.find_by_id(id)
      instance.find_by_id(id)
    end

    def self.find_file_by_id(id)
      instance.find_file_by_id(id)
    end

    def store_file(filename:, type:, file:, classification:)
      adapter.store(filename: filename, type: type, file: file, classification: classification)
    end

    def find_by_id(id)
      adapter.fetch_by_id(id)
    end

    def find_file_by_id(id)
      adapter.fetch_file_by_id(id)
    end

    def adapter
      @adapter ||= InMemoryAdapter.new
    end

    class InMemoryAdapter
      def initialize(file_storage_path: ::EtFakeCcd.config.file_storage_path)
        self.data = {}
        self.file_storage_path = file_storage_path
      end

      def store(filename:, type:, file:, classification:)
        uuid = SecureRandom.uuid
        file_path = File.join(file_storage_path, uuid)
        FileUtils.cp file.path, file_path
        data[uuid] = {
          'filename' => filename,
          'type' => type,
          'file_path' => uuid,
          'size' => file.size,
          'classification' => classification
        }
        uuid
      end

      def fetch_by_id(id)
        data[id]
      end

      def fetch_file_by_id(id)
        document = fetch_by_id(id)
        return nil if document.nil?

        File.new(File.join(file_storage_path, document['file_path']), 'rb')
      end

      private

      attr_accessor :data, :file_storage_path
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
et_fake_ccd-2.1.0 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-2.0.0 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.1.11 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.1.10 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.1.9 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.1.8 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.1.6 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.1.5 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.1.4 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.1.3 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.1.2 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.1.1 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.1.0 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.0.8 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.0.7 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.0.6 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.0.5 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.0.4 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.0.3 lib/et_fake_ccd/document_store_service.rb
et_fake_ccd-1.0.2 lib/et_fake_ccd/document_store_service.rb