lib/kamerling/repo.rb in kamerling-0.0.2 vs lib/kamerling/repo.rb in kamerling-0.0.3
- old
+ new
@@ -1,32 +1,39 @@
-module Kamerling class Repo
- NotFound = Class.new RuntimeError
+require 'sequel'
+require_relative 'mapper'
- def initialize klass, source
- @klass, @source = klass, source
- end
+module Kamerling
+ class Repo
+ NotFound = Class.new(RuntimeError)
- def << object
- hash = object.to_h
- warn_off { source << hash }
- rescue Sequel::UniqueConstraintViolation
- warn_off { source.where(uuid: object.uuid).update hash }
- end
+ def initialize(klass, source, mapper: Mapper)
+ @klass = klass
+ @mapper = mapper
+ @source = source
+ end
- def [] uuid
- hash = warn_off { source[uuid: uuid] }
- fail NotFound, "#{klass} with UUID #{uuid}" unless hash
- klass.from_h hash
- end
+ def <<(object)
+ hash = mapper.to_h(object)
+ warn_off { source << hash }
+ rescue Sequel::UniqueConstraintViolation
+ warn_off { source.where(uuid: object.uuid).update hash }
+ end
- def all
- source.all.map { |hash| klass.from_h hash }
- end
+ def [](uuid)
+ hash = warn_off { source[uuid: uuid] }
+ fail NotFound, "#{klass} with UUID #{uuid}" unless hash
+ mapper.from_h(klass, 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
+ def all
+ source.all.map { |hash| mapper.from_h(klass, hash) }
+ end
- attr_reader :klass, :source
- private :klass, :source
-end end
+ def related_to(object)
+ key = "#{object.class.name.split('::').last.downcase}_uuid".to_sym
+ source.where(key => object.uuid).map { |hash| mapper.from_h(klass, hash) }
+ end
+
+ attr_reader :klass, :mapper, :source
+ private :klass, :mapper, :source
+ end
+end