Class: DailyLog::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/daily_log/options.rb

Overview

Parses options passed into the command-line and returns the desired values

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



19
20
21
22
# File 'lib/daily_log/options.rb', line 19

def initialize
  @hash = defaults
  parse
end

Instance Attribute Details

#hashObject (readonly) Also known as: to_hash

The options as a Hash

Returns Hash



15
16
17
# File 'lib/daily_log/options.rb', line 15

def hash
  @hash
end

Instance Method Details

#defaultsObject

The default options

Returns Hash



27
28
29
# File 'lib/daily_log/options.rb', line 27

def defaults
  { date: Date.today }
end

#parseObject

Parse the input options and update the #hash with desired values.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/daily_log/options.rb', line 32

def parse
  OptionParser.new do |opts|
    opts.banner = "Usage: dl [options]"
    opts.on("-e", "--edit", "Open the entry in the editor",
                            "(Even when it's not today's date)") do
      hash[:edit] = true
    end
    opts.on("", "--path", "Show the path for the given entry file") do
      hash[:path] = true
    end
    opts.on("-p", "--print", "Print the entry to STDOUT",
                            "(Even when it's today's date)") do
      hash[:print] = true
    end
    opts.on("-d", "--date=DATE", Date, "Date to show string for") do |date|
      hash[:date] = date
    end
    opts.on("-l", "--last", "Show the last daily log entry") do |dh|
      last = DailyLog::LatestDate.new.find
      if last
        hash[:date] = last
      else
        puts "There are no previous entries"
      end
    end
  end.parse!
  hash[:print] = (hash[:date] != Date.today) unless hash.key?(:print)
  hash[:edit]  = (hash[:date] == Date.today) unless hash.key?(:edit)
end