Module: Commands::Add

Included in:
LogTime
Defined in:
lib/commands/add.rb

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/commands/add.rb', line 3

def self.included(thor)
    thor.class_eval do
		option :tag, :type => :string, :alias => '-t', default: ""
		option :estimate, :type => :string, :alias => '-e', default: false
		option :start, :type => :boolean, default: true
		desc "add [NAME]", "Create new log"
		def add(name)
			log = Log.new(name: name).tag(options[:tag])

			# estimate option
			if options[:estimate]
				estimation = ChronicDuration.parse(options[:estimate]) if !estimation
				if !estimation
					say "could not parse estimation time: " + options[:estimate], :red
					exit
				end
				log.estimation = estimation
			end

			# save log
			if !log.save
				log.errors.full_messages.each do |error|
					say error, :red
				end
				exit
			end

			# start option
			if options[:start] and log.start!
				say name.to_s + " added and started", :green
			else
				say name.to_s + " added", :green
			end
		end
	end
end