Class: Quby::Questionnaires::API

Inherits:
Object
  • Object
show all
Defined in:
lib/quby/questionnaires/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(questionnaire_repo:) ⇒ API

Returns a new instance of API.



6
7
8
9
# File 'lib/quby/questionnaires/api.rb', line 6

def initialize(questionnaire_repo:)
  @repo = questionnaire_repo
  @cache = {}
end

Instance Method Details

#allObject



28
29
30
# File 'lib/quby/questionnaires/api.rb', line 28

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

#exists?(questionnaire_key) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/quby/questionnaires/api.rb', line 24

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

#find(key) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# 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) ⇒ Object



32
33
34
35
36
# 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