Sha256: ad5cabd5be1b80c52e9a5709e91ebea0c2c6ea0e4bae170a8aeb1d3b7a56e0da

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

module Koine
  module Repository
    class Repository
      class RecordNotFound < RuntimeError; end

      attr_accessor :hydrator, :entity_prototype
      attr_reader :storage

      def initialize(storage)
        @storage = storage
      end

      def save(*)
        raise "Method not implemented"
      end

      def remove(*)
        raise "Method not implemented"
      end

      def find_all_by(criterias)
        hydrate_collection(storage.find_all_by(criterias))
      end

      def find_one_by(criterias)
        raw_data = storage.find_one_by(criterias)
        hydrate(raw_data, new_entity) if raw_data
      end

      def find_one_by!(criterias)
        find_one_by(criterias) or raise RecordNotFound
      end

      def hydrator
        @hydrator ||= Koine::Hydrator::Hydrator.new
      end

      def entity_prototype
        @entity_prototype or raise RuntimeError.new("Entity prototype was not set")
      end

      private

      def hydrate_collection(collection)
        [].tap do |hydrated|
          collection.each do |element|
            hydrated << hydrate(element, new_entity)
          end
        end
      end

      def hydrate(data, entity)
        hydrator.hydrate(data, entity)
        entity
      end

      def new_entity
        entity_prototype.dup
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
koine-repository-1.1.1 lib/koine/repository/repository.rb
koine-repository-1.1 lib/koine/repository/repository.rb
koine-repository-1.0 lib/koine/repository/repository.rb
koine-repository-0.9.0 lib/koine/repository/repository.rb