Sha256: 17a8fc9e7bbadc7999066e160f4b7af5f66154fb05a5c9db1ae767755a46b107

Contents?: true

Size: 772 Bytes

Versions: 1

Compression:

Stored size: 772 Bytes

Contents

module Kamerling class Repo
  NotFound = Class.new RuntimeError

  def initialize klass, source
    @klass, @source = klass, source
  end

  def << object
    hash = object.to_h
    warn_off { source << hash }
  rescue Sequel::UniqueConstraintViolation
    warn_off { source.where(uuid: object.uuid).update hash }
  end

  def [] uuid
    hash = warn_off { source[uuid: uuid] }
    fail NotFound, "#{klass} with UUID #{uuid}" unless hash
    klass.from_h hash
  end

  def all
    source.all.map { |hash| klass.from_h hash }
  end

  def related_to object
    key = "#{object.class.name.split('::').last.downcase}_uuid".to_sym
    source.where(key => object.uuid).map { |hash| klass.from_h hash }
  end

  attr_reader :klass, :source
  private     :klass, :source
end end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kamerling-0.0.2 lib/kamerling/repo.rb