Class: Timelog::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/timelog/command.rb,
lib/timelog/command/help.rb,
lib/timelog/command/list.rb,
lib/timelog/command/stop.rb,
lib/timelog/command/pause.rb,
lib/timelog/command/start.rb,
lib/timelog/command/active.rb,
lib/timelog/command/resume.rb,
lib/timelog/command/archive.rb,
lib/timelog/command/client_add.rb,
lib/timelog/command/project_add.rb

Direct Known Subclasses

Active, Archive, ClientAdd, Help, List, Pause, ProjectAdd, Resume, Start, Stop

Defined Under Namespace

Classes: Active, Archive, ClientAdd, Help, List, Pause, ProjectAdd, Resume, Start, Stop

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book, arguments = []) ⇒ Command

Returns a new instance of Command.



6
7
8
9
10
# File 'lib/timelog/command.rb', line 6

def initialize(book, arguments = [])
  @book = book
  @session = session
  @arguments = arguments
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



3
4
5
# File 'lib/timelog/command.rb', line 3

def arguments
  @arguments
end

#bookObject (readonly)

Returns the value of attribute book.



3
4
5
# File 'lib/timelog/command.rb', line 3

def book
  @book
end

#sessionObject (readonly)

Returns the value of attribute session.



3
4
5
# File 'lib/timelog/command.rb', line 3

def session
  @session
end

Class Method Details

.get(name, book, arguments = []) ⇒ Object



46
47
48
49
# File 'lib/timelog/command.rb', line 46

def get(name, book, arguments = [])
  klass = @commands[name]
  klass.new(book, arguments) if klass
end

.inherited(subclass) ⇒ Object



38
39
40
# File 'lib/timelog/command.rb', line 38

def inherited(subclass)
  register subclass.name.split("::").last.underscore.gsub("_"," "), subclass
end

.knownObject



51
52
53
# File 'lib/timelog/command.rb', line 51

def known
  @commands.keys
end

.register(name, klass) ⇒ Object



42
43
44
# File 'lib/timelog/command.rb', line 42

def register(name, klass)
  @commands[name] = klass
end

Instance Method Details

#executeObject



12
13
14
# File 'lib/timelog/command.rb', line 12

def execute
  help
end

#helpObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/timelog/command.rb', line 16

def help
  <<-HELP
usage: timelog <command> [arguments]

Commands:
  #{Command.known.join("\n  ")}

See 'timelog <command> help' for more information on a specific command.
  HELP
end

#time_in_arguments(default = Time.now) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/timelog/command.rb', line 27

def time_in_arguments(default = Time.now)
  if time = @arguments.find {|arg| arg.match /\A([01]?[0-9]|2[0-3])([\:\.]([0-5][0-9])|)\Z/}
    hour = time.match(/^([01]?[0-9]|2[0-3])/)[0].to_i
    minute = time.match(/([\:\.]([0-5][0-9]))$/).try(:values_at, 2).try(:first).try(:to_i) || 0
    @arguments.delete_if {|arg| arg =~ TIME_REGEX}
    Time.now.change({hour: hour, min: minute, sec: 0})
  end
  default
end