Class: Fields::Serializer::FieldsTree
- Inherits:
-
Object
- Object
- Fields::Serializer::FieldsTree
- Defined in:
- lib/fields/serializer/fields_tree.rb
Overview
A class to store a tree structure of a model klass, its attributes and associations.
Instance Attribute Summary collapse
-
#associations ⇒ Object
readonly
Returns the value of attribute associations.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
-
#initialize(klass) ⇒ FieldsTree
constructor
A new instance of FieldsTree.
-
#merge!(join_field) ⇒ Object
Adds a new field (json api notation) to the tree structure:.
-
#notation ⇒ Object
Return the tree structure in Rails includes notation including both associations and fields.
-
#presence ⇒ Object
Self if any fields or associations.
-
#to_includes ⇒ Object
Return the tree structure in Rails includes notation including only associations.
- #to_s ⇒ Object
Constructor Details
#initialize(klass) ⇒ FieldsTree
Returns a new instance of FieldsTree.
11 12 13 14 15 |
# File 'lib/fields/serializer/fields_tree.rb', line 11 def initialize(klass) @klass = klass @fields = [] @associations = {} end |
Instance Attribute Details
#associations ⇒ Object (readonly)
Returns the value of attribute associations.
9 10 11 |
# File 'lib/fields/serializer/fields_tree.rb', line 9 def associations @associations end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
9 10 11 |
# File 'lib/fields/serializer/fields_tree.rb', line 9 def fields @fields end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
9 10 11 |
# File 'lib/fields/serializer/fields_tree.rb', line 9 def klass @klass end |
Instance Method Details
#merge!(join_field) ⇒ Object
Adds a new field (json api notation) to the tree structure:
user_tree.notation
#=> [:name, :surname, { subjects: [:title, { posts: { comments: :count } }], followers: :nickname }]
user_tree.merge!("subjects.posts.date").notation
#=> [:name, :surname, { subjects: [:title, { posts: [{ comments: :count }, :date] }], followers: :nickname }]
30 31 32 33 34 35 |
# File 'lib/fields/serializer/fields_tree.rb', line 30 def merge!(join_field) return self if join_field.blank? parent, rest = join_field.to_s.split(".", 2) rest.present? ? add_association!(parent, rest) : add_field!(parent) self end |
#notation ⇒ Object
Return the tree structure in Rails includes notation including both associations and fields
user_tree.notation
#=> [:name, :surname, { subjects: [:title, { posts: :comments }], followers: :nickname }]
42 43 44 45 46 47 48 49 |
# File 'lib/fields/serializer/fields_tree.rb', line 42 def notation return associations_to_notation.presence if fields.blank? if associations.present? fields.dup << associations_to_notation else fields.one? ? fields.first.dup : fields.dup end end |
#presence ⇒ Object
Self if any fields or associations. Nil otherwise
18 19 20 |
# File 'lib/fields/serializer/fields_tree.rb', line 18 def presence self if fields.present? || associations.present? end |
#to_includes ⇒ Object
Return the tree structure in Rails includes notation including only associations
user_tree.notation
#=> [{ subjects: { posts: :comments }}, :followers]
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fields/serializer/fields_tree.rb', line 56 def to_includes to_includes = associations.inject([]) do |result, (association_name, association_tree)| association_includes = association_tree.to_includes if association_includes.present? add_association_includes_to_includes!(result, association_name, association_includes) else add_association_to_includes!(result, association_name) end end.presence Array.wrap(to_includes).one? ? to_includes.first : to_includes end |
#to_s ⇒ Object
68 69 70 |
# File 'lib/fields/serializer/fields_tree.rb', line 68 def to_s notation.to_s end |