Class: Changelog
- Inherits:
-
Object
show all
- Defined in:
- lib/changelog.rb
Overview
Main class that manages the set of actions for managing change logs.
Defined Under Namespace
Classes: Creator, OptionParser, Writer
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(params, source = :file) ⇒ Changelog
Returns a new instance of Changelog.
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/changelog.rb', line 7
def initialize(params, source = :file)
@source = source
@errors = []
if source == :file
@file_path = params
else
@options = params
end
load_data
end
|
Instance Attribute Details
#author ⇒ Object
Returns the value of attribute author.
5
6
7
|
# File 'lib/changelog.rb', line 5
def author
@author
end
|
#errors ⇒ Object
Returns the value of attribute errors.
5
6
7
|
# File 'lib/changelog.rb', line 5
def errors
@errors
end
|
#file_path ⇒ Object
Returns the value of attribute file_path.
5
6
7
|
# File 'lib/changelog.rb', line 5
def file_path
@file_path
end
|
#options ⇒ Object
Returns the value of attribute options.
5
6
7
|
# File 'lib/changelog.rb', line 5
def options
@options
end
|
#source ⇒ Object
Returns the value of attribute source.
5
6
7
|
# File 'lib/changelog.rb', line 5
def source
@source
end
|
#title ⇒ Object
Returns the value of attribute title.
5
6
7
|
# File 'lib/changelog.rb', line 5
def title
@title
end
|
#type ⇒ Object
Returns the value of attribute type.
5
6
7
|
# File 'lib/changelog.rb', line 5
def type
@type
end
|
Instance Method Details
#add_error(message) ⇒ Object
110
111
112
|
# File 'lib/changelog.rb', line 110
def add_error(message)
errors << message
end
|
#author_name ⇒ Object
92
93
94
95
96
|
# File 'lib/changelog.rb', line 92
def author_name
name = "#{`git config user.name`[0..-2]} (#{`git config user.email`[0..-2]})"
name = "#{`git config --global user.name`[0..-2]} (#{`git config --global user.email`[0..-2]})" if name =~ /\A\s*\Z/
name
end
|
#check_data ⇒ Object
43
44
45
46
47
|
# File 'lib/changelog.rb', line 43
def check_data
add_error 'Missing Title' if @title.nil?
add_error 'Missing Type' if @type.nil?
validate_type
end
|
#clear_errors ⇒ Object
55
56
57
|
# File 'lib/changelog.rb', line 55
def clear_errors
@errors = []
end
|
#data ⇒ Object
59
60
61
|
# File 'lib/changelog.rb', line 59
def data
@data ||= load_from_source
end
|
#error_full_messages ⇒ Object
102
103
104
105
106
107
108
|
# File 'lib/changelog.rb', line 102
def error_full_messages
if source == :file
errors.collect { |error| "#{error} at #{file_path}" }
else
errors
end
end
|
#file ⇒ Object
33
34
35
|
# File 'lib/changelog.rb', line 33
def file
@file ||= File.read(file_path)
end
|
#generate_file_name ⇒ Object
122
123
124
125
126
127
|
# File 'lib/changelog.rb', line 122
def generate_file_name
base_path = 'changelogs/unreleased'
FileUtils.mkdir_p(base_path)
safe_name = title.gsub(/[^\w-]/, '-')
"#{base_path}/#{safe_name}.yml"
end
|
#initialize_props_from_options ⇒ Object
80
81
82
83
84
85
86
|
# File 'lib/changelog.rb', line 80
def initialize_props_from_options
{
title: options.title,
type: options.type,
author: author_name
}
end
|
#initialize_props_from_yaml(yaml) ⇒ Object
72
73
74
75
76
77
78
|
# File 'lib/changelog.rb', line 72
def initialize_props_from_yaml(yaml)
{
title: yaml.fetch('title', nil),
type: yaml.fetch('type', nil),
author: yaml.fetch('author', nil)
}
end
|
#load_data ⇒ Object
37
38
39
40
41
|
# File 'lib/changelog.rb', line 37
def load_data
@title = data[:title]
@type = data[:type]
@author = data[:author]
end
|
#load_from_source ⇒ Object
63
64
65
66
67
68
69
70
|
# File 'lib/changelog.rb', line 63
def load_from_source
if source == :file
yaml = YAML.safe_load(file)
initialize_props_from_yaml(yaml)
else
initialize_props_from_options
end
end
|
#save ⇒ Object
114
115
116
117
118
119
120
|
# File 'lib/changelog.rb', line 114
def save
File.open(generate_file_name, 'w') do |file|
file.puts("title: #{title}")
file.puts("author: #{author}")
file.puts("type: #{type}")
end
end
|
#show_errors ⇒ Object
98
99
100
|
# File 'lib/changelog.rb', line 98
def show_errors
errors_full_messages.each { |message| puts message }
end
|
#to_s ⇒ Object
88
89
90
|
# File 'lib/changelog.rb', line 88
def to_s
"Title: #{@title} | Type: #{@type}"
end
|
#valid? ⇒ Boolean
19
20
21
22
23
24
25
|
# File 'lib/changelog.rb', line 19
def valid?
clear_errors
load_data
check_data
errors.empty?
end
|
#validate! ⇒ Object
27
28
29
30
31
|
# File 'lib/changelog.rb', line 27
def validate!
return true if valid?
raise InvalidLogFile, error_full_messages.join(".\n")
end
|
#validate_type ⇒ Object
49
50
51
52
53
|
# File 'lib/changelog.rb', line 49
def validate_type
type = TYPES.find { |t| t.name == @type }
add_error "Invalid Type '#{@type}'" unless type
end
|