Class: CreditGateway::BaseModel
- Inherits:
-
Object
- Object
- CreditGateway::BaseModel
show all
- Defined in:
- lib/credit_gateway/base_model.rb
Direct Known Subclasses
Amount, Balance, BankAccount, BankAccountScheme, BankConnection, BankConnectionParams, BankData, BankDataRequest, BankSubject, Company, Rating, RatingComponent, RatingMetadata, RatingStatus, Score, ScoreBadge, Transaction
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ BaseModel
Returns a new instance of BaseModel.
43
44
45
46
47
|
# File 'lib/credit_gateway/base_model.rb', line 43
def initialize(attributes = {})
attributes.each do |attr, value|
public_send("#{attr}=", value)
end
end
|
Class Method Details
.attribute_aliases(aliases = nil) ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/credit_gateway/base_model.rb', line 19
def attribute_aliases(aliases = nil)
@attribute_aliases ||= {}
return @attribute_aliases unless aliases
@attribute_aliases.merge!(aliases)
end
|
.attributes(*attributes) ⇒ Object
9
10
11
12
13
14
15
16
17
|
# File 'lib/credit_gateway/base_model.rb', line 9
def attributes(*attributes)
@attributes ||= []
return @attributes unless attributes
attr_accessor(*attributes)
@attributes += attributes
end
|
.build(json: {}, key_transformer: self.key_transformer) ⇒ Object
Sets all the instance variables by reading the JSON from CreditGateway and converting the keys from PascalCase to snake_case, as it’s the standard in Ruby.
33
34
35
36
37
38
39
40
|
# File 'lib/credit_gateway/base_model.rb', line 33
def build(json: {}, key_transformer: self.key_transformer)
new.tap do |record|
attributes.each do |attr|
key = attribute_aliases.key?(attr) ? attribute_aliases[attr] : attr
record.public_send("#{attr}=", json[key_transformer.transform(key)])
end
end
end
|
27
28
29
|
# File 'lib/credit_gateway/base_model.rb', line 27
def key_transformer
CamelizerLower
end
|
Instance Method Details
#as_json ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/credit_gateway/base_model.rb', line 49
def as_json
self.class.attributes.each_with_object({}) do |attr, hash|
value = public_send(attr)
value = value.as_json if value.respond_to?(:as_json)
if value.is_a?(Array)
value = value.map { |v| v.respond_to?(:as_json) ? v.as_json : v }
end
value = value.iso8601 if value.respond_to?(:iso8601)
key = self.class.key_transformer.transform(attr)
hash[key] = value
end
end
|