Method: TypeformData::ValueClass#initialize

Defined in:
lib/typeform_data/value_class.rb

#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