Class: Checkr::APIList

Inherits:
APIClass show all
Includes:
Enumerable
Defined in:
lib/checkr/api_list.rb

Instance Attribute Summary collapse

Attributes inherited from APIClass

#json

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from APIClass

api_class_method, api_instance_method, attribute, attribute_aliases, attribute_names, attribute_writer_alias, attribute_writer_names, #attributes, #changed_attribute_names, #changed_attributes, changed_lambda, #clear_changed_attributes, construct, #construct, #initialize, #inspect, #mark_attribute_changed, #non_nil_attributes, #path, path, register_subclass, subclass_fetch, subclasses, #to_json, #to_s

Constructor Details

This class inherits a constructor from Checkr::APIClass

Instance Attribute Details

#api_list_klassObject

Returns the value of attribute api_list_klass.



7
8
9
# File 'lib/checkr/api_list.rb', line 7

def api_list_klass
  @api_list_klass
end

Class Method Details

.constructor(klass) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/checkr/api_list.rb', line 29

def self.constructor(klass)
  lambda do |json|
    klass = Util.constantize(klass) unless klass.is_a?(Class)

    instance = self.new
    instance.api_list_klass = klass
    instance.refresh_from(json)
  end
end

Instance Method Details

#[](k) ⇒ Object



9
10
11
# File 'lib/checkr/api_list.rb', line 9

def [](k)
  data[k]
end

#[]=(k, v) ⇒ Object



13
14
15
# File 'lib/checkr/api_list.rb', line 13

def []=(k, v)
  data[k]=v
end

#each(&blk) ⇒ Object



25
26
27
# File 'lib/checkr/api_list.rb', line 25

def each(&blk)
  data.each(&blk)
end

#lastObject



17
18
19
# File 'lib/checkr/api_list.rb', line 17

def last
  data.last
end

#lengthObject



21
22
23
# File 'lib/checkr/api_list.rb', line 21

def length
  data.length
end

#refresh_from(json) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/checkr/api_list.rb', line 39

def refresh_from(json)
  self.object = "list"
  self.data ||= []
  self.json = Util.sorta_deep_clone(json)

  if json.is_a?(Hash)
    self.refresh_from_hash(json)
  elsif json.is_a?(Array)
    self.refresh_from_array(json)
  else
    self.clear_changed_attributes
    self
  end
end

#refresh_from_array(array = []) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/checkr/api_list.rb', line 69

def refresh_from_array(array=[])
  klass = api_list_klass
  json = {
    :object => "list",
    :data => array
  }
  refresh_from_hash(json)
end

#refresh_from_hash(json = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/checkr/api_list.rb', line 54

def refresh_from_hash(json={})
  klass = api_list_klass

  json.each do |k, v|
    if self.class.attribute_writer_names.include?(k.to_sym)
      if k.to_sym == :data
        self.send("#{k}=", v.map{ |i| klass.construct(i) })
      else
        self.send("#{k}=", v)
      end
    end
  end
  self
end