flexserializer 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/flexserializer/base.rb +35 -34
- 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: 2af5104b81744b9e96b10b97df9b9a1ab6b56d2c
|
4
|
+
data.tar.gz: 86ceddcf5f9d165b1fe46fbe5900dd451da215e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86ca7d1897a1f2e82aa038caf1a384fd6dae0aacc3a7767ee188e349c314695871ba8390e05c948ad45bff418915f59729361a59a790c6463258e237e21195c5
|
7
|
+
data.tar.gz: 7e7e104d5a267e6fae7cb8eae6d1c9fc08ed9432d24293efda99af702aad2831ea49010a262d241e291cf838dbfc6dd8a70627bac4ef6c43012708174e78e64d
|
data/lib/flexserializer/base.rb
CHANGED
@@ -1,42 +1,43 @@
|
|
1
1
|
module Flexserializer
|
2
2
|
class Base < ActiveModel::Serializer
|
3
3
|
class << self
|
4
|
-
|
4
|
+
attr_accessor :groups, :data_default_attributes
|
5
|
+
|
6
|
+
def default_attributes(*attrs)
|
7
|
+
attrs = attrs.first if attrs.first.class == Array
|
8
|
+
self.data_default_attributes = attrs
|
9
|
+
end
|
5
10
|
|
6
|
-
|
7
|
-
|
8
|
-
|
11
|
+
def group(group_name, &block)
|
12
|
+
self.groups ||= {}
|
13
|
+
self.groups[group_name] = block
|
14
|
+
end
|
9
15
|
end
|
10
|
-
|
11
|
-
def
|
12
|
-
self.
|
13
|
-
|
16
|
+
|
17
|
+
def initialize(object, options = {})
|
18
|
+
self.class.data_default_attributes ||= []
|
19
|
+
define_attributes(options[:group])
|
20
|
+
super(object, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def define_attributes(group)
|
24
|
+
clear_data
|
25
|
+
define_default_attrs
|
26
|
+
return unless self.class.groups.keys.include?(group)
|
27
|
+
define_group_attrs(group)
|
28
|
+
end
|
29
|
+
|
30
|
+
def clear_data
|
31
|
+
self.class._attributes_data = {}
|
32
|
+
end
|
33
|
+
|
34
|
+
def define_default_attrs
|
35
|
+
self.class.data_default_attributes = self.class.data_default_attributes.first if self.class.data_default_attributes.first.class == Array
|
36
|
+
self.class.data_default_attributes.each { |attr| self.class.attribute(attr) }
|
37
|
+
end
|
38
|
+
|
39
|
+
def define_group_attrs(group)
|
40
|
+
self.class.groups[group].call
|
14
41
|
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def initialize(object, options = {})
|
18
|
-
self.class.data_default_attributes ||= []
|
19
|
-
define_attributes(options[:group])
|
20
|
-
super(object, options)
|
21
|
-
end
|
22
|
-
|
23
|
-
def define_attributes(group)
|
24
|
-
clear_data
|
25
|
-
define_default_attrs
|
26
|
-
return unless self.class.groups.keys.include?(group)
|
27
|
-
define_group_attrs(group)
|
28
|
-
end
|
29
|
-
|
30
|
-
def clear_data
|
31
|
-
self.class._attributes_data = {}
|
32
|
-
end
|
33
|
-
|
34
|
-
def define_default_attrs
|
35
|
-
self.class.data_default_attributes = self.class.data_default_attributes.first if self.class.data_default_attributes.first.class == Array
|
36
|
-
self.class.data_default_attributes.each { |attr| self.class.attribute(attr) }
|
37
|
-
end
|
38
|
-
|
39
|
-
def define_group_attrs(group)
|
40
|
-
self.class.groups[group].call
|
41
42
|
end
|
42
43
|
end
|