Class: Skit::Serialization::Path

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/skit/serialization/path.rb

Constant Summary collapse

Segment =
T.type_alias { T.any(::String, ::Integer) }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segments = []) ⇒ Path

Returns a new instance of Path.



12
13
14
# File 'lib/skit/serialization/path.rb', line 12

def initialize(segments = [])
  @segments = T.let(segments.freeze, T::Array[Segment])
end

Instance Attribute Details

#segmentsObject (readonly)

Returns the value of attribute segments.



22
23
24
# File 'lib/skit/serialization/path.rb', line 22

def segments
  @segments
end

Instance Method Details

#==(other) ⇒ Object



52
53
54
55
56
# File 'lib/skit/serialization/path.rb', line 52

def ==(other)
  return false unless other.is_a?(Path)

  @segments == other.segments
end

#append(segment) ⇒ Object



17
18
19
# File 'lib/skit/serialization/path.rb', line 17

def append(segment)
  Path.new(@segments + [segment])
end

#empty?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/skit/serialization/path.rb', line 25

def empty?
  @segments.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/skit/serialization/path.rb', line 64

def eql?(other)
  self == other
end

#hashObject



59
60
61
# File 'lib/skit/serialization/path.rb', line 59

def hash
  @segments.hash
end

#to_json_pointerObject



45
46
47
48
49
# File 'lib/skit/serialization/path.rb', line 45

def to_json_pointer
  return "" if @segments.empty?

  "/#{@segments.map { |s| s.to_s.gsub("~", "~0").gsub("/", "~1") }.join("/")}"
end

#to_sObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/skit/serialization/path.rb', line 30

def to_s
  result = +""
  @segments.each do |segment|
    case segment
    when ::Integer
      result << "[#{segment}]"
    when ::String
      result << "." unless result.empty?
      result << segment
    end
  end
  result.freeze
end