Class: OpenPayU::Models::Model
- Inherits:
-
Object
- Object
- OpenPayU::Models::Model
show all
- Includes:
- ActiveModel::Serializers::JSON, ActiveModel::Validations
- Defined in:
- lib/openpayu/models/model.rb
Direct Known Subclasses
Address, Buyer, Card, Fee, NotifyResponse, Order, PayMethod, Product, Refund, ShippingMethod, StatusUpdate, Token
Instance Attribute Summary (collapse)
Class Method Summary
(collapse)
Instance Method Summary
(collapse)
Constructor Details
- (Model) initialize(values)
Returns a new instance of Model
13
14
15
16
17
18
|
# File 'lib/openpayu/models/model.rb', line 13
def initialize(values)
values.each_pair do |k, v|
send("#{k}=", v)
end
after_initialize
end
|
Instance Attribute Details
- (Object) all_errors
Returns the value of attribute all_errors
11
12
13
|
# File 'lib/openpayu/models/model.rb', line 11
def all_errors
@all_errors
end
|
Class Method Details
+ (Object) define_reader(association)
123
124
125
|
# File 'lib/openpayu/models/model.rb', line 123
def define_reader(association)
attr_reader association
end
|
+ (Object) define_writer(association, class_name)
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/openpayu/models/model.rb', line 108
def define_writer(association, class_name)
class_eval <<-CODE
def #{association}=(value)
@#{association} =
if value.class.name == "Array"
value.collect do |val|
#{class_name.to_s.camelize}.new(val)
end
else
#{class_name.to_s.camelize}.new(value)
end
end
CODE
end
|
+ (Object) has_many_objects(association, class_name)
98
99
100
101
|
# File 'lib/openpayu/models/model.rb', line 98
def has_many_objects(association, class_name)
define_writer(association, class_name)
define_reader(association)
end
|
+ (Object) has_one_object(association)
103
104
105
106
|
# File 'lib/openpayu/models/model.rb', line 103
def has_one_object(association)
define_writer(association, association)
define_reader(association)
end
|
Instance Method Details
- (Object) after_initialize
20
21
|
# File 'lib/openpayu/models/model.rb', line 20
def after_initialize
end
|
- (Boolean) all_objects_valid?
73
74
75
|
# File 'lib/openpayu/models/model.rb', line 73
def all_objects_valid?
!validate_all_objects.any?
end
|
- (Object) attributes
23
24
25
|
# File 'lib/openpayu/models/model.rb', line 23
def attributes
instance_values
end
|
- (Object) get_instance_values
35
36
37
38
39
|
# File 'lib/openpayu/models/model.rb', line 35
def get_instance_values
instance_values.delete_if do |k, v|
%w(all_errors errors validation_context).include?(k)
end
end
|
- (Object) prepare_data
31
32
33
|
# File 'lib/openpayu/models/model.rb', line 31
def prepare_data
prepare_keys.to_json
end
|
- (Object) prepare_keys(attrs = {}, hash = get_instance_values)
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/openpayu/models/model.rb', line 41
def prepare_keys(attrs = {}, hash = get_instance_values)
hash.each_pair do |k, v|
attrs[k.camelize(:lower)] =
if v.is_a? Array
{ k.camelize(:lower) => v.map(&:prepare_keys) }
elsif v.class.name =~ /OpenPayU::Models/
v.prepare_keys
else
v
end
end
attrs
end
|
- (Object) to_flatten_hash(source = nil, target = {}, namespace = nil, index = nil)
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/openpayu/models/model.rb', line 77
def to_flatten_hash(source = nil, target = {}, namespace = nil, index = nil)
source ||= prepare_keys(instance_values)
prefix = "#{namespace}." if namespace
case source
when Hash
array_index = "[#{index}]" if index
source.each do |key, value|
to_flatten_hash(value, target, "#{prefix}#{key}#{array_index}")
end
when Array
source.each_with_index do |value, i|
to_flatten_hash(value, target, namespace, i)
end
else
target[namespace] = source
end
target
end
|
- (Object) to_json
27
28
29
|
# File 'lib/openpayu/models/model.rb', line 27
def to_json
prepare_keys.to_json
end
|
- (Object) validate_all_objects
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/openpayu/models/model.rb', line 55
def validate_all_objects
@all_errors = {}
instance_values.each_pair do |k, v|
if v.is_a? Array
v.each do |element|
if element.validate_all_objects.any?
@all_errors[element.class.name] = element.errors
end
end
elsif v.class.name =~ /OpenPayU::Models/
@all_errors[v.class.name] = v.errors unless v.valid?
end
end
@all_errors[self.class.name] = errors unless valid?
@all_errors
end
|