Class: Gitt::Parsers::Trailer

Inherits:
Object
  • Object
show all
Defined in:
lib/gitt/parsers/trailer.rb

Overview

Parses raw trailer data to produce a trailer record.

Constant Summary collapse

PATTERN =
/
  \A                   # Start of line.
  (?<key>[a-zA-Z\-]+)  # Key.
  (?<delimiter>:)      # Delimiter (colon).
  (?<space>\s?)        # Space (optional).
  (?<value>.*?)        # Value.
  \Z                   # End of line.
/x
EMPTY =
Models::Trailer[key: nil, value: nil]

Instance Method Summary collapse

Constructor Details

#initialize(pattern: PATTERN, model: Models::Trailer, empty: EMPTY) ⇒ Trailer

Returns a new instance of Trailer.



18
19
20
21
22
# File 'lib/gitt/parsers/trailer.rb', line 18

def initialize pattern: PATTERN, model: Models::Trailer, empty: EMPTY
  @pattern = pattern
  @model = model
  @empty = empty
end

Instance Method Details

#call(content) ⇒ Object



24
25
26
27
28
29
# File 'lib/gitt/parsers/trailer.rb', line 24

def call content
  return empty if content.start_with? "#"

  content.match(pattern)
         .then { |data| data ? model[**data.named_captures(symbolize_names: true)] : empty }
end