Class: Checkr::APIClass

Inherits:
Object
  • Object
show all
Defined in:
lib/checkr/api_class.rb

Direct Known Subclasses

APIList, APIResource, APISingleton

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil) ⇒ APIClass

Returns a new instance of APIClass.



109
110
111
# File 'lib/checkr/api_class.rb', line 109

def initialize(id=nil)
  refresh_from(id)
end

Instance Attribute Details

#jsonObject

Returns the value of attribute json.



3
4
5
# File 'lib/checkr/api_class.rb', line 3

def json
  @json
end

Class Method Details

.api_class_method(name, method, path = nil, opts = {}) ⇒ Object



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

def self.api_class_method(name, method, path=nil, opts={})
  singleton = class << self; self end
  singleton.send(:define_method, name, api_lambda(name, method, path, opts))
end

.api_instance_method(name, method, path = nil, opts = {}) ⇒ Object



18
19
20
# File 'lib/checkr/api_class.rb', line 18

def self.api_instance_method(name, method, path=nil, opts={})
  self.send(:define_method, name, api_lambda(name, method, path, opts))
end

.attribute(name, klass = nil, opts = {}) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/checkr/api_class.rb', line 22

def self.attribute(name, klass=nil, opts={})
  @attribute_names ||= Set.new
  @attribute_names << name.to_sym

  self.send(:define_method, "#{name}", attribute_get_lambda(name, opts))
  self.send(:define_method, "#{name}=", attribute_set_lambda(name, klass, opts))
end

.attribute_aliasesObject



59
60
61
62
63
64
65
66
# File 'lib/checkr/api_class.rb', line 59

def self.attribute_aliases
  @attribute_aliases ||= Set.new
  unless self == APIClass
    @attribute_aliases + self.superclass.attribute_aliases
  else
    @attribute_aliases
  end
end

.attribute_namesObject



50
51
52
53
54
55
56
57
# File 'lib/checkr/api_class.rb', line 50

def self.attribute_names
  @attribute_names ||= Set.new
  unless self == APIClass
    @attribute_names + self.superclass.attribute_names
  else
    @attribute_names
  end
end

.attribute_writer_alias(name, attr_name) ⇒ Object



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

def self.attribute_writer_alias(name, attr_name)
  @attribute_aliases ||= Set.new
  @attribute_aliases << name.to_sym

  # self.send(:alias_method, name, attr_name)
  self.send(:alias_method, "#{name}=", "#{attr_name}=")
end

.attribute_writer_namesObject



68
69
70
# File 'lib/checkr/api_class.rb', line 68

def self.attribute_writer_names
  self.attribute_names + self.attribute_aliases
end

.changed_lambdaObject



101
102
103
104
105
106
107
# File 'lib/checkr/api_class.rb', line 101

def self.changed_lambda
  # This runs in the context of an instance since it is used in
  # an api_instance_method
  lambda do |instance|
    instance.changed_attributes
  end
end

.construct(json = {}) ⇒ Object



113
114
115
# File 'lib/checkr/api_class.rb', line 113

def self.construct(json={})
  self.new.refresh_from(json)
end

.pathObject

Raises:

  • (NotImplementedError)


5
6
7
# File 'lib/checkr/api_class.rb', line 5

def self.path
  raise NotImplementedError.new("APIClass is an abstract class. Please refer to its subclasses: #{subclasses}")
end

.register_subclass(subclass, name = nil) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/checkr/api_class.rb', line 148

def self.register_subclass(subclass, name=nil)
  @subclasses ||= Set.new
  @subclasses << subclass

  unless name.nil?
    @subclasses_hash ||= {}
    @subclasses_hash[name] = subclass
  end
end

.subclass_fetch(name) ⇒ Object



141
142
143
144
145
146
# File 'lib/checkr/api_class.rb', line 141

def self.subclass_fetch(name)
  @subclasses_hash ||= {}
  if @subclasses_hash.has_key?(name)
    @subclasses_hash[name]
  end
end

.subclassesObject



137
138
139
# File 'lib/checkr/api_class.rb', line 137

def self.subclasses
  return @subclasses ||= Set.new
end

Instance Method Details

#attributesObject



38
39
40
41
42
43
44
# File 'lib/checkr/api_class.rb', line 38

def attributes
  attributes = {}
  self.class.attribute_names.each do |attr|
    attributes[attr.to_sym] = self.send(attr)
  end
  attributes
end

#changed_attribute_namesObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/checkr/api_class.rb', line 77

def changed_attribute_names
  @changed_attribute_names ||= Set.new
  attributes.each do |key, val|
    next if @changed_attribute_names.include?(key)
    if val.is_a?(Array) || val.is_a?(Hash)
      @changed_attribute_names << key if json[key] != val
    end
  end
  @changed_attribute_names
end

#changed_attributesObject



88
89
90
91
92
93
94
# File 'lib/checkr/api_class.rb', line 88

def changed_attributes
  ret = {}
  changed_attribute_names.each do |attr|
    ret[attr] = send(attr)
  end
  ret
end

#clear_changed_attributesObject



96
97
98
# File 'lib/checkr/api_class.rb', line 96

def clear_changed_attributes
  @changed_attribute_names = Set.new
end

#construct(json = {}) ⇒ Object

Alias, but dont declare it as one because we need to use overloaded methods.



133
134
135
# File 'lib/checkr/api_class.rb', line 133

def construct(json={})
  refresh_from(json)
end

#inspectObject



158
159
160
161
# File 'lib/checkr/api_class.rb', line 158

def inspect
  id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
  "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(attributes)
end

#mark_attribute_changed(attr_name) ⇒ Object



72
73
74
75
# File 'lib/checkr/api_class.rb', line 72

def mark_attribute_changed(attr_name)
  @changed_attribute_names ||= Set.new
  @changed_attribute_names << attr_name.to_sym
end

#non_nil_attributesObject



46
47
48
# File 'lib/checkr/api_class.rb', line 46

def non_nil_attributes
  attributes.select{|k, v| !v.nil? }
end

#pathObject

Raises:

  • (NotImplementedError)


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

def path
  raise NotImplementedError.new("APIClass is an abstract class. Please refer to its subclasses: #{APIClass.subclasses}")
end

#refresh_from(json = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/checkr/api_class.rb', line 117

def refresh_from(json={})
  unless json.is_a?(Hash)
    json = { :id => json }
  end
  self.json = Util.sorta_deep_clone(json)

  json.each do |k, v|
    if self.class.attribute_writer_names.include?(k.to_sym)
      self.send("#{k}=", v)
    end
  end
  clear_changed_attributes
  self
end

#to_json(*a) ⇒ Object



167
168
169
# File 'lib/checkr/api_class.rb', line 167

def to_json(*a)
  JSON.generate(non_nil_attributes)
end

#to_s(*args) ⇒ Object



163
164
165
# File 'lib/checkr/api_class.rb', line 163

def to_s(*args)
  JSON.pretty_generate(non_nil_attributes)
end