Class: Collector::BaseModel
- Inherits:
-
Object
- Object
- Collector::BaseModel
show all
- Defined in:
- lib/collector/base_model.rb
Direct Known Subclasses
ActivateInvoiceRequest, Address, AdjustInvoiceRequest, ArticleListItem, CancelInvoiceRequest, GetAddressRequest, InvoiceRequest, InvoiceResponse, InvoiceRow, ReplaceInvoiceRequest, User
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash = {}) ⇒ BaseModel
Returns a new instance of BaseModel.
49
50
51
52
53
54
55
|
# File 'lib/collector/base_model.rb', line 49
def initialize(hash = {})
validate_input(hash) unless self.class.swallow_unsupported_attributes?
all_attributes.each do |attr|
val = hash[attr] || hash[attr.to_s]
self.send("#{attr}=", val)
end
end
|
Class Method Details
.attribute ⇒ Object
18
19
20
21
22
|
# File 'lib/collector/base_model.rb', line 18
def attributes(*args)
@attributes ||= []
@attributes += args
args.each{|attr| attr_accessor attr }
end
|
.attribute_opt ⇒ Object
19
20
21
22
23
|
# File 'lib/collector/base_model.rb', line 19
def attributes_opt(*args)
@attributes_opt ||= []
@attributes_opt += args
args.each{|attr| attr_accessor attr }
end
|
.attributes(*args) ⇒ Object
7
8
9
10
11
|
# File 'lib/collector/base_model.rb', line 7
def attributes(*args)
@attributes ||= []
@attributes += args
args.each{|attr| attr_accessor attr }
end
|
.attributes_opt(*args) ⇒ Object
12
13
14
15
16
|
# File 'lib/collector/base_model.rb', line 12
def attributes_opt(*args)
@attributes_opt ||= []
@attributes_opt += args
args.each{|attr| attr_accessor attr }
end
|
.swallow_unsupported_attributes ⇒ Object
21
22
23
|
# File 'lib/collector/base_model.rb', line 21
def swallow_unsupported_attributes
@swallow_unsupported_attributes = true
end
|
.swallow_unsupported_attributes? ⇒ Boolean
25
26
27
|
# File 'lib/collector/base_model.rb', line 25
def swallow_unsupported_attributes?
@swallow_unsupported_attributes
end
|
Instance Method Details
#==(other) ⇒ Object
57
58
59
60
61
62
|
# File 'lib/collector/base_model.rb', line 57
def ==(other)
self.class.attributes.each do |attr|
return false if self.send(attr) != other.send(attr)
end
true
end
|
#all_attributes ⇒ Object
38
39
40
|
# File 'lib/collector/base_model.rb', line 38
def all_attributes
attributes + attributes_opt
end
|
#attributes ⇒ Object
30
31
32
|
# File 'lib/collector/base_model.rb', line 30
def attributes
self.class.instance_variable_get("@attributes") || []
end
|
#attributes_opt ⇒ Object
34
35
36
|
# File 'lib/collector/base_model.rb', line 34
def attributes_opt
self.class.instance_variable_get("@attributes_opt") || []
end
|
#has_required_attributes? ⇒ Boolean
64
65
66
|
# File 'lib/collector/base_model.rb', line 64
def has_required_attributes?
missing_attributes.empty?
end
|
#missing_attributes ⇒ Object
68
69
70
|
# File 'lib/collector/base_model.rb', line 68
def missing_attributes
own_missing + nested_missing
end
|
#missing_attributes_human_readable(prefix = "", missing_attr = nil) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/collector/base_model.rb', line 97
def missing_attributes_human_readable(prefix = "", missing_attr = nil)
missing_attr = missing_attributes if missing_attr.nil?
missing_key_paths = missing_attr.map.each_with_index do |item|
if item.kind_of? Hash
key = item.keys.first
missing_attributes_human_readable(prefix + "#{key}.", item[key])
else
"#{prefix}#{item}"
end
end
if prefix.empty?
"Missing attributes: " + missing_key_paths.flatten.join(", ")
else
missing_key_paths
end
end
|
#nested_missing ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/collector/base_model.rb', line 76
def nested_missing
missing = []
all_attributes.each do |attr|
val = send(attr)
if !!val
if val.kind_of? BaseModel
nested_missing = val.missing_attributes
missing << {attr => nested_missing} unless nested_missing.empty?
elsif val.kind_of? Array
val.each_with_index do |nested_val, index|
if nested_val.kind_of? BaseModel
nested_missing = nested_val.missing_attributes
missing << {"#{attr}[#{index}]" => nested_missing} unless nested_missing.empty?
end
end
end
end
end
missing
end
|
#own_missing ⇒ Object
72
73
74
|
# File 'lib/collector/base_model.rb', line 72
def own_missing
attributes.select{|attr| send(attr).nil? }
end
|
42
43
44
45
46
47
|
# File 'lib/collector/base_model.rb', line 42
def validate_input(hash)
unsupported_keys = hash.keys.map(&:to_sym) - all_attributes
unless unsupported_keys.empty?
raise ArgumentError.new("Unsupported attribute(s): #{unsupported_keys}")
end
end
|