fields-serializer 0.6.2 → 0.6.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4eb7b537453470e5785c408759a7cefd465931b
4
- data.tar.gz: 6aeab9610fdc44c06d6555f7a2a2f32d0ad3f7ba
3
+ metadata.gz: 0a40d814b11b8a789f079dd17c01cd6da9ef08a9
4
+ data.tar.gz: b107747d4a6d73b2e23616db6af21ad986108e96
5
5
  SHA512:
6
- metadata.gz: 11d02a736631a8580b55a550718a70315354f48a68a3afa742db813145da57e179c62a376d6ea53095e8b8829437cd580d03e93f7386e42c96cebd48852d4b53
7
- data.tar.gz: 04c6ea5ce89247bb960c760f1efff1eb4713f719ef2dd53f629571bc4a1e26663cd640afe07a769659a8b285017e81ce4b26fed0ae4231496311456f82a5cff1
6
+ metadata.gz: a10e3adab1752e273bbef998e232cf8e9a466cfe92ec8cfc1ae7c7b2418fe3b3e1167265380e3119de7c4ab3b4b03991e61ea1ceda9613a2670df3728f0db004
7
+ data.tar.gz: 2b5c1a57588956d70ca910535bbe8cc0de4100ad0b3152e5fb04d078fbc1c129a0316f092ab00fd12cde9c667be7281c27b6d3473c1b54324b589a92753aa25a
@@ -14,6 +14,7 @@ module Fields
14
14
  @associations = {}
15
15
  end
16
16
 
17
+ # Self if any fields or associations. Nil otherwise
17
18
  def presence
18
19
  self if fields.present? || associations.present?
19
20
  end
@@ -27,13 +28,9 @@ module Fields
27
28
  # #=> [:name, :surname, { subjects: [:title, { posts: [{ comments: :count }, :date] }], followers: :nickname }]
28
29
  #
29
30
  def merge!(join_field)
30
- return self unless join_field.present?
31
+ return self if join_field.blank?
31
32
  parent, rest = join_field.to_s.split(".", 2)
32
- if rest.blank?
33
- fields << parent if !(existing_field?(parent) || association?(parent))
34
- else
35
- existing_association?(parent) ? associations[parent].merge!(rest) : add_association!(parent, rest)
36
- end
33
+ rest.present? ? add_association!(parent, rest) : add_field!(parent)
37
34
  self
38
35
  end
39
36
 
@@ -43,14 +40,11 @@ module Fields
43
40
  # #=> [:name, :surname, { subjects: [:title, { posts: :comments }], followers: :nickname }]
44
41
  #
45
42
  def notation
46
- if fields.present?
47
- if associations.present?
48
- fields.dup << associations_to_notation
49
- else
50
- fields.one? ? fields.first.dup : fields.dup
51
- end
43
+ return associations_to_notation.presence if fields.blank?
44
+ if associations.present?
45
+ fields.dup << associations_to_notation
52
46
  else
53
- associations_to_notation.presence
47
+ fields.one? ? fields.first.dup : fields.dup
54
48
  end
55
49
  end
56
50
 
@@ -60,15 +54,12 @@ module Fields
60
54
  # #=> [{ subjects: { posts: :comments }}, :followers]
61
55
  #
62
56
  def to_includes
63
- to_includes = associations.inject([]) do |result, (k, v)|
64
- v_includes = v.to_includes
65
- if v_includes.present?
66
- new_has_entry = { k => v_includes }
67
- hash = result.find { |e| e.is_a?(Hash) }
68
- hash ? hash.merge!(new_has_entry) : (result << new_has_entry)
69
- result
57
+ to_includes = associations.inject([]) do |result, (association_name, association_tree)|
58
+ association_includes = association_tree.to_includes
59
+ if association_includes.present?
60
+ add_association_includes_to_includes!(result, association_name, association_includes)
70
61
  else
71
- result << k
62
+ add_association_to_includes!(result, association_name)
72
63
  end
73
64
  end.presence
74
65
  Array.wrap(to_includes).one? ? to_includes.first : to_includes
@@ -81,6 +72,21 @@ module Fields
81
72
  private
82
73
 
83
74
  def add_association!(parent, rest)
75
+ existing_association?(parent) ? merge_association!(parent, rest) : append_association!(parent, rest)
76
+ end
77
+
78
+ def add_association_includes_to_includes!(includes, association_name, association_includes)
79
+ new_has_entry = { association_name => association_includes }
80
+ includes_hash = includes.find { |e| e.is_a?(Hash) }
81
+ includes_hash ? includes_hash.merge!(new_has_entry) : (includes << new_has_entry)
82
+ includes
83
+ end
84
+
85
+ def add_association_to_includes!(includes, association_name)
86
+ includes << association_name
87
+ end
88
+
89
+ def append_association!(parent, rest)
84
90
  if association?(parent)
85
91
  nested_class = klass.reflections[parent].klass
86
92
  nested_fields_tree = FieldsTree.new(nested_class).merge!(rest).presence
@@ -88,6 +94,14 @@ module Fields
88
94
  associations.merge!(new_association) if new_association
89
95
  end
90
96
  end
97
+
98
+ def add_field!(value)
99
+ fields << value if new_field?(value)
100
+ end
101
+
102
+ def association?(value)
103
+ klass.reflections.keys.include?(value)
104
+ end
91
105
 
92
106
  def associations_to_notation
93
107
  associations.inject({}) do |result, (k, v)|
@@ -103,8 +117,12 @@ module Fields
103
117
  fields.include?(value)
104
118
  end
105
119
 
106
- def association?(value)
107
- klass.reflections.keys.include?(value)
120
+ def merge_association!(key, fields)
121
+ associations[key].merge!(fields)
122
+ end
123
+
124
+ def new_field?(value)
125
+ !existing_field?(value) && !association?(value)
108
126
  end
109
127
  end
110
128
  end
@@ -1,5 +1,5 @@
1
1
  module Fields
2
2
  module Serializer
3
- VERSION = "0.6.2"
3
+ VERSION = "0.6.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fields-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stuart Chinery