Module: GitHub

Extended by:
GitHub
Included in:
GitHub
Defined in:
lib/github.rb,
lib/github/helper.rb,
lib/github/command.rb

Overview

Starting simple.

$ github <command> <args>

GitHub.register <command> do |*args|
  whatever 
end

We’ll probably want to use the ‘choice` gem for concise, tasty DSL arg parsing action.

Defined Under Namespace

Classes: Command, Helper

Constant Summary collapse

BasePath =
File.expand_path(File.dirname(__FILE__) + '/..')

Instance Method Summary collapse

Instance Method Details

#activate(args) ⇒ Object



49
50
51
52
53
54
# File 'lib/github.rb', line 49

def activate(args)
  @options = parse_options(args)
  load 'helpers.rb'
  load 'commands.rb'
  invoke(args.shift, *args)
end

#command(command, &block) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/github.rb', line 26

def command(command, &block)
  debug "Registered `#{command}`"
  descriptions[command] = @next_description if @next_description
  @next_description = nil
  flag_descriptions[command].update @next_flags if @next_flags
  @next_flags = nil
  commands[command.to_s] = Command.new(block)
end

#commandsObject



62
63
64
# File 'lib/github.rb', line 62

def commands
  @commands ||= {}
end

#debug(*messages) ⇒ Object



104
105
106
# File 'lib/github.rb', line 104

def debug(*messages)
  puts *messages.map { |m| "== #{m}" } if debug?
end

#debug?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/github.rb', line 108

def debug?
  !!@debug
end

#desc(str) ⇒ Object



35
36
37
# File 'lib/github.rb', line 35

def desc(str)
  @next_description = str
end

#descriptionsObject



66
67
68
# File 'lib/github.rb', line 66

def descriptions
  @descriptions ||= {}
end

#flag_descriptionsObject



70
71
72
# File 'lib/github.rb', line 70

def flag_descriptions
  @flagdescs ||= Hash.new { |h, k| h[k] = {} }
end

#flags(hash) ⇒ Object



39
40
41
42
# File 'lib/github.rb', line 39

def flags(hash)
  @next_flags ||= {}
  @next_flags.update hash
end

#helper(command, &block) ⇒ Object



44
45
46
47
# File 'lib/github.rb', line 44

def helper(command, &block)
  debug "Helper'd `#{command}`"
  Helper.send :define_method, command, &block
end

#invoke(command, *args) ⇒ Object



56
57
58
59
60
# File 'lib/github.rb', line 56

def invoke(command, *args)
  block = commands[command.to_s] || commands['default']
  debug "Invoking `#{command}`"
  block.call(*args)
end

#load(file) ⇒ Object



98
99
100
101
102
# File 'lib/github.rb', line 98

def load(file)
  file[0] == ?/ ? path = file : path = BasePath + "/commands/#{file}"
  data = File.read(path)
  GitHub.module_eval data, path
end

#optionsObject



74
75
76
# File 'lib/github.rb', line 74

def options
  @options
end

#parse_options(args) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/github.rb', line 78

def parse_options(args)
  idx = 0
  args.clone.inject({}) do |memo, arg|
    case arg
    when /^--(.+?)=(.*)/
      args.delete_at(idx)
      memo.merge($1.to_sym => $2)
    when /^--(.+)/
      args.delete_at(idx)
      memo.merge($1.to_sym => true)
    when "--"
      args.delete_at(idx)
      return memo
    else
      idx += 1
      memo
    end
  end
end