Sha256: f36c8e94c7991578760973c670e398898a1436108f346e9b2586632be15275e7

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

module Asynchronic
  module DataStore
    class ScopedStore

      include Helper

      attr_reader :data_store
      attr_reader :scope
      
      def initialize(data_store, scope)
        @data_store = data_store
        @scope = Key.new scope
      end

      def [](key)
        @data_store[@scope[key]]
      end

      def []=(key, value)
        @data_store[@scope[key]] = value
      end

      def delete(key)
        @data_store.delete @scope[key]
      end

      def keys
        @data_store.keys.
          select { |k| k.start_with? @scope[''] }.
          map { |k| Key.new(k).remove_first @scope.sections.count }
      end

      def connection
        {
          data_store_class: @data_store.class,
          data_store_connection: @data_store.connection,
          scope: @scope
        }
      end

      def self.connect(options)
        data_store = options[:data_store_class].connect options[:data_store_connection]
        new data_store, options[:scope]
      end

      def to_s
        "#<#{self.class} @data_store=#{@data_store} @scope=#{@scope}>"
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
asynchronic-0.3.1 lib/asynchronic/data_store/scoped_store.rb
asynchronic-0.3.0 lib/asynchronic/data_store/scoped_store.rb
asynchronic-0.2.3 lib/asynchronic/data_store/scoped_store.rb
asynchronic-0.2.2 lib/asynchronic/data_store/scoped_store.rb
asynchronic-0.2.1 lib/asynchronic/data_store/scoped_store.rb
asynchronic-0.2.0 lib/asynchronic/data_store/scoped_store.rb