Sha256: 6eed4b918007cba1a6226a94e7074560603748884976a26e58185832992ad030

Contents?: true

Size: 1.36 KB

Versions: 24

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Quby
  module Questionnaires
    class API
      def initialize(questionnaire_repo:)
        @repo = questionnaire_repo
        @cache = {}
      end

      def find(key)
        if fresh?(key)
          # print "HIT:#{key} "
          return @cache[key][:questionnaire]
          # else
          # print "MISS:#{key} "
        end

        definition = @repo.find key
        @cache[key] = {questionnaire: build_from_definition(definition), timestamp: definition.timestamp}
        @cache[key][:questionnaire]
      end

      def exists?(questionnaire_key)
        @repo.exists? questionnaire_key
      end

      def all
        @repo.keys.map { |key| find(key) }
      end

      def validate(key, sourcecode)
        definition = Entities::Definition.new(key: key, sourcecode: sourcecode, path: "validating '#{key}'")
        definition.valid?
        definition
      end

      private

      def build_from_definition(definition)
        ActiveSupport::Notifications.instrument('quby.questionaire.build') do
          if definition.json
            DSL.from_json(definition.json)
          else
            DSL.build_from_definition(definition)
          end
        end
      end

      def fresh?(key)
        return false unless @cache.key?(key)
        @cache[key][:timestamp].to_i == @repo.timestamp(key).to_i
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
quby-5.6.5 lib/quby/questionnaires/api.rb
quby-5.6.3 lib/quby/questionnaires/api.rb
quby-5.6.2 lib/quby/questionnaires/api.rb
quby-5.6.1 lib/quby/questionnaires/api.rb
quby-5.6.0 lib/quby/questionnaires/api.rb
quby-5.5.0 lib/quby/questionnaires/api.rb
quby-5.4.0 lib/quby/questionnaires/api.rb
quby-5.3.1 lib/quby/questionnaires/api.rb
quby-5.3.0 lib/quby/questionnaires/api.rb
quby-5.2.0 lib/quby/questionnaires/api.rb
quby-5.1.3 lib/quby/questionnaires/api.rb
quby-5.1.2 lib/quby/questionnaires/api.rb
quby-5.1.1 lib/quby/questionnaires/api.rb
quby-5.1.0 lib/quby/questionnaires/api.rb
quby-5.0.5 lib/quby/questionnaires/api.rb
quby-5.0.4 lib/quby/questionnaires/api.rb
quby-5.0.3 lib/quby/questionnaires/api.rb
quby-5.0.2 lib/quby/questionnaires/api.rb
quby-5.0.1 lib/quby/questionnaires/api.rb
quby-5.0.0 lib/quby/questionnaires/api.rb