Class: TypedForm::VirtualModel

Inherits:
Object
  • Object
show all
Defined in:
lib/typed_form/virtual_model.rb

Overview

A representation of the Typeform Response Data for a single form response. Dynamically adds methods at initialization time, to provide a simple interface for accessing form response data.

# this example would require json data with a question of "What is your
# "email?" and an answer of "[email protected]"

form = TypedForm::Form.build_form_from(json: your_json_source)
hash = { "user_email" => "What is your email" }
virtual_model = TypedForm::VirtualModel.new(form: form, question_attr: hash)

VirtualModel has now dynamically matched the user_email attribute to the answer of the question, by finding the question matching the regular expression "What is your email", and then responding with the answer.

virtual_model.user_email => "[email protected]"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form:, question_attrs: nil, response_attrs: nil) ⇒ VirtualModel

Returns a new instance of VirtualModel.



47
48
49
50
51
52
# File 'lib/typed_form/virtual_model.rb', line 47

def initialize(form:, question_attrs: nil, response_attrs: nil)
  @form = form

  _define_question_attributes(question_attrs || {})
  _define_response_attributes(response_attrs || {})
end

Instance Attribute Details

#formForm (readonly)

The instance of the form used when referencing questions and answers in the submission.

Returns:

  • (Form)

    the current value of form



44
45
46
# File 'lib/typed_form/virtual_model.rb', line 44

def form
  @form
end

#question_attrsHash (readonly)

Hash mapping attribute names to regular expressions, expressed as strings.

Format example: To match the answers of "What is your email" to an attribute of "user_email", the format would look like:

question_attrs = { "user_email" => "What is your email" }

Returns:

  • (Hash)

    the current value of question_attrs



44
45
46
# File 'lib/typed_form/virtual_model.rb', line 44

def question_attrs
  @question_attrs
end

#response_attrsHash (readonly)

Hash mapping attribute names to a chain of attributes accessbile from the form response.

Format example: To match the user_agent of the submission, which is under response["metadata"]["user_agent"], the format would look like:

response_attrs = { "user_agent" => "metadata.user_agent" }

Returns:

  • (Hash)

    the current value of response_attrs



44
45
46
# File 'lib/typed_form/virtual_model.rb', line 44

def response_attrs
  @response_attrs
end