Class: Txtar::Archive

Inherits:
Object
  • Object
show all
Defined in:
lib/txtar/archive.rb

Overview

An Archive is a collection of files. It might have an optional comment, as well.

Constant Summary collapse

FILE_MARKER =

Each file entry begins with a file marker line of the form “– FILENAME –” and is followed by zero or more file content lines making up the file data.

The comment or file content ends at the next file marker line. The file marker line must begin with the three-byte sequence “– ” and end with the three-byte sequence “ –”, but the enclosed file name can be surrounding by additional white space, all of which is stripped.

See git.io/JLyG7

Regexp.new('^-{2}\s{1}(?<filename>.*)\s{1}-{2}$')
LINE_SEPARATOR =
"\n"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment: nil, files: []) ⇒ Archive

Returns a new instance of Archive.



66
67
68
69
# File 'lib/txtar/archive.rb', line 66

def initialize(comment: nil, files: [])
  @comment = comment
  @files = files
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



22
23
24
# File 'lib/txtar/archive.rb', line 22

def comment
  @comment
end

#filesObject (readonly)

Returns the value of attribute files.



22
23
24
# File 'lib/txtar/archive.rb', line 22

def files
  @files
end

Class Method Details

.parse(data:) ⇒ Object

Parses given String and returns a Txtar::Archive instance.



27
28
29
30
31
32
33
# File 'lib/txtar/archive.rb', line 27

def parse(data:)
  data = add_missing_newline(data.dup)
  lines = data.lines(LINE_SEPARATOR)

  new(comment: extract_comment(lines),
      files: extract_files(lines))
end