Class: StandupMD::Cli

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/standup_md/cli.rb,
lib/standup_md/cli/helpers.rb

Overview

Class for handing the command-line interface.

Defined Under Namespace

Modules: Helpers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#print

Constructor Details

#initialize(options = [], load_config: true) ⇒ Cli

Constructor. Sets defaults.

Parameters:

  • options (Array) (defaults to: [])


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/standup_md/cli.rb', line 71

def initialize(options = [], load_config: true)
  @config = self.class.config
  @preference_file_loaded = false
  @file_date_argument = false
  @options = options
  load_preferences if load_config
  load_runtime_preferences(options)
  @file = find_file
  @file&.load
  @entry = @file.nil? ? nil : new_entry(@file)
end

Instance Attribute Details

#entryStandupMD::Entry (readonly)

The entry searched for, usually today.

Returns:



45
46
47
# File 'lib/standup_md/cli.rb', line 45

def entry
  @entry
end

#fileStandupMD::File (readonly)

The file loaded.

Returns:



57
58
59
# File 'lib/standup_md/cli.rb', line 57

def file
  @file
end

#optionsArray (readonly)

Arguments passed at runtime.

Returns:

  • (Array)

    ARGV



51
52
53
# File 'lib/standup_md/cli.rb', line 51

def options
  @options
end

Class Method Details

.configStandupMD::Config::Cli

Access to the class’s configuration.



16
17
18
# File 'lib/standup_md/cli.rb', line 16

def self.config
  @config ||= StandupMD.config.cli
end

.echo(msg) ⇒ nil

Prints output if verbose is true.

Returns:

  • (nil)


24
25
26
# File 'lib/standup_md/cli.rb', line 24

def self.echo(msg)
  puts msg if config.verbose
end

.execute(options = []) ⇒ Object

Creates an instance of StandupMD and runs what the user requested.



30
31
32
33
34
35
36
37
38
39
# File 'lib/standup_md/cli.rb', line 30

def self.execute(options = [])
  new(options).tap do |exe|
    exe.write_file if exe.write?
    if config.print
      exe.print(exe.entry)
    elsif config.edit
      exe.edit
    end
  end
end

Instance Method Details

#echo(msg) ⇒ nil

Quick access to Cli.echo.

Returns:

  • (nil)


134
135
136
# File 'lib/standup_md/cli.rb', line 134

def echo(msg)
  self.class.echo(msg)
end

#editnil

Opens the file in an editor. Abandons the script.

Returns:

  • (nil)


108
109
110
111
# File 'lib/standup_md/cli.rb', line 108

def edit
  echo "Opening file in #{@config.editor}"
  exec("#{@config.editor} #{file.name}")
end

#file_date_argument?Boolean

Was a file date argument passed?

Returns:

  • (Boolean)


63
64
65
# File 'lib/standup_md/cli.rb', line 63

def file_date_argument?
  @file_date_argument
end

#load_preferencesnil

Load the preference file.

Returns:

  • (nil)


87
88
89
90
91
92
93
94
# File 'lib/standup_md/cli.rb', line 87

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
end

#preference_file_loaded?boolean

Has the preference file been loaded?

Returns:

  • (boolean)


100
101
102
# File 'lib/standup_md/cli.rb', line 100

def preference_file_loaded?
  @preference_file_loaded
end

#write?Boolean

Should the file be written?

Returns:

  • (Boolean)


126
127
128
# File 'lib/standup_md/cli.rb', line 126

def write?
  !!(@config.write && !read_only? && entry)
end

#write_fileBoolean

Writes entries to the file.

Returns:

  • (Boolean)

    true if file was written



117
118
119
120
# File 'lib/standup_md/cli.rb', line 117

def write_file
  echo "Writing file #{file.name}"
  file.write
end