flexserializer 1.2.4 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 461eb05eb77be4a74e81b0e144939a080c301f24
4
- data.tar.gz: 4fa3445af89e1bfaa78d56c81ace55a037ca0df8
3
+ metadata.gz: 3749f7978bc0bc289332da7da45d64ad75727c1b
4
+ data.tar.gz: 40fba01616492f5037fd2adcc64169f284ef357e
5
5
  SHA512:
6
- metadata.gz: 20b66a1775ec2964337433ba8afc98b4e60eafc6fa53a7cb9e96842337dbf5da39b9386a79ddb3b9793cc992bce70e5557a384861c37c71d67e90fcec0182f50
7
- data.tar.gz: 18385c5677004d2ecc3520e748c50117886d92b432ec9711e1f9ffe121b655715d5708c007fe33309faf0ec8618dbcd9cef55e3ea0a192ebef20e46dba19a7f9
6
+ metadata.gz: 8eebebd61086ccb38f862547533e5588ec9aeaa847d37970e23b219c7d3e7eae071813458d1a552d7b3a9c39ae807644d2f0f1d2ad15880dd208f7f3e666ea44
7
+ data.tar.gz: 752a6ac3c958b0015c069fae81c272c2c2464ea324a97ad40df47b45407e670b3cf67d16babc6e3bd119af8103b9803e1029085157d6fbe25d6c544a7e229e5f
@@ -3,12 +3,15 @@ module Flexserializer
3
3
  class << self
4
4
  attr_accessor :groups, :data_default_attributes
5
5
 
6
+ def inherited(base)
7
+ base.groups = {}
8
+ end
9
+
6
10
  def default_attributes(&block)
7
11
  self.data_default_attributes = block
8
12
  end
9
13
 
10
14
  def group(*group_names, &block)
11
- self.groups ||= {}
12
15
  group_names.each do |name_group|
13
16
  self.groups[name_group] ||= []
14
17
  self.groups[name_group] << block
@@ -16,32 +19,53 @@ module Flexserializer
16
19
  end
17
20
  end
18
21
 
19
- attr_reader :group_name
20
-
22
+ attr_reader :group_name, :_attributes_data, :define_options
23
+
21
24
  def initialize(object, options = {})
22
- @group_name = options[:group]
23
- define_attributes
24
25
  super(object, options)
26
+ @_attributes_data = {}
27
+ @group_name = options[:group]
28
+ @define_options = options.clone
29
+ make_all_attributes
25
30
  end
26
31
 
27
- def define_attributes
28
- clear_data
29
- define_default_attrs
30
- define_group_attrs
32
+ def define_attributes(*attrs)
33
+ attrs = attrs.first if attrs.first.class == Array
34
+ attrs.each do |attr|
35
+ define_attribute(attr)
36
+ end
31
37
  end
32
38
 
33
- def clear_data
34
- self.class._attributes_data = {}
39
+ def define_attribute(attr, options = {}, &block)
40
+ key = options.fetch(:key, attr)
41
+ _attributes_data[key] = Attribute.new(attr, options, block)
42
+ end
43
+
44
+ def make_all_attributes
45
+ define_default_attrs
46
+ define_group_attrs
35
47
  end
36
48
 
37
49
  def define_default_attrs
38
50
  return unless self.class.data_default_attributes
39
- self.class.data_default_attributes.call
51
+ self.instance_eval &self.class.data_default_attributes
40
52
  end
41
53
 
42
54
  def define_group_attrs
43
- return if !self.class.groups or !self.class.groups.keys.include?(group_name)
44
- self.class.groups[group_name].each { |block| block.call }
55
+ self.class.groups.send(:[], group_name)&.each do |block|
56
+ self.instance_eval(&block)
57
+ end
58
+ end
59
+
60
+ #override serializer methods
61
+
62
+ def attributes(requested_attrs = nil, reload = false)
63
+ @attributes = nil if reload
64
+ @attributes ||= _attributes_data.each_with_object({}) do |(key, attr), hash|
65
+ next if attr.excluded?(self)
66
+ next unless requested_attrs.nil? || requested_attrs.include?(key)
67
+ hash[key] = attr.value(self)
68
+ end
45
69
  end
46
70
  end
47
71
  end
@@ -1,3 +1,3 @@
1
1
  module Flexserializer
2
- VERSION = "1.2.4"
2
+ VERSION = "1.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexserializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.4
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - woodcrust
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-01 00:00:00.000000000 Z
11
+ date: 2017-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers