Class: HRX::File

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

Overview

A file in an HRX archive.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, content, comment: nil) ⇒ File

Creates a new file with the given path, content, and comment.

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



45
46
47
48
49
50
51
52
53
54
# File 'lib/hrx/file.rb', line 45

def initialize(path, content, comment: nil)
  @comment = comment&.clone&.encode("UTF-8")&.freeze
  @path = HRX::Util.scan_path(StringScanner.new(path.encode("UTF-8"))).freeze

  if @path.end_with?("/")
    raise HRX::ParseError.new("path \"#{path}\" may not end with \"/\"", 1, path.length - 1)
  end

  @content = content.clone.encode("UTF-8").freeze
end

Instance Attribute Details

#commentObject (readonly)

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

HRX comments are always encoded as UTF-8.

This string is frozen.



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

def comment
  @comment
end

#contentObject (readonly)

The contents of the file.

HRX file contents are always encoded as UTF-8.

This string is frozen.



39
40
41
# File 'lib/hrx/file.rb', line 39

def content
  @content
end

#pathObject (readonly)

The path to this file, relative to the archive’s root.

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

This string is frozen.



32
33
34
# File 'lib/hrx/file.rb', line 32

def path
  @path
end

Class Method Details

._new_without_checks(path, content, comment) ⇒ Object

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



57
58
59
60
61
# File 'lib/hrx/file.rb', line 57

def self._new_without_checks(path, content, comment) # :nodoc:
  allocate.tap do |file|
    file._initialize_without_checks(path, content, 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.



82
83
84
85
# File 'lib/hrx/file.rb', line 82

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

#_initialize_without_checks(path, content, comment) ⇒ Object

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



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

def _initialize_without_checks(path, content, comment) # :nodoc:
  @comment = comment.freeze
  @path = path.freeze
  @content = content.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.



74
75
76
77
# File 'lib/hrx/file.rb', line 74

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