Sha256: 5c4c6bc8270b4a76fb7d4fe5eed7547818f310efb1d7cf940b0557de6dea65bf

Contents?: true

Size: 1.14 KB

Versions: 34

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'quby/questionnaires/repos'
require 'quby/questionnaires'

module Quby
  module Questionnaires
    module Repos
      class MemoryRepo < Base
        attr_reader :records

        def initialize(definitions = {})
          @records = definitions.map do |key, definition|
            [key, record_for_definition(definition)]
          end.to_h
        end

        def keys
          records.keys
        end

        def find(key)
          fail(QuestionnaireNotFound, key) unless exists?(key)
          record = records.fetch(key)
          entity(key, record.fetch(:definition), record.fetch(:last_update), "memoryrepo #{key}")
        end

        def exists?(key)
          records.key?(key)
        end

        def timestamp(key)
          fail(QuestionnaireNotFound, key) unless exists?(key)
          records.fetch(key).fetch(:last_update)
        end

        private

        def store!(key, definition)
          records[key] = record_for_definition(definition)
        end

        def record_for_definition(definition)
          {definition: definition, last_update: Time.now}
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
quby-5.0.0 lib/quby/questionnaires/repos/memory_repo.rb
quby-4.0.4 lib/quby/questionnaires/repos/memory_repo.rb
quby-5.0.0.pre4 lib/quby/questionnaires/repos/memory_repo.rb
quby-5.0.0.pre3 lib/quby/questionnaires/repos/memory_repo.rb
quby-5.0.0.pre2 lib/quby/questionnaires/repos/memory_repo.rb
quby-5.0.0.pre1 lib/quby/questionnaires/repos/memory_repo.rb
quby-4.0.3 lib/quby/questionnaires/repos/memory_repo.rb
quby-4.0.2 lib/quby/questionnaires/repos/memory_repo.rb
quby-4.0.1 lib/quby/questionnaires/repos/memory_repo.rb
quby-4.0.0 lib/quby/questionnaires/repos/memory_repo.rb
quby-3.2.2 lib/quby/questionnaires/repos/memory_repo.rb
quby-3.2.1 lib/quby/questionnaires/repos/memory_repo.rb
quby-3.2.0 lib/quby/questionnaires/repos/memory_repo.rb
quby-3.2.0.pre.pre1 lib/quby/questionnaires/repos/memory_repo.rb