Class: JSONAPIonify::Structure::Objects::Base
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#[], #[]=
#allowed_type_map, #permitted_key?, #permitted_keys, #permitted_type_for?, #permitted_types_for, #required_key?, #required_keys
#[], #[]=
#client?, #origin, #server?
Methods included from Callbacks
#run_callbacks
Constructor Details
#initialize(**attributes) ⇒ Base
56
57
58
59
60
61
62
63
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 56
def initialize(**attributes)
@object = Hash.new
run_callbacks :initialize do
attributes.each do |k, v|
self[k] = v
end
end
end
|
Instance Attribute Details
#errors ⇒ Object
81
82
83
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 81
def errors
@errors
end
|
#object ⇒ Object
26
27
28
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 26
def object
@object
end
|
#parent ⇒ Object
26
27
28
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 26
def parent
@parent
end
|
#warnings ⇒ Object
81
82
83
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 81
def warnings
@warnings
end
|
Class Method Details
.define_order(*keys) ⇒ Object
43
44
45
46
47
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 43
def self.define_order(*keys)
define_singleton_method :ordered_keys do
keys
end
end
|
.from_hash(hash) ⇒ Object
39
40
41
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 39
def self.from_hash(hash)
new hash.deep_symbolize_keys
end
|
.from_json(json) ⇒ Object
51
52
53
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 51
def self.from_json(json)
from_hash Oj.load json
end
|
Instance Method Details
#==(other) ⇒ Object
69
70
71
72
73
74
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 69
def ==(other)
return unless other.respond_to? :[]
object.all? do |k, v|
other[k] == v
end
end
|
#===(other) ⇒ Object
76
77
78
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 76
def ===(other)
other.class == self.class && self == other
end
|
#as_json(**opts) ⇒ Object
88
89
90
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 88
def as_json(**opts)
compile(**opts).deep_stringify_keys
end
|
#compile(validate: true) ⇒ Object
83
84
85
86
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 83
def compile(validate: true)
self.validate if validate && !JSONAPIonify.validation_disabled?
to_h
end
|
#compile!(*args) ⇒ Object
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 121
def compile!(*args)
compile(*args).tap do
if (wrns = warnings).present?
warn validation_error wrns.all_messages.to_sentence + '.'
end
if (errs = errors).present?
raise validation_error errs.all_messages.to_sentence + '.'
end
end
end
|
#copy ⇒ Object
65
66
67
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 65
def copy
self.class.from_hash to_h
end
|
#inspect ⇒ Object
158
159
160
161
162
163
164
165
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 158
def inspect
hash_inspect = to_h.to_s.gsub(
/\:([a-zA-Z_-]+)=>/, '\\1: '
)[1..-2].gsub(
/\{([^\s])/, '{ \\1').gsub(/([^\s])\}/, '\\1 }'
)
to_s.sub(/>$/, " #{hash_inspect}>")
end
|
#pretty_json ⇒ Object
154
155
156
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 154
def pretty_json
JSON.pretty_generate as_json
end
|
#signature ⇒ Object
96
97
98
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 96
def signature
"#{self.class.name}:#{Digest::SHA2.hexdigest to_h.to_s}"
end
|
#to_h ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 100
def to_h
ordered_object = [
*object.slice(*self.class.ordered_keys),
*object.except(*self.class.ordered_keys)
]
ordered_object.reduce({}) do |hash, (k, v)|
hash[k] =
case v
when Objects::Base
v.to_h
when Hash
v.deep_stringify_keys
when Collections::Base
v.collect_hashes
else
v
end
hash
end
end
|
#to_json(**opts) ⇒ Object
92
93
94
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 92
def to_json(**opts)
Oj.dump as_json(**opts)
end
|
#validate ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/jsonapionify/structure/objects/base.rb', line 132
def validate
object.values.each { |val| val.validate if val.respond_to? :validate }
[errors, warnings].each(&:clear)
@errors, @warnings =
cache_store.fetch(signature) do
run_callbacks :validation do
collect_child_errors
collect_child_warnings
end
[errors, warnings]
end
errors.blank?
end
|