Permalink
Browse files

Add controller generator

  • Loading branch information...
siuying committed Jul 13, 2012
1 parent 4c18c19 commit d80775b3d12c7ea7a1471944f6ed860b4e157ce4
Showing with 126 additions and 17 deletions.
  1. +26 −15 README.md
  2. +63 −0 bin/ib
  3. +3 −2 ib.gemspec
  4. +24 −0 template/controller.erb
  5. +2 −0 template/controller_helper.erb
  6. +8 −0 template/controller_spec.erb
View
@@ -19,9 +19,9 @@ Or install it yourself as:
In your Rake file:
```ruby
-# -*- coding: utf-8 -*-
-$:.unshift("/Library/RubyMotion/lib")
-require 'motion/project'
+
+$:.unshift("/Library/RubyMotion/lib")
+require 'motion/project'
# if you use bundler
require 'bundler'
@@ -39,26 +39,37 @@ end
## Usage
-Extend your controllers with IB module:
+Generate controller with folllowing command:
-```ruby
-class SuperController < UIViewController
+```
+ib c Hello UIViewController \
+ --outlets scroller:UIScrollView btn_hello:UIButton \
+ --actions say_hello \
+ --accessors data_source
+```
+The generated file:
+
+#### /app/controllers/hello_controller.rb
+```ruby
+class HelloController < UIViewController
extend IB
- # define attribute accessor
- attr_accessor title
+ ## define accessors
+ attr_accessor :data_source, :scroller, :htn_hello
- # define ib outlet
- ib_outlet :title, UILabel
+ ## define ib outlets
+ ib_outlet :scroller, UIScrollView
+ ib_outlet :btn_hello, UIButton
- # define action method
- def ontouch button
- title.text = "Touched!!!"
+ ## define actions
+ def say_hello(sender)
+ # TODO Implement action here
end
- # define ib action
- ib_action :ontouch
+ ## define ib action
+ ib_action :say_hello
+
end
```
View
63 bin/ib
@@ -0,0 +1,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
View
@@ -2,8 +2,8 @@
require File.expand_path('../lib/ib/version', __FILE__)
Gem::Specification.new do |gem|
- gem.authors = ["Yury Korolev"]
- gem.email = ["yury.korolev@gmail.com"]
+ gem.authors = ["Yury Korolev", "Francis Chong"]
+ gem.email = ["yury.korolev@gmail.com", "francis@ignition.hk"]
gem.description = %q{Stupid rubymotion ib outlets support}
gem.summary = %q{Small portion of love to interface builder with rubymotion}
gem.homepage = ""
@@ -16,6 +16,7 @@ Gem::Specification.new do |gem|
gem.version = IB::VERSION
gem.add_dependency "xcodeproj", ">= 0.2.3"
+ gem.add_dependency "thor", "~> 0.15.4"
gem.add_development_dependency "rspec", ">= 2.0"
end
View
@@ -0,0 +1,24 @@
+class <%= controller_name %> < <%= controller_type %>
+ extend IB
+
+ ## define accessors
+ attr_accessor <%= (options[:accessors] + options[:outlets].keys).collect{|acc| ":#{acc}"}.join(", ") %>
+
+ ## define ib outlets
+<% options[:outlets].each do |name, type| -%>
+ ib_outlet :<%= name %>, <%= type %>
+<% end -%>
+
+ ## define actions
+<% options[:actions].each do |action| -%>
+ def <%= action %>(sender)
+ # TODO Implement action here
+ end
+<% end -%>
+
+ ## define ib action
+<% options[:actions].each do |action| -%>
+ ib_action :<%= action %>
+<% end -%>
+
+end
@@ -0,0 +1,2 @@
+module <%= camelize("#{name}Helper") %>
+end
@@ -0,0 +1,8 @@
+describe "<%= controller_name %>" do
+ tests <%= controller_name %>
+
+<% options[:actions].each do |action| -%>
+ describe "#<%= action %>" do
+ end
+<% end -%>
+end

0 comments on commit d80775b

Please sign in to comment.