Class: AbcJsonapi::IncludedResource
- Inherits:
-
Object
- Object
- AbcJsonapi::IncludedResource
- Defined in:
- lib/abc_jsonapi/included_resource.rb
Instance Attribute Summary collapse
-
#includes ⇒ Object
readonly
Returns the value of attribute includes.
-
#includes_result ⇒ Object
readonly
Returns the value of attribute includes_result.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#serializer_namespace ⇒ Object
readonly
Returns the value of attribute serializer_namespace.
Instance Method Summary collapse
- #get_included_records(resource, include_chain) ⇒ Object
- #included_items_from_collection(collection, include_name) ⇒ Object
-
#initialize(resource:, includes:, serializer_namespace:) ⇒ IncludedResource
constructor
A new instance of IncludedResource.
- #serializable_hash ⇒ Object
- #serializer(included_resource_name) ⇒ Object
Constructor Details
#initialize(resource:, includes:, serializer_namespace:) ⇒ IncludedResource
Returns a new instance of IncludedResource.
5 6 7 8 9 10 |
# File 'lib/abc_jsonapi/included_resource.rb', line 5 def initialize(resource:, includes:, serializer_namespace:) @includes_result = [] @resource = resource @includes = includes @serializer_namespace = serializer_namespace end |
Instance Attribute Details
#includes ⇒ Object (readonly)
Returns the value of attribute includes.
3 4 5 |
# File 'lib/abc_jsonapi/included_resource.rb', line 3 def includes @includes end |
#includes_result ⇒ Object (readonly)
Returns the value of attribute includes_result.
3 4 5 |
# File 'lib/abc_jsonapi/included_resource.rb', line 3 def includes_result @includes_result end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
3 4 5 |
# File 'lib/abc_jsonapi/included_resource.rb', line 3 def resource @resource end |
#serializer_namespace ⇒ Object (readonly)
Returns the value of attribute serializer_namespace.
3 4 5 |
# File 'lib/abc_jsonapi/included_resource.rb', line 3 def serializer_namespace @serializer_namespace end |
Instance Method Details
#get_included_records(resource, include_chain) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/abc_jsonapi/included_resource.rb', line 26 def get_included_records(resource, include_chain) return if resource.nil? || include_chain.empty? # Get first include name of include_chain and delete it from include_chain inc_resource_name = include_chain.shift # Check if include name is exist in relationships array. Take it or return nil resource_class_name = resource.is_a?(Enumerable) ? resource[0].class.name : resource.class.name relationship = serializer(resource_class_name).relationships.find { |h| h[:name] == inc_resource_name.to_sym } return if relationship.nil? # Get included resource if resource.is_a?(Enumerable) resource = included_items_from_collection(resource, inc_resource_name) else resource = resource.public_send(inc_resource_name) end return if resource.nil? @includes_result << serializer(inc_resource_name).new(resource).serializable_hash[:data] # If resource is a collection call get_included_records for each. Otherwise send whole resource if resource.is_a?(Enumerable) resource.each do |single_res| get_included_records(single_res, include_chain.dup) end else get_included_records(resource, include_chain) end end |
#included_items_from_collection(collection, include_name) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/abc_jsonapi/included_resource.rb', line 57 def included_items_from_collection(collection, include_name) # Run custom include strategy if block given. Otherwise run default method if (block = relationship.dig(:block)).present? block.call(resource) else collection.map(&:include_name).flatten.uniq end end |
#serializable_hash ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/abc_jsonapi/included_resource.rb', line 12 def serializable_hash includes.each do |include_path| include_chain = include_path.split('.') if resource.is_a?(Enumerable) resource.each do |single_res| get_included_records(single_res, include_chain.dup) end else get_included_records(resource, include_chain) end end includes_result.flatten end |
#serializer(included_resource_name) ⇒ Object
66 67 68 |
# File 'lib/abc_jsonapi/included_resource.rb', line 66 def serializer(included_resource_name) (serializer_namespace + included_resource_name.to_s.classify + 'Serializer').constantize end |