Sha256: 8800b4dc4c76255c4b00261ec86335e771f4012a6bb8ab8dba482bbd8ba215bd

Contents?: true

Size: 754 Bytes

Versions: 67

Compression:

Stored size: 754 Bytes

Contents

require 'forwardable'

module Inferno
  module Repositories
    class InMemoryRepository
      extend Forwardable

      def_delegators 'self.class', :all, :all_by_id

      def insert(entity)
        all << entity
        entity
      end

      def find(id)
        all_by_id[id.to_s]
      end

      def exists?(id)
        all_by_id.include? id
      end

      class << self
        def all
          @all ||= []
        end

        # @private
        def all_by_id
          @all_by_id ||= {}
          @all_by_id.length == all.length ? @all_by_id : index_by_id
        end

        def index_by_id
          @all_by_id = {}
          all.each { |klass| @all_by_id[klass.id] = klass }
          @all_by_id
        end
      end
    end
  end
end

Version data entries

67 entries across 67 versions & 1 rubygems

Version Path
inferno_core-0.4.44 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.43 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.42 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.41 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.40 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.39 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.38 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.35 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.34 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.33 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.32 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.31 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.30 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.29 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.28 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.27 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.26 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.25 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.24 lib/inferno/repositories/in_memory_repository.rb
inferno_core-0.4.23 lib/inferno/repositories/in_memory_repository.rb