Sha256: b49764f638630ae8ad0d21cab744475c198cff6bd78d11f2a85d43e37aa8b692
Contents?: true
Size: 1.45 KB
Versions: 14
Compression:
Stored size: 1.45 KB
Contents
module JSONAPI class IncludeDirectives # Construct an IncludeDirectives Hash from an array of dot separated include strings. # For example ['posts.comments.tags'] # will transform into => # { # posts: { # include_related: { # comments:{ # include_related: { # tags: { # include_related: {} # } # } # } # } # } # } def initialize(resource_klass, includes_array) @resource_klass = resource_klass @include_directives_hash = { include_related: {} } includes_array.each do |include| parse_include(include) end end def include_directives @include_directives_hash end private def parse_include(include) path = JSONAPI::Path.new(resource_klass: @resource_klass, path_string: include, ensure_default_field: false, parse_fields: false) current = @include_directives_hash path.segments.each do |segment| relationship_name = segment.relationship.name.to_sym current[:include_related][relationship_name] ||= { include_related: {} } current = current[:include_related][relationship_name] end rescue JSONAPI::Exceptions::InvalidRelationship => _e raise JSONAPI::Exceptions::InvalidInclude.new(@resource_klass, include) end end end
Version data entries
14 entries across 14 versions & 1 rubygems