Sha256: 26a489fdd2e572d7c416eecdac4828fabfac43d93198cfb4d601c77a96cef06f

Contents?: true

Size: 978 Bytes

Versions: 2

Compression:

Stored size: 978 Bytes

Contents

require 'browsing_history/storages/base'

module BrowsingHistory::Storage
  class InvalidStorage < BrowsingHistory::Error; end
  class StoragesNotSet < BrowsingHistory::Error; end

  def self.included(klass)
    klass.extend ConfigureMethods
    klass.init_storages
  end

  module ConfigureMethods
    attr_reader :current_storage_type

    def current_storage
      storages[current_storage_type]
    end

    def storages
      @storages ||= {}
    end

    def attach_storage(storage_type, **opts)
      storage = storages[storage_type]

      if storage
        storage.connect(opts)
        @current_storage_type = storage_type
      else
        raise InvalidStorage
      end
    end

    def init_storages
      BrowsingHistory.storage_types.each do |key|
        storages[key] =
          "BrowsingHistory::Storages::#{key.to_s.camelize}".constantize
      end

      raise StoragesNotSet if storages.empty?
      attach_storage(storages.keys.first)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
browsing_history-0.0.3 lib/browsing_history/storage.rb
browsing_history-0.0.2 lib/browsing_history/storage.rb