-
Notifications
You must be signed in to change notification settings - Fork 33
/
ib
executable file
·63 lines (51 loc) · 1.84 KB
/
ib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env ruby
require "rubygems"
require "thor"
require "thor/group"
require 'thor/rake_compat'
module IB
class ControllerGenerator < Thor
include Thor::Actions
include Thor::RakeCompat
map "c" => :controller
def self.source_root
File.dirname(__FILE__)
end
desc "controller", "generate controller"
argument :name, :type => :string, :desc => "Name of the controller"
argument :controller_type, :type => :string, :desc => "Type of the controller, default: UIViewController", :default => "UIViewController"
class_option :outlets, :type => :hash, :default => {}, :required => false, :desc => "IBOutlets of the controller"
class_option :actions, :type => :array, :default => [], :required => false, :desc => "IBAtions of the controller"
class_option :accessors, :type => :array, :default => [], :required => false, :desc => "Accessors of the controller"
def controller
create_controller_file
create_helper_file
create_spec_file
end
private
def create_controller_file
template('../template/controller.erb', "app/controllers/#{underscore(name)}_controller.rb")
end
def create_helper_file
template('../template/controller_helper.erb', "app/helpers/#{underscore(name)}_helper.rb")
end
def create_spec_file
template('../template/controller_spec.erb', "spec/controllers/#{underscore(name)}_controller_spec.rb")
end
def controller_name
camelize("#{name}Controller")
end
def underscore(string)
string.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
def camelize(string)
return string if string !~ /_/ && string =~ /[A-Z]+.*/
string.split('_').map{|e| e.capitalize }.join
end
end
end
IB::ControllerGenerator.start