Module: TypeformData::ValueClass

Included in:
Typeform, Typeform::Answer, Typeform::Field, Typeform::Question, Typeform::Response, Typeform::Stats
Defined in:
lib/typeform_data/value_class.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



26
27
28
# File 'lib/typeform_data/value_class.rb', line 26

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#initialize(config, attrs) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/typeform_data/value_class.rb', line 7

def initialize(config, attrs)
  unless config && config.is_a?(TypeformData::Config)
    raise TypeformData::ArgumentError, 'Expected a TypeformData::Config instance as the first '\
      'argument'
  end
  @config = config

  keys = attribute_keys

  attrs.each do |key, value|
    # Previously, we would throw an error if an unexpected attribute was present. Unfortunately,
    # this causes exceptions in production if Typeform adds new fields to their API responses.
    # We could filter the fields to the expected keys, but that defeats the purpose of having
    # the validation.
    next unless keys.include?(key) || keys.include?(key.to_sym)
    instance_variable_set("@#{key}", value)
  end
end

#marshal_dumpObject



42
43
44
45
46
47
# File 'lib/typeform_data/value_class.rb', line 42

def marshal_dump
  # For the sake of security, we don't want to serialize our API key.
  attribute_keys.to_a.map do |key|
    [key, instance_variable_get("@#{key}")]
  end
end

#marshal_load(hash) ⇒ Object



49
50
51
52
53
# File 'lib/typeform_data/value_class.rb', line 49

def marshal_load(hash)
  hash.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

#reconfig(config) ⇒ Object

Compound classes (e.g. a Response which has many Answers) should use this method to re-set ‘config’ on each child object. ValueClass#reconfig is called in TypeformData#load.



57
58
59
# File 'lib/typeform_data/value_class.rb', line 57

def reconfig(config)
  self.config = config
end