Class: JSONAPI::Path

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_klass:, path_string:, ensure_default_field: true, parse_fields: true) ⇒ Path

Returns a new instance of Path.



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

def initialize(resource_klass:,
               path_string:,
               ensure_default_field: true,
               parse_fields: true)
  @resource_klass = resource_klass

  current_resource_klass = resource_klass
  @segments = path_string.to_s.split('.').collect do |segment_string|
    segment = PathSegment.parse(source_resource_klass: current_resource_klass,
                             segment_string: segment_string,
                             parse_fields: parse_fields)

    current_resource_klass = segment.resource_klass
    segment
  end

  if ensure_default_field && parse_fields && @segments.last.is_a?(PathSegment::Relationship)
    last = @segments.last
    @segments << PathSegment::Field.new(resource_klass: last.resource_klass,
                                        field_name: last.resource_klass._primary_key)
  end
end

Instance Attribute Details

#resource_klassObject (readonly)

Returns the value of attribute resource_klass.



3
4
5
# File 'lib/jsonapi/path.rb', line 3

def resource_klass
  @resource_klass
end

#segmentsObject (readonly)

Returns the value of attribute segments.



3
4
5
# File 'lib/jsonapi/path.rb', line 3

def segments
  @segments
end

Instance Method Details

#last_relationshipObject



35
36
37
38
39
40
41
# File 'lib/jsonapi/path.rb', line 35

def last_relationship
  if @segments.last.is_a?(PathSegment::Relationship)
    @segments.last
  else
    @segments[-2]
  end
end

#relationship_path_stringObject



31
32
33
# File 'lib/jsonapi/path.rb', line 31

def relationship_path_string
  relationship_segments.collect(&:to_s).join('.')
end

#relationship_segmentsObject



27
28
29
# File 'lib/jsonapi/path.rb', line 27

def relationship_segments
  @segments.select {|p| p.is_a?(PathSegment::Relationship)}
end