Sha256: cd1eac206bba49b0fce72f2c5610899d766928b1c6119b2d1f6b2304e66d5690

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

module GreyscaleRecord
  module DataStore
    class Store
      def initialize
        @data = {}
        @tables = {}
      end

      def []( name )
        data[ name ]
      end

      def table( name )
        unless @tables[name]
          raise GreyscaleRecord::Errors::DataStoreError, "Data Store error: table '#{name}' does not exist"
        end

        @tables[name]
      end

      def init_table( name, rows )
        @data[name] = rows
        @tables[name] = Table.new( name, self )
      end

      def with_patch( patch )
        apply_patch patch
        yield
        remove_patch
      end

      # This only allows for one patch at a time. 
      # Is there ever a case when we would need, like a stack of these things? 
      # I don't think so? 

      def apply_patch( patch )
        Thread.current[patch_key] = patched_data patch
      end

      def remove_patch
        Thread.current[patch_key] = nil
      end

      def patched?
        Thread.current[patch_key].present?
      end
      
      private

      def patched_data(patch)
        unless patch.respond_to? :apply
          raise GreyscaleRecord::Errors::DataStoreError, "Data Store Error: apply_patch: patch must respond to 'apply(doc)'."
        end

        patch.apply( @data.deep_dup )
      end

      def patch_key
        @key ||= "#{object_id}_patch"
      end


      def data
        if patched?
          Thread.current[patch_key]
        else
          @data
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
greyscale_record-1.0.3 lib/greyscale_record/data_store/store.rb
greyscale_record-1.0.2 lib/greyscale_record/data_store/store.rb
greyscale_record-1.0.1 lib/greyscale_record/data_store/store.rb
greyscale_record-1.0.0 lib/greyscale_record/data_store/store.rb