Class: JSONAPIonify::Structure::Collections::IncludedResources

Inherits:
Resources
  • Object
show all
Defined in:
lib/jsonapionify/structure/collections/included_resources.rb

Instance Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#<<, #[], #[]=, #collect_hashes, #compile, #each, #errors, #initialize, #new, #original_method, #signature, #validate, value_is, #warnings

Methods included from Helpers::InheritsOrigin

#client?, #origin, #server?

Constructor Details

This class inherits a constructor from JSONAPIonify::Structure::Collections::Base

Class Method Details

.referenceable?(referenced, not_referenced) ⇒ Boolean

Returns:



6
7
8
9
10
11
12
# File 'lib/jsonapionify/structure/collections/included_resources.rb', line 6

def self.referenceable?(referenced, not_referenced)
  not_referenced.any? do |unreferenced_item|
    referenced.any? do |referenced_item|
      referenced_item.relates_to? unreferenced_item
    end
  end
end

Instance Method Details

#referencedObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jsonapionify/structure/collections/included_resources.rb', line 14

def referenced
  return [] unless parent
  top_level_referenced.tap do |referenced|
    not_referenced = reduce([]) { |a, i| a << i }

    while self.class.referenceable?(referenced, not_referenced)
      not_referenced.each do |resource|
        if referenced.any? { |referenced_item| referenced_item.relates_to? resource }
          referenced << not_referenced.delete(resource)
        end
      end
    end
  end
end