class Au::Diff

Attributes

change[R]
db[R]
id[R]
md5[R]
parent_id[R]

Public Class Methods

create(db_name, parent_id, change, md5) click to toggle source
# File lib/au/models/diff.rb, line 6
def self.create(db_name, parent_id, change, md5)
  db = get_db(db_name)
  id = Time.now.to_f
  db.transaction do
    db[id] = [parent_id, change, md5]
  end
  id
end
find(id, db_name: nil, db: nil) click to toggle source
# File lib/au/models/diff.rb, line 27
def self.find(id, db_name: nil, db: nil)
  raise 'Missing both db name and db' if db_name.nil? && db.nil?
  db ||= get_db(db_name)
  stored_values = db.transaction{ db[id] }

  raise 'Diff not found' unless stored_values
  parent_id, change, md5 = stored_values
  new(id, parent_id, change, md5, db)
end
get_db(db_name) click to toggle source

memoize all encountered dbs Note, may need to watch memory usage. Note, depending on PStore initialization, may not need this

# File lib/au/models/diff.rb, line 40
def self.get_db(db_name)
  if @dbs
    @dbs[db_name] ||= PStore.new(db_name)
  else
    @dbs = {}
    get_db(db_name)
  end
end
new(id, parent_id, change, md5, db) click to toggle source
# File lib/au/models/diff.rb, line 51
def initialize(id, parent_id, change, md5, db)
  @id = id
  @parent_id = parent_id
  @change = change
  @md5 = md5
  @db = db
end
replay_changes_upto(id, db_name) { |diff| ... } click to toggle source
# File lib/au/models/diff.rb, line 15
def self.replay_changes_upto(id, db_name)
  raise 'Missing block' unless block_given?
  diffs = [find(id, db_name: db_name)]
  while diffs.last.has_parent?
    diffs << diffs.last.parent
  end
  # apply the oldest first
  diffs.reverse.each do |diff|
    yield(diff)
  end
end

Public Instance Methods

has_parent?() click to toggle source
# File lib/au/models/diff.rb, line 64
def has_parent?
  !!parent_id
end
parent() click to toggle source
# File lib/au/models/diff.rb, line 59
def parent
  return unless parent_id
  self.class.find(parent_id, db: db)
end