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



23
24
25
# File 'lib/typeform_data/value_class.rb', line 23

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
# File 'lib/typeform_data/value_class.rb', line 7

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

  keys = attribute_keys

  attrs.each do |key, value|
    unless keys.include?(key) || keys.include?(key.to_sym)
      raise ArgumentError, "Unexpected key: #{key}"
    end
    instance_variable_set("@#{key}", value)
  end
end

#marshal_dumpObject



39
40
41
42
43
44
# File 'lib/typeform_data/value_class.rb', line 39

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



46
47
48
49
50
# File 'lib/typeform_data/value_class.rb', line 46

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

#reconfig(config) ⇒ Object

Compond 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.



54
55
56
# File 'lib/typeform_data/value_class.rb', line 54

def reconfig(config)
  self.config = config
end