class Quby::Questionnaires::API

Public Class Methods

new(questionnaire_repo:) click to toggle source
# File lib/quby/questionnaires/api.rb, line 6
def initialize(questionnaire_repo:)
  @repo = questionnaire_repo
  @cache = {}
end

Public Instance Methods

all() click to toggle source
# File lib/quby/questionnaires/api.rb, line 28
def all
  @repo.keys.map { |key| find(key) }
end
exists?(questionnaire_key) click to toggle source
# File lib/quby/questionnaires/api.rb, line 24
def exists?(questionnaire_key)
  @repo.exists? questionnaire_key
end
find(key) click to toggle source
# File lib/quby/questionnaires/api.rb, line 11
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
validate(key, sourcecode) click to toggle source
# File lib/quby/questionnaires/api.rb, line 32
def validate(key, sourcecode)
  definition = Entities::Definition.new(key: key, sourcecode: sourcecode, path: "validating '#{key}'")
  definition.valid?
  definition
end

Private Instance Methods

build_from_definition(definition) click to toggle source
# File lib/quby/questionnaires/api.rb, line 40
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
fresh?(key) click to toggle source
# File lib/quby/questionnaires/api.rb, line 50
def fresh?(key)
  return false unless @cache.key?(key)
  @cache[key][:timestamp].to_i == @repo.timestamp(key).to_i
end