flexserializer 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/flexserializer/base.rb +45 -7
- data/lib/flexserializer/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 979100e56ed56a77928ecff0210714f950433fb8
|
4
|
+
data.tar.gz: 9c132b27819571afebe6832993aefcc5958f5a7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 230c9d488326bf2bc667a76481bb17e4f5f9fb887d2d85185a60d6eb594a0327a22e0371ef606c370ad58491cd0ca08886bbb89a35959a02e7c02e202d87f740
|
7
|
+
data.tar.gz: 728ccec0f83f07e06de5f04b79b5d9583e22a60ea03917512f5be7a95a48a090d7aa9dabcd7e8b8ab58ef2bd3c100fa6ee2270a4392704b400ce1d1ab69d7cb4
|
data/lib/flexserializer/base.rb
CHANGED
@@ -19,13 +19,13 @@ module Flexserializer
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
attr_reader :group_name, :_attributes_data, :
|
22
|
+
attr_reader :group_name, :_attributes_data, :_reflections
|
23
23
|
|
24
24
|
def initialize(object, options = {})
|
25
25
|
super(object, options)
|
26
26
|
@_attributes_data = {}
|
27
|
+
@_reflections = {}
|
27
28
|
@group_name = options[:group]
|
28
|
-
@define_options = options.clone
|
29
29
|
make_all_attributes
|
30
30
|
end
|
31
31
|
|
@@ -41,11 +41,6 @@ module Flexserializer
|
|
41
41
|
_attributes_data[key] = Attribute.new(attr, options, block)
|
42
42
|
end
|
43
43
|
|
44
|
-
def make_all_attributes
|
45
|
-
define_default_attrs
|
46
|
-
define_group_attrs
|
47
|
-
end
|
48
|
-
|
49
44
|
def define_default_attrs
|
50
45
|
return unless self.class.data_default_attributes
|
51
46
|
self.instance_eval &self.class.data_default_attributes
|
@@ -57,6 +52,22 @@ module Flexserializer
|
|
57
52
|
end
|
58
53
|
end
|
59
54
|
|
55
|
+
def define_has_many(name, options = {}, &block)
|
56
|
+
define_associate(HasManyReflection.new(name, options, block))
|
57
|
+
end
|
58
|
+
|
59
|
+
def define_belongs_to(name, options = {}, &block)
|
60
|
+
define_associate(BelongsToReflection.new(name, options, block))
|
61
|
+
end
|
62
|
+
|
63
|
+
def define_has_one(name, options = {}, &block)
|
64
|
+
define_associate(HasOneReflection.new(name, options, block))
|
65
|
+
end
|
66
|
+
|
67
|
+
def define_options
|
68
|
+
instance_options
|
69
|
+
end
|
70
|
+
|
60
71
|
#override serializer methods
|
61
72
|
|
62
73
|
def attributes(requested_attrs = nil, reload = false)
|
@@ -67,5 +78,32 @@ module Flexserializer
|
|
67
78
|
hash[key] = attr.value(self)
|
68
79
|
end
|
69
80
|
end
|
81
|
+
|
82
|
+
def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
|
83
|
+
include_slice ||= include_directive
|
84
|
+
return Enumerator.new unless object
|
85
|
+
|
86
|
+
Enumerator.new do |y|
|
87
|
+
_reflections.each do |key, reflection|
|
88
|
+
next if reflection.excluded?(self)
|
89
|
+
next unless include_directive.key?(key)
|
90
|
+
|
91
|
+
association = reflection.build_association(self, instance_options, include_slice)
|
92
|
+
y.yield association
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
def make_all_attributes
|
100
|
+
define_default_attrs
|
101
|
+
define_group_attrs
|
102
|
+
end
|
103
|
+
|
104
|
+
def define_associate(reflection)
|
105
|
+
key = reflection.options[:key] || reflection.name
|
106
|
+
_reflections[key] = reflection
|
107
|
+
end
|
70
108
|
end
|
71
109
|
end
|