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.command <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, GitCommand, Helper
Constant Summary
collapse
- BasePath =
File.expand_path(File.dirname(__FILE__) + '/..')
Instance Method Summary
collapse
Instance Method Details
#activate(args) ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'lib/github.rb', line 51
def activate(args)
@@original_args = args.clone
@options = parse_options(args)
@debug = @options[:debug]
load 'helpers.rb'
load 'commands.rb'
invoke(args.shift, *args)
end
|
#command(command, &block) ⇒ Object
28
29
30
31
32
33
34
35
|
# File 'lib/github.rb', line 28
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
|
71
72
73
|
# File 'lib/github.rb', line 71
def commands
@commands ||= {}
end
|
#debug(*messages) ⇒ Object
117
118
119
|
# File 'lib/github.rb', line 117
def debug(*messages)
puts *messages.map { |m| "== #{m}" } if debug?
end
|
#debug? ⇒ Boolean
121
122
123
|
# File 'lib/github.rb', line 121
def debug?
!!@debug
end
|
#desc(str) ⇒ Object
37
38
39
|
# File 'lib/github.rb', line 37
def desc(str)
@next_description = str
end
|
#descriptions ⇒ Object
75
76
77
|
# File 'lib/github.rb', line 75
def descriptions
@descriptions ||= {}
end
|
#find_command(name) ⇒ Object
66
67
68
69
|
# File 'lib/github.rb', line 66
def find_command(name)
name = name.to_s
commands[name] || GitCommand.new(name) || commands['default']
end
|
#flag_descriptions ⇒ Object
79
80
81
|
# File 'lib/github.rb', line 79
def flag_descriptions
@flagdescs ||= Hash.new { |h, k| h[k] = {} }
end
|
#flags(hash) ⇒ Object
41
42
43
44
|
# File 'lib/github.rb', line 41
def flags(hash)
@next_flags ||= {}
@next_flags.update hash
end
|
#helper(command, &block) ⇒ Object
46
47
48
49
|
# File 'lib/github.rb', line 46
def helper(command, &block)
debug "Helper'd `#{command}`"
Helper.send :define_method, command, &block
end
|
#invoke(command, *args) ⇒ Object
60
61
62
63
64
|
# File 'lib/github.rb', line 60
def invoke(command, *args)
block = find_command(command)
debug "Invoking `#{command}`"
block.call(*args)
end
|
#load(file) ⇒ Object
111
112
113
114
115
|
# File 'lib/github.rb', line 111
def load(file)
file[0] == ?/ ? path = file : path = BasePath + "/commands/#{file}"
data = File.read(path)
GitHub.module_eval data, path
end
|
83
84
85
|
# File 'lib/github.rb', line 83
def options
@options
end
|
#original_args ⇒ Object
87
88
89
|
# File 'lib/github.rb', line 87
def original_args
@@original_args ||= []
end
|
#parse_options(args) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/github.rb', line 91
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
|