Class: USerializer::BaseSerializer
- Inherits:
-
Object
- Object
- USerializer::BaseSerializer
show all
- Defined in:
- lib/userializer/base_serializer.rb
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(obj, opts = {}) ⇒ BaseSerializer
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/userializer/base_serializer.rb', line 43
def initialize(obj, opts = {})
@obj = obj
@opts = opts
@meta = opts[:meta]
@except = Set.new([opts[:except]].flatten.compact)
@only = Set.new([opts[:only]].flatten.compact)
@root_key = (opts[:root] || ActiveSupport::Inflector.underscore(
obj.class.name
).split('/').last).to_sym
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mth) ⇒ Object
98
|
# File 'lib/userializer/base_serializer.rb', line 98
def method_missing(mth); @obj.send(mth); end
|
Class Attribute Details
.attrs ⇒ Object
Returns the value of attribute attrs.
36
37
38
|
# File 'lib/userializer/base_serializer.rb', line 36
def attrs
@attrs
end
|
.relations ⇒ Object
Returns the value of attribute relations.
36
37
38
|
# File 'lib/userializer/base_serializer.rb', line 36
def relations
@relations
end
|
Instance Attribute Details
Returns the value of attribute meta.
39
40
41
|
# File 'lib/userializer/base_serializer.rb', line 39
def meta
@meta
end
|
#obj ⇒ Object
Also known as:
object
Returns the value of attribute obj.
39
40
41
|
# File 'lib/userializer/base_serializer.rb', line 39
def obj
@obj
end
|
#opts ⇒ Object
Returns the value of attribute opts.
39
40
41
|
# File 'lib/userializer/base_serializer.rb', line 39
def opts
@opts
end
|
Class Method Details
.attributes(*attrs, &block) ⇒ Object
15
16
17
18
19
20
|
# File 'lib/userializer/base_serializer.rb', line 15
def attributes(*attrs, &block)
attrs = attrs.first if attrs.first.class.is_a?(Array)
opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
attrs.each { |attr| @attrs[attr] = Attribute.new(attr, opts, block) }
end
|
.has_many(*attrs) ⇒ Object
29
30
31
32
33
34
|
# File 'lib/userializer/base_serializer.rb', line 29
def has_many(*attrs)
attrs = attrs.first if attrs.first.class.is_a?(Array)
opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
attrs.each { |attr| @relations[attr] = HasMany.new(attr, opts) }
end
|
.has_one(*attrs) ⇒ Object
22
23
24
25
26
27
|
# File 'lib/userializer/base_serializer.rb', line 22
def has_one(*attrs)
attrs = attrs.first if attrs.first.class.is_a?(Array)
opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
attrs.each { |attr| @relations[attr] = HasOne.new(attr, opts) }
end
|
.inherited(subclass) ⇒ Object
10
11
12
13
|
# File 'lib/userializer/base_serializer.rb', line 10
def inherited(subclass)
subclass.attrs = self.attrs.dup || { id: Attribute.new(:id, {}, nil) }
subclass.relations = self.relations.dup || {}
end
|
Instance Method Details
#merge_root(res, key, single, opts) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/userializer/base_serializer.rb', line 66
def merge_root(res, key, single, opts)
if single
res[key] = serializable_hash(opts)
else
res[key] ||= []
id = @obj.id
if res[key].detect { |v| id && v[:id] == id }
return
else
res[key] << serializable_hash(opts)
end
end
_relations.each { |rel| rel.merge_root(res, self, opts) }
end
|
#serializable_hash(opts) ⇒ Object
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/userializer/base_serializer.rb', line 55
def serializable_hash(opts)
res = {}
_attributes.each { |attr| attr.merge_attributes(res, self, opts) }
_relations.each do |rel|
rel.merge_attributes(res, self, opts)
end
res
end
|
#to_hash ⇒ Object
84
85
86
87
88
89
90
91
92
|
# File 'lib/userializer/base_serializer.rb', line 84
def to_hash
res = {}
merge_root(res, @root_key, true, @opts.slice(:scope))
res[:meta] = @meta if @meta
res
end
|
#to_json ⇒ Object
94
95
96
|
# File 'lib/userializer/base_serializer.rb', line 94
def to_json
Oj.dump(to_hash, mode: :compat)
end
|