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



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

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

#commandsObject



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

def commands
  @commands ||= {}
end

#debug(*messages) ⇒ Object



81
82
83
# File 'lib/github.rb', line 81

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

#debug?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/github.rb', line 85

def debug?
  !!@debug
end

#describe(hash) ⇒ Object



28
29
30
# File 'lib/github.rb', line 28

def describe(hash)
  descriptions.update hash
end

#descriptionsObject



54
55
56
# File 'lib/github.rb', line 54

def descriptions
  @descriptions ||= {}
end

#helper(command, &block) ⇒ Object



32
33
34
35
# File 'lib/github.rb', line 32

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

#invoke(command, *args) ⇒ Object



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

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

#load(file) ⇒ Object



77
78
79
# File 'lib/github.rb', line 77

def load(file)
  file[0] == ?/ ? super : super(BasePath + "/commands/#{file}")
end

#optionsObject



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

def options
  @options
end

#parse_options(args) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/github.rb', line 62

def parse_options(args)
  @debug = args.delete('--debug')
  args.inject({}) do |memo, arg|
    if arg =~ /^--([^=]+)=(.+)/
      args.delete(arg)
      memo.merge($1.to_sym => $2)
    elsif arg =~ /^--(.+)/
      args.delete(arg)
      memo.merge($1.to_sym => true)
    else
      memo
    end
  end
end

#register(command, &block) ⇒ Object



23
24
25
26
# File 'lib/github.rb', line 23

def register(command, &block)
  debug "Registered `#{command}`"
  commands[command.to_s] = Command.new(block)
end