Class: Cmd::NewTask

Inherits:
Object
  • Object
show all
Defined in:
lib/cmd/new.rb

Instance Method Summary collapse

Constructor Details

#initialize(db, logger, stdout, stderr) ⇒ NewTask

Returns a new instance of NewTask.



21
22
23
24
25
26
27
# File 'lib/cmd/new.rb', line 21

def initialize(db, logger, stdout, stderr)
  @db = db
  @logger = logger
  @stdout = stdout
  @stderr = stderr
  @contract = NewTaskContract.new
end

Instance Method Details

#call(args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cmd/new.rb', line 29

def call(args)
  options = parse(args)
  errors = @contract.(options).errors(full: true).to_h
  unless errors.empty?
    table = Terminal::Table.new do |t|
      errors.each do |field, errors|
        row = []
        row.push(field)
        errors.each do |err|
          row.push(err)
        end
        t.add_row(row)
      end
    end
    @stderr.puts "Failed to create task"
    @stderr.puts table
    return
  end
  options[:done] = false
  @db[:todos].insert(options)
  @stdout.puts "Task added successfully"
rescue => e
  @stderr.puts e.message
  @stderr.puts parser
  exit(1)
end