Class: Schmersion::Formatters::YAML

Inherits:
Schmersion::Formatter show all
Defined in:
lib/schmersion/formatters/yaml.rb

Constant Summary collapse

DEFAULT_STRUCTURE =
[].to_yaml.freeze

Instance Attribute Summary

Attributes inherited from Schmersion::Formatter

#filename

Instance Method Summary collapse

Methods inherited from Schmersion::Formatter

#initialize

Constructor Details

This class inherits a constructor from Schmersion::Formatter

Instance Method Details

#generate(_, version) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/schmersion/formatters/yaml.rb', line 12

def generate(_, version)
  commits = version.commits.sort_by { |c| c.message.description.upcase }

  commits = commits.each_with_object([]) do |commit, array|
    next unless include_type?(commit.message.type)

    array << commit_to_hash(commit)
  end

  {
    'version' => version.version.to_s,
    'commits' => commits
  }.to_yaml
end

#insert(part) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/schmersion/formatters/yaml.rb', line 27

def insert(part)
  unless File.file?(@filename)
    File.write(@filename, DEFAULT_STRUCTURE)
  end

  part_as_hash = ::YAML.safe_load(part)
  existing_yaml = ::YAML.load_file(@filename)
  existing_yaml = [] unless existing_yaml.is_a?(Array)
  existing_yaml.prepend(part_as_hash)
  File.write(@filename, existing_yaml.to_yaml)
end