Class: JSONAPIonify::Structure::Collections::Base
- Inherits:
-
Array
- Object
- Array
- JSONAPIonify::Structure::Collections::Base
show all
- Includes:
- Helpers::InheritsOrigin
- Defined in:
- lib/jsonapionify/structure/collections/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#client?, #origin, #server?
Constructor Details
#initialize(array = []) ⇒ Base
Returns a new instance of Base.
21
22
23
24
25
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 21
def initialize(array = [])
array.each do |instance|
self << instance
end
end
|
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
9
10
11
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 9
def parent
@parent
end
|
Class Method Details
.value_is(type_class) ⇒ Object
13
14
15
16
17
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 13
def self.value_is(type_class)
define_method(:type_class) do
type_class
end
end
|
Instance Method Details
#<<(instance) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 62
def <<(instance)
new_instance =
case instance
when Hash
type_class.new(**instance)
when type_class
instance
else
if type_class < instance.class
type_class.from_hash instance.to_h
else
raise(
ValidationError,
"Can't initialize collection `#{self.class.name}` with a type of `#{instance.class.name}`"
)
end
end
self[length] = new_instance
end
|
#[](k) ⇒ Object
82
83
84
85
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 82
def [] k
v = super
v.nil? || v.instance_variable_get(:@parent) == self ? v : self[k] = v
end
|
#[]=(k, v) ⇒ Object
87
88
89
90
91
92
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 87
def []= k, v
unless v.nil? || v.instance_variable_get(:@parent) == self
v = v.dup.tap { |obj| obj.instance_variable_set :@parent, self}
end
super(k, v)
end
|
#collect_hashes ⇒ Object
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 41
def collect_hashes
map do |member|
case member
when Objects::Base, Hash
member.to_h
else
member
end
end
end
|
#compile ⇒ Object
52
53
54
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 52
def compile
collect_hashes
end
|
#each ⇒ Object
94
95
96
97
98
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 94
def each
length.times do |i|
yield self[i]
end
end
|
#errors ⇒ Object
100
101
102
103
104
105
106
107
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 100
def errors
map.each_with_index.each_with_object({}) do |(value, key), errors|
next unless value.respond_to? :errors
value.errors.each do |error_key, message|
errors[[key, error_key].join('/')] = message
end
end
end
|
#new(**attributes) ⇒ Object
Also known as:
append
56
57
58
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 56
def new(**attributes)
self << attributes
end
|
#original_method(method) ⇒ Object
27
28
29
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 27
def original_method(method)
Array.instance_method(method).bind(self)
end
|
#signature ⇒ Object
37
38
39
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 37
def signature
"#{self.class.name}:#{Digest::SHA2.hexdigest map(&:signature).join}"
end
|
#validate ⇒ Object
31
32
33
34
35
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 31
def validate
each do |member|
member.validate if member.respond_to? :validate
end
end
|
#warnings ⇒ Object
Also known as:
all_warnings
109
110
111
112
113
114
115
116
|
# File 'lib/jsonapionify/structure/collections/base.rb', line 109
def warnings
map.each_with_index.each_with_object({}) do |(value, key), warnings|
next unless value.respond_to? :all_warnings
value.all_warnings.each do |warning_key, message|
warnings[[key, warning_key].join('. ')] = message
end
end
end
|