Class: JSONAPI::PathSegment

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/path_segment.rb

Defined Under Namespace

Classes: Field, Relationship

Class Method Summary collapse

Class Method Details

.parse(source_resource_klass:, segment_string:, parse_fields: true) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jsonapi/path_segment.rb', line 3

def self.parse(source_resource_klass:, segment_string:, parse_fields: true)
  first_part, last_part = segment_string.split('#', 2)
  relationship = source_resource_klass._relationship(first_part)

  if relationship
    if last_part
      unless relationship.resource_types.include?(last_part)
        raise JSONAPI::Exceptions::InvalidRelationship.new(source_resource_klass._type, segment_string)
      end
      resource_klass = source_resource_klass.resource_klass_for(last_part)
    end
    return PathSegment::Relationship.new(relationship: relationship, resource_klass: resource_klass)
  else
    if last_part.blank? && parse_fields
      return PathSegment::Field.new(resource_klass: source_resource_klass, field_name: first_part)
    else
      raise JSONAPI::Exceptions::InvalidRelationship.new(source_resource_klass._type, segment_string)
    end
  end
end