fields-serializer 0.6.1 → 0.6.2

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: 968cff087294a5ca3abe9dba8156639eb2a60c7a
4
- data.tar.gz: 32764dc22f65232dce5c1bfba8940b968e49245f
3
+ metadata.gz: a4eb7b537453470e5785c408759a7cefd465931b
4
+ data.tar.gz: 6aeab9610fdc44c06d6555f7a2a2f32d0ad3f7ba
5
5
  SHA512:
6
- metadata.gz: 46e56499debd139d9292746fac8e46ecc112a73a5f61811cbd1acbe31ab95e3cf437c2d4aad3a2ba7c1a52674c5c619d62ac66cfee2319ddf1c49d3f12e1e783
7
- data.tar.gz: 7e0709ed97e68ccadf161a2329bdf6580b70b78fd5c76256814bf44901e1f969c39a7feb21cfeb92c98124780b76afe465aff2fb73498038f23636a9049dd872
6
+ metadata.gz: 11d02a736631a8580b55a550718a70315354f48a68a3afa742db813145da57e179c62a376d6ea53095e8b8829437cd580d03e93f7386e42c96cebd48852d4b53
7
+ data.tar.gz: 04c6ea5ce89247bb960c760f1efff1eb4713f719ef2dd53f629571bc4a1e26663cd640afe07a769659a8b285017e81ce4b26fed0ae4231496311456f82a5cff1
@@ -4,6 +4,7 @@ require "active_model_serializers"
4
4
  module Fields
5
5
  module Serializer
6
6
 
7
+ # A class to store a tree structure of a model klass, its attributes and associations.
7
8
  class FieldsTree
8
9
  attr_reader :klass, :fields, :associations
9
10
 
@@ -17,6 +18,14 @@ module Fields
17
18
  self if fields.present? || associations.present?
18
19
  end
19
20
 
21
+ # Adds a new field (json api notation) to the tree structure:
22
+ #
23
+ # user_tree.notation
24
+ # #=> [:name, :surname, { subjects: [:title, { posts: { comments: :count } }], followers: :nickname }]
25
+ #
26
+ # user_tree.merge!("subjects.posts.date").notation
27
+ # #=> [:name, :surname, { subjects: [:title, { posts: [{ comments: :count }, :date] }], followers: :nickname }]
28
+ #
20
29
  def merge!(join_field)
21
30
  return self unless join_field.present?
22
31
  parent, rest = join_field.to_s.split(".", 2)
@@ -28,6 +37,11 @@ module Fields
28
37
  self
29
38
  end
30
39
 
40
+ # Return the tree structure in Rails includes notation including both associations and fields
41
+ #
42
+ # user_tree.notation
43
+ # #=> [:name, :surname, { subjects: [:title, { posts: :comments }], followers: :nickname }]
44
+ #
31
45
  def notation
32
46
  if fields.present?
33
47
  if associations.present?
@@ -39,7 +53,12 @@ module Fields
39
53
  associations_to_notation.presence
40
54
  end
41
55
  end
42
-
56
+
57
+ # Return the tree structure in Rails includes notation including only associations
58
+ #
59
+ # user_tree.notation
60
+ # #=> [{ subjects: { posts: :comments }}, :followers]
61
+ #
43
62
  def to_includes
44
63
  to_includes = associations.inject([]) do |result, (k, v)|
45
64
  v_includes = v.to_includes
@@ -1,5 +1,5 @@
1
1
  module Fields
2
2
  module Serializer
3
- VERSION = "0.6.1"
3
+ VERSION = "0.6.2"
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.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stuart Chinery