Class: TypedForm::VirtualModel
- Inherits:
-
Object
- Object
- TypedForm::VirtualModel
- 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
-
#form ⇒ Form
readonly
The instance of the form used when referencing questions and answers in the submission.
-
#question_attrs ⇒ Hash
readonly
Hash mapping attribute names to regular expressions, expressed as strings.
-
#response_attrs ⇒ Hash
readonly
Hash mapping attribute names to a chain of attributes accessbile from the form response.
Instance Method Summary collapse
-
#initialize(form:, question_attrs: nil, response_attrs: nil) ⇒ VirtualModel
constructor
A new instance of VirtualModel.
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
#form ⇒ Form (readonly)
The instance of the form used when referencing questions and answers in the submission.
44 45 46 |
# File 'lib/typed_form/virtual_model.rb', line 44 def form @form end |
#question_attrs ⇒ Hash (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" }
44 45 46 |
# File 'lib/typed_form/virtual_model.rb', line 44 def question_attrs @question_attrs end |
#response_attrs ⇒ Hash (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" }
44 45 46 |
# File 'lib/typed_form/virtual_model.rb', line 44 def response_attrs @response_attrs end |