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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3749f7978bc0bc289332da7da45d64ad75727c1b
4
- data.tar.gz: 40fba01616492f5037fd2adcc64169f284ef357e
3
+ metadata.gz: 979100e56ed56a77928ecff0210714f950433fb8
4
+ data.tar.gz: 9c132b27819571afebe6832993aefcc5958f5a7a
5
5
  SHA512:
6
- metadata.gz: 8eebebd61086ccb38f862547533e5588ec9aeaa847d37970e23b219c7d3e7eae071813458d1a552d7b3a9c39ae807644d2f0f1d2ad15880dd208f7f3e666ea44
7
- data.tar.gz: 752a6ac3c958b0015c069fae81c272c2c2464ea324a97ad40df47b45407e670b3cf67d16babc6e3bd119af8103b9803e1029085157d6fbe25d6c544a7e229e5f
6
+ metadata.gz: 230c9d488326bf2bc667a76481bb17e4f5f9fb887d2d85185a60d6eb594a0327a22e0371ef606c370ad58491cd0ca08886bbb89a35959a02e7c02e202d87f740
7
+ data.tar.gz: 728ccec0f83f07e06de5f04b79b5d9583e22a60ea03917512f5be7a95a48a090d7aa9dabcd7e8b8ab58ef2bd3c100fa6ee2270a4392704b400ce1d1ab69d7cb4
@@ -19,13 +19,13 @@ module Flexserializer
19
19
  end
20
20
  end
21
21
 
22
- attr_reader :group_name, :_attributes_data, :define_options
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
@@ -1,3 +1,3 @@
1
1
  module Flexserializer
2
- VERSION = "1.3.0"
2
+ VERSION = "1.3.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexserializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - woodcrust