class Checkr::APIList

Attributes

api_list_klass[RW]

Public Class Methods

constructor(klass) click to toggle source
# 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

Public Instance Methods

[](k) click to toggle source
# File lib/checkr/api_list.rb, line 9
def [](k)
  data[k]
end
[]=(k, v) click to toggle source
# File lib/checkr/api_list.rb, line 13
def []=(k, v)
  data[k]=v
end
each(&blk) click to toggle source
# File lib/checkr/api_list.rb, line 25
def each(&blk)
  data.each(&blk)
end
last() click to toggle source
# File lib/checkr/api_list.rb, line 17
def last
  data.last
end
length() click to toggle source
# File lib/checkr/api_list.rb, line 21
def length
  data.length
end
refresh_from(json) click to toggle source
# 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=[]) click to toggle source
# 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={}) click to toggle source
# 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