class JSONAPI::IncludeDirectives

Public Class Methods

new(resource_klass, includes_array) click to toggle source

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: {}
        }
      }
    }
  }
}

}

# File lib/jsonapi/include_directives.rb, line 20
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

Public Instance Methods

include_directives() click to toggle source
# File lib/jsonapi/include_directives.rb, line 28
def include_directives
  @include_directives_hash
end

Private Instance Methods

parse_include(include) click to toggle source
# File lib/jsonapi/include_directives.rb, line 34
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