Class: JSONAPI::Path
- Inherits:
-
Object
- Object
- JSONAPI::Path
- Defined in:
- lib/jsonapi/path.rb
Instance Attribute Summary collapse
-
#resource_klass ⇒ Object
readonly
Returns the value of attribute resource_klass.
-
#segments ⇒ Object
readonly
Returns the value of attribute segments.
Instance Method Summary collapse
-
#initialize(resource_klass:, path_string:, ensure_default_field: true, parse_fields: true) ⇒ Path
constructor
A new instance of Path.
- #last_relationship ⇒ Object
- #relationship_path_string ⇒ Object
- #relationship_segments ⇒ Object
Constructor Details
#initialize(resource_klass:, path_string:, ensure_default_field: true, parse_fields: true) ⇒ Path
Returns a new instance of Path.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/jsonapi/path.rb', line 6 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_klass ⇒ Object (readonly)
Returns the value of attribute resource_klass.
5 6 7 |
# File 'lib/jsonapi/path.rb', line 5 def resource_klass @resource_klass end |
#segments ⇒ Object (readonly)
Returns the value of attribute segments.
5 6 7 |
# File 'lib/jsonapi/path.rb', line 5 def segments @segments end |
Instance Method Details
#last_relationship ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/jsonapi/path.rb', line 37 def last_relationship if @segments.last.is_a?(PathSegment::Relationship) @segments.last else @segments[-2] end end |
#relationship_path_string ⇒ Object
33 34 35 |
# File 'lib/jsonapi/path.rb', line 33 def relationship_path_string relationship_segments.collect(&:to_s).join('.') end |
#relationship_segments ⇒ Object
29 30 31 |
# File 'lib/jsonapi/path.rb', line 29 def relationship_segments @segments.select {|p| p.is_a?(PathSegment::Relationship)} end |