diff --git a/lib/standup_md.rb b/lib/standup_md.rb index d39d3d0..d982a35 100644 --- a/lib/standup_md.rb +++ b/lib/standup_md.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true -require_relative 'standup_md/version' -require_relative 'standup_md/file' -require_relative 'standup_md/entry' -require_relative 'standup_md/entry_list' -require_relative 'standup_md/cli' -require_relative 'standup_md/config' +require_relative "standup_md/version" +require_relative "standup_md/file" +require_relative "standup_md/entry" +require_relative "standup_md/entry_list" +require_relative "standup_md/cli" +require_relative "standup_md/config" ## # The main module for the gem. Provides access to configuration classes. diff --git a/lib/standup_md/cli.rb b/lib/standup_md/cli.rb index 622b66b..d88954a 100644 --- a/lib/standup_md/cli.rb +++ b/lib/standup_md/cli.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require 'optparse' -require_relative 'cli/helpers' +require "optparse" +require_relative "cli/helpers" module StandupMD ## @@ -76,18 +76,16 @@ def initialize(options = [], load_config: true) # # @return [nil] def load_preferences - if ::File.exist?(@config.preference_file) - ::StandupMD.load_config_file(@config.preference_file) - @preference_file_loaded = true - else - echo "Preference file #{@config.preference_file} does not exist." - end + ::StandupMD::ConfigFile.new(@config.preference_file) + @preference_file_loaded = true + rescue + echo "Preference file #{@config.preference_file} does not exist." end ## # Has the preference file been loaded? # - # @return boolean + # @return [Boolean] def preference_file_loaded? @preference_file_loaded end @@ -111,7 +109,7 @@ def write_file end ## - # Quick access to Cli.echo. + # Quick access to +Cli.echo+. # # @return [nil] def echo(msg) diff --git a/lib/standup_md/config.rb b/lib/standup_md/config.rb index d15a109..a59c3ea 100644 --- a/lib/standup_md/config.rb +++ b/lib/standup_md/config.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true -require_relative 'config/cli' -require_relative 'config/file' -require_relative 'config/entry' -require_relative 'config/entry_list' +require_relative "config/cli" +require_relative "config/file" +require_relative "config/entry" +require_relative "config/entry_list" module StandupMD ## @@ -36,9 +36,9 @@ class Config ## # Builds the links to the configuration classes. def initialize - @cli = StandupMD::Config::Cli.new - @file = StandupMD::Config::File.new - @entry = StandupMD::Config::Entry.new + @cli = StandupMD::Config::Cli.new + @file = StandupMD::Config::File.new + @entry = StandupMD::Config::Entry.new @entry_list = StandupMD::Config::EntryList.new end end diff --git a/lib/standup_md/config_file.rb b/lib/standup_md/config_file.rb new file mode 100644 index 0000000..30bf9bd --- /dev/null +++ b/lib/standup_md/config_file.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module StandupMD + class ConfigFile + ## + # The name of the instantiated config file. + attr_reader :name + + ## + # @param [String] file_name + def initialize(name) + @name = ::File.expand_path(name) + raise "File #{@name} does not exist." unless ::File.file?(@name) + @loaded = false + end + + ## + # Has the config file been loaded? + # + # @return [Boolean] + def loaded? + @loaded + end + + ## + # Loads the config file. + # + # @return [Boolean] Was the file loaded? + def load + @loaded = load @file + end + end +end diff --git a/lib/standup_md/version.rb b/lib/standup_md/version.rb index fb7ecbe..1e100bc 100644 --- a/lib/standup_md/version.rb +++ b/lib/standup_md/version.rb @@ -23,11 +23,13 @@ module Version # @return [Integer] PATCH = 14 + module_function + ## # Version as +[MAJOR, MINOR, PATCH]+ # # @return [Array] - def self.to_a + def to_a [MAJOR, MINOR, PATCH] end @@ -35,7 +37,7 @@ def self.to_a # Version as +MAJOR.MINOR.PATCH+ # # @return [String] - def self.to_s + def to_s to_a.join('.') end @@ -43,7 +45,7 @@ def self.to_s # Version as +{major: MAJOR, minor: MINOR, patch: PATCH}+ # # @return [Hash] - def self.to_h + def to_h Hash[%i[major minor patch].zip(to_a)] end end diff --git a/test/standup_md/cli_test.rb b/test/standup_md/cli_test.rb index e0be168..062b334 100644 --- a/test/standup_md/cli_test.rb +++ b/test/standup_md/cli_test.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require_relative '../test_helper' -require_relative '../../lib/standup_md' +require_relative "../test_helper" +require_relative "../../lib/standup_md" ## # The test suite for +Cli+. @@ -14,10 +14,10 @@ def setup StandupMD.config.entry.reset StandupMD.config.entry_list.reset StandupMD.config.cli.preference_file = test_config_file_name - StandupMD.instance_variable_set('@config_file_loaded', false) + StandupMD.instance_variable_set(:@config_file_loaded, false) @previous_month_test_file = - File.join(workdir, Date.today.prev_month.strftime('%Y_%m.md')) - @options = ['--no-edit', '--no-write', '--directory', workdir.to_s] + File.join(workdir, Date.today.prev_month.strftime("%Y_%m.md")) + @options = ["--no-edit", "--no-write", "--directory", workdir.to_s] end def teardown @@ -83,7 +83,7 @@ def test_verbose cli(@options) refute(StandupMD.config.cli.verbose) - cli(['--directory', workdir.to_s, '-v'] + @options) + cli(["--directory", workdir.to_s, "-v"] + @options) assert(StandupMD.config.cli.verbose) end @@ -91,7 +91,7 @@ def test_write cli(@options) refute(StandupMD.config.cli.write) - cli(['--directory', workdir.to_s, '--write'] + @options) + cli(["--directory", workdir.to_s, "--write"] + @options) assert(StandupMD.config.cli.write) end @@ -99,7 +99,7 @@ def test_auto_fill_previous assert(StandupMD.config.cli.auto_fill_previous) cli(@options) - cli(['--no-auto-fill-previous'] + @options) + cli(["--no-auto-fill-previous"] + @options) refute(StandupMD.config.cli.auto_fill_previous) end @@ -109,43 +109,43 @@ def test_write_file end def test_current - cli(['--directory', workdir.to_s]) + cli(["--directory", workdir.to_s]) assert_equal([""], StandupMD.config.entry.current) - cli(['--current', 'test', '--directory', workdir.to_s]) + cli(["--current", "test", "--directory", workdir.to_s]) assert_equal(%w[test], StandupMD.config.entry.current) end def test_previous - cli(['--directory', workdir.to_s]) + cli(["--directory", workdir.to_s]) assert_equal([], StandupMD.config.entry.previous) - cli(['--previous', 'test', '--directory', workdir.to_s]) + cli(["--previous", "test", "--directory", workdir.to_s]) assert_equal(%w[test], StandupMD.config.entry.previous) end def test_impediments - cli(['--directory', workdir.to_s]) - assert_equal(['None'], StandupMD.config.entry.impediments) + cli(["--directory", workdir.to_s]) + assert_equal(["None"], StandupMD.config.entry.impediments) - cli(['--impediments', 'test', '--directory', workdir.to_s]) + cli(["--impediments", "test", "--directory", workdir.to_s]) assert_equal(%w[test], StandupMD.config.entry.impediments) end def test_notes - cli(['--directory', workdir.to_s]) + cli(["--directory", workdir.to_s]) assert_equal([], StandupMD.config.entry.notes) - cli(['--notes', 'test', '--directory', workdir.to_s]) + cli(["--notes", "test", "--directory", workdir.to_s]) assert_equal(%w[test], StandupMD.config.entry.notes) end def test_sub_header_order - cli(['--directory', workdir.to_s]) + cli(["--directory", workdir.to_s]) assert_equal(%w[previous current impediments notes], StandupMD.config.file.sub_header_order) cli( - ['--sub-header-order', 'current,previous,notes,impediments', '--directory', workdir.to_s], + ["--sub-header-order", "current,previous,notes,impediments", "--directory", workdir.to_s], load_config: false ) assert_equal( @@ -155,37 +155,37 @@ def test_sub_header_order end def test_file_name_format - cli(['--directory', workdir.to_s], load_config: false) - assert_equal('%Y_%m.md', StandupMD.config.file.name_format) + cli(["--directory", workdir.to_s], load_config: false) + assert_equal("%Y_%m.md", StandupMD.config.file.name_format) - cli(['--file-name-format', '%y_%m.md', '--directory', workdir.to_s]) - assert_equal('%y_%m.md', StandupMD.config.file.name_format) + cli(["--file-name-format", "%y_%m.md", "--directory", workdir.to_s]) + assert_equal("%y_%m.md", StandupMD.config.file.name_format) end def test_editor - ENV['VISUAL'] = 'vim' - cli(['--directory', workdir.to_s]) - assert_equal('vim', StandupMD.config.cli.editor) + ENV["VISUAL"] = "vim" + cli(["--directory", workdir.to_s]) + assert_equal("vim", StandupMD.config.cli.editor) - cli(['--editor', 'mate', '--directory', workdir.to_s]) - assert_equal('mate', StandupMD.config.cli.editor) + cli(["--editor", "mate", "--directory", workdir.to_s]) + assert_equal("mate", StandupMD.config.cli.editor) end def test_print enable_stdout_redirection - cli(['--directory', workdir.to_s]) + cli(["--directory", workdir.to_s]) refute(StandupMD.config.cli.print) - cli(['--print', '--directory', workdir.to_s]) + cli(["--print", "--directory", workdir.to_s]) assert(StandupMD.config.cli.print) assert_equal(Date.today, StandupMD.config.cli.date) cli( [ - '--print', - '--directory', workdir.to_s, - '--print', Date.today.prev_day.strftime(StandupMD.config.file.header_date_format).to_s + "--print", + "--directory", workdir.to_s, + "--print", Date.today.prev_day.strftime(StandupMD.config.file.header_date_format).to_s ] ) assert(StandupMD.config.cli.print) diff --git a/test/standup_md_test.rb b/test/standup_md_test.rb index d0dab84..cad53a9 100644 --- a/test/standup_md_test.rb +++ b/test/standup_md_test.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true -require_relative 'test_helper' -require_relative '../lib/standup_md' +require_relative "test_helper" +require_relative "../lib/standup_md" class TestStandupMD < TestHelper def setup - StandupMD.instance_variable_set('@config_file_loaded', false) + StandupMD.instance_variable_set(:@config_file_loaded, false) end def test_config @@ -39,14 +39,14 @@ def test_version def test_configure assert_nothing_raised do StandupMD.configure do |s| - s.file.directory = workdir - s.cli.editor = 'mate' - s.entry.impediments = ['Nothing'] + s.file.directory = workdir + s.cli.editor = "mate" + s.entry.impediments = ["Nothing"] end end assert_equal(workdir, StandupMD.config.file.directory) - assert_equal('mate', StandupMD.config.cli.editor) - assert_equal(['Nothing'], StandupMD.config.entry.impediments) + assert_equal("mate", StandupMD.config.cli.editor) + assert_equal(["Nothing"], StandupMD.config.entry.impediments) end def test_load_config_file @@ -54,8 +54,8 @@ def test_load_config_file refute(StandupMD.config_file_loaded?) assert_nothing_raised { StandupMD.load_config_file(test_config_file_name) } assert(StandupMD.config_file_loaded?) - assert_equal(['NONE'], StandupMD.config.entry.impediments) - assert_equal('Current', StandupMD.config.file.current_header) - assert_equal('mate', StandupMD.config.cli.editor) + assert_equal(["NONE"], StandupMD.config.entry.impediments) + assert_equal("Current", StandupMD.config.file.current_header) + assert_equal("mate", StandupMD.config.cli.editor) end end