Class: HRX::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/hrx/directory.rb

Overview

A directory in an HRX archive.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, comment: nil) ⇒ Directory

Creates a new file with the given paths and comment.

Throws an HRX::ParseError if ‘path` is invalid, or an EncodingError if either argument can’t be converted to UTF-8.

The ‘path` may or may not end with a `/`. If it doesn’t a ‘/` will be added.



41
42
43
44
45
46
# File 'lib/hrx/directory.rb', line 41

def initialize(path, comment: nil)
  @comment = comment&.clone&.encode("UTF-8")&.freeze
  @path = HRX::Util.scan_path(StringScanner.new(path.encode("UTF-8")))
  @path << "/" unless @path.end_with?("/")
  @path.freeze
end

Instance Attribute Details

#commentObject (readonly)

The comment that appeared before this directory, or ‘nil` if it had no preceding comment.

HRX file contents are always encoded as UTF-8.

This string is frozen.



25
26
27
# File 'lib/hrx/directory.rb', line 25

def comment
  @comment
end

#pathObject (readonly)

The path to this file, relative to the archive’s root, including the trailing ‘/`.

HRX paths are always ‘/`-separated and always encoded as UTF-8.

This string is frozen.



33
34
35
# File 'lib/hrx/directory.rb', line 33

def path
  @path
end

Class Method Details

._new_without_checks(path, comment) ⇒ Object

Like ::new, but doesn’t verify that the arguments are valid.



49
50
51
52
53
# File 'lib/hrx/directory.rb', line 49

def self._new_without_checks(path, comment) # :nodoc:
  allocate.tap do |dir|
    dir._initialize_without_checks(path, comment)
  end
end

Instance Method Details

#_absolute(root) ⇒ Object

Returns a copy of this entry with ‘root` added tothe beginning of the path.

If ‘root` is `nil`, returns this as-is.



73
74
75
76
# File 'lib/hrx/directory.rb', line 73

def _absolute(root) # :nodoc:
  return self unless root
  HRX::Directory._new_without_checks(root + path, comment)
end

#_initialize_without_checks(path, comment) ⇒ Object

Like #initialize, but doesn’t verify that the arguments are valid.



56
57
58
59
# File 'lib/hrx/directory.rb', line 56

def _initialize_without_checks(path, comment) # :nodoc:
  @comment = comment.freeze
  @path = path.freeze
end

#_relative(root) ⇒ Object

Returns a copy of this entry with the path modified to be relative to ‘root`.

If ‘root` is `nil`, returns this as-is.



65
66
67
68
# File 'lib/hrx/directory.rb', line 65

def _relative(root) # :nodoc:
  return self unless root
  HRX::Directory._new_without_checks(HRX::Util.relative(root, path), comment)
end