class GreyscaleRecord::DataStore::Store

Public Class Methods

new() click to toggle source
# File lib/greyscale_record/data_store/store.rb, line 4
def initialize
  @data = {}
  @tables = {}
end

Public Instance Methods

[]( name ) click to toggle source
# File lib/greyscale_record/data_store/store.rb, line 9
def []( name )
  data[ name ]
end
apply_patch( patch ) click to toggle source

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?

# File lib/greyscale_record/data_store/store.rb, line 36
def apply_patch( patch )
  Thread.current[patch_key] = patched_data patch
end
init_table( name, rows ) click to toggle source
# File lib/greyscale_record/data_store/store.rb, line 21
def init_table( name, rows )
  @data[name] = rows
  @tables[name] = Table.new( name, self )
end
patched?() click to toggle source
# File lib/greyscale_record/data_store/store.rb, line 44
def patched?
  Thread.current[patch_key].present?
end
remove_patch() click to toggle source
# File lib/greyscale_record/data_store/store.rb, line 40
def remove_patch
  Thread.current[patch_key] = nil
end
table( name ) click to toggle source
# File lib/greyscale_record/data_store/store.rb, line 13
def table( name )
  unless @tables[name]
    raise GreyscaleRecord::Errors::DataStoreError, "Data Store error: table '#{name}' does not exist"
  end

  @tables[name]
end
with_patch( patch ) { || ... } click to toggle source
# File lib/greyscale_record/data_store/store.rb, line 26
def with_patch( patch )
  apply_patch patch
  yield
  remove_patch
end

Private Instance Methods

data() click to toggle source
# File lib/greyscale_record/data_store/store.rb, line 63
def data
  if patched?
    Thread.current[patch_key]
  else
    @data
  end
end
patch_key() click to toggle source
# File lib/greyscale_record/data_store/store.rb, line 58
def patch_key
  @key ||= "#{object_id}_patch"
end
patched_data(patch) click to toggle source
# File lib/greyscale_record/data_store/store.rb, line 50
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