Class: Backlog::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/backlog/cli.rb

Constant Summary collapse

@@commands =
{
  init:  Init, 
  help: Help,
  open: Open,
  archive: Archive,
  save: Save,
  push: Push
}
@@aliases =
{
  
  :init => :in,
  :open => [:o, :edit],
  :help => :h,
  :archive => :a,
  :save => :s,
  :push => :p,
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel) ⇒ CLI

Returns a new instance of CLI.



27
28
29
30
# File 'lib/backlog/cli.rb', line 27

def initialize(argv, stdin=STDIN, stdout=STDOUT, stderr=STDERR, kernel=Kernel)

  @argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel
end

Class Method Details

.commandsObject



23
24
25
# File 'lib/backlog/cli.rb', line 23

def self.commands
  return @@commands
end

Instance Method Details

#execute!Object



32
33
34
35
36
37
38
39
40
# File 'lib/backlog/cli.rb', line 32

def execute!

  # get the method to call for this command
  command_class, argv = subcommand

  # run the command
  command_class.new(argv, @datefile).execute!

end

#subcommandObject

return a function pointer for the subcommand



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/backlog/cli.rb', line 43

def subcommand()

  if not @argv.length > 0
    @datefile = DateFile.new
    return Open, nil
  end
  # cache the first string a symbol
  keyword = @argv[0].to_sym
  
  # loop through all commands and see if we have a match
  @@commands.each do |key, command_class|

    # grab a list of the aliases
    aliases = nil
    # check to see if aliases key exists
    if @@aliases.has_key?(key)

      # grab the key and then fill the aliases hash properly
      value = @@aliases[key] 
      if value.kind_of?(Symbol)
        aliases = [value]
      else
        aliases = value
      end
    end
    
    # if alias / keywords match up then go ahead and return the correct class
    if keyword == key or aliases.include? keyword
      # return the correct command method
      @argv = @argv[1,@argv.length]
      @datefile = DateFile.new_from_argv(@argv)
      return command_class, @argv 
    end
  end

  # no command matches up - check to see if this is a date
  # we need to pass the date on to the next step ...
  date = DateFile.new_from_argv(@argv)

  if date != nil 
    @datefile = date
    return Open, date.argv
  else
    @datefile = DateFile.new_from_argv(@argv)
    return Open, nil
  end
end