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.
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_klass ⇒ Object (readonly)
Returns the value of attribute resource_klass.
3 4 5 |
# File 'lib/jsonapi/path.rb', line 3 def resource_klass @resource_klass end |
#segments ⇒ Object (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_relationship ⇒ Object
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_string ⇒ Object
31 32 33 |
# File 'lib/jsonapi/path.rb', line 31 def relationship_path_string relationship_segments.collect(&:to_s).join('.') end |
#relationship_segments ⇒ Object
27 28 29 |
# File 'lib/jsonapi/path.rb', line 27 def relationship_segments @segments.select {|p| p.is_a?(PathSegment::Relationship)} end |