Class: OpenUSD::Path

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/openusd/path.rb

Overview

Immutable SdfPath-like value object for prim and property paths.

Constant Summary collapse

IDENTIFIER =

Reusable USD identifier fragment.

/[\p{L}_][\p{L}\p{N}_]*/u
PRIM_NAME =

Validation expression for prim names.

/\A#{IDENTIFIER}\z/u
PROPERTY_NAME =

Validation expression for namespaced property names.

/\A#{IDENTIFIER}(?::#{IDENTIFIER})*\z/u

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Path

Returns a new instance of Path.



31
32
33
34
35
36
# File 'lib/openusd/path.rb', line 31

def initialize(value)
  @value = value.dup.freeze
  @property_name = split_property
  validate!
  freeze
end

Instance Attribute Details

#property_nameObject (readonly)

Returns the value of attribute property_name.



15
16
17
# File 'lib/openusd/path.rb', line 15

def property_name
  @property_name
end

Class Method Details

.parse(value) ⇒ Path

Parse a path string.

Parameters:

  • value (String, Path)

Returns:

Raises:



22
23
24
25
26
27
28
# File 'lib/openusd/path.rb', line 22

def parse(value)
  return value if value.is_a?(self)

  new(String(value))
rescue ArgumentError, TypeError
  raise PathError, "path must be a string or Path"
end

Instance Method Details

#<=>(other) ⇒ Object

Compare paths by their canonical string form.



99
100
101
102
103
# File 'lib/openusd/path.rb', line 99

def <=>(other)
  @value <=> self.class.parse(other).to_s
rescue PathError
  nil
end

#absolute?Boolean

Whether this path begins at the pseudo-root.

Returns:

  • (Boolean)


39
40
41
# File 'lib/openusd/path.rb', line 39

def absolute?
  @value.start_with?("/")
end

#child(name) ⇒ Path

Append a child prim name.

Parameters:

  • name (String)

Returns:



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/openusd/path.rb', line 72

def child(name)
  raise PathError, "a property path cannot have prim children" if property?

  child_name = String(name)
  raise PathError, "invalid prim name: #{child_name.inspect}" unless PRIM_NAME.match?(child_name)

  separator = @value == "/" ? "" : "/"
  self.class.parse("#{@value}#{separator}#{child_name}")
rescue ArgumentError, TypeError
  raise PathError, "child name must be a string"
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


105
106
107
# File 'lib/openusd/path.rb', line 105

def eql?(other)
  other.is_a?(self.class) && @value == other.to_s
end

#hashInteger

Returns value hash compatible with #eql?.

Returns:

  • (Integer)

    value hash compatible with #eql?



111
112
113
# File 'lib/openusd/path.rb', line 111

def hash
  @value.hash
end

#inspectString

Returns developer representation.

Returns:

  • (String)

    developer representation



121
122
123
# File 'lib/openusd/path.rb', line 121

def inspect
  "#<#{self.class} #{@value.inspect}>"
end

#parentPath?

Return the namespace parent, or nil when no parent exists.

Returns:



58
59
60
61
62
63
64
65
66
67
# File 'lib/openusd/path.rb', line 58

def parent
  return prim_path if property?
  return nil if @value == "/"

  separator = @value.rindex("/")
  return nil unless separator
  return self.class.parse("/") if separator.zero?

  self.class.parse(@value[0...separator])
end

#prim_pathPath

Return the prim portion of this path.

Returns:



50
51
52
53
54
# File 'lib/openusd/path.rb', line 50

def prim_path
  return self unless property?

  self.class.parse(@value.delete_suffix(".#{property_name}"))
end

#property(name) ⇒ Path

Append a property name.

Parameters:

  • name (String)

Returns:



87
88
89
90
91
92
93
94
95
96
# File 'lib/openusd/path.rb', line 87

def property(name)
  raise PathError, "a property path cannot have another property" if property?

  property = String(name)
  raise PathError, "invalid property name: #{property.inspect}" unless PROPERTY_NAME.match?(property)

  self.class.parse("#{@value}.#{property}")
rescue ArgumentError, TypeError
  raise PathError, "property name must be a string"
end

#property?Boolean

Whether this identifies a property rather than a prim.

Returns:

  • (Boolean)


44
45
46
# File 'lib/openusd/path.rb', line 44

def property?
  !property_name.nil?
end

#to_sString

Returns canonical path text.

Returns:

  • (String)

    canonical path text



116
117
118
# File 'lib/openusd/path.rb', line 116

def to_s
  @value
end