motion-util 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,4 +3,6 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  .DS_Store
6
- *.xcworkspace
6
+ *.xcworkspace
7
+ .rvmrc
8
+ tmp
data/README.md CHANGED
@@ -6,23 +6,32 @@ Convenient command set for Ruby Motion.
6
6
  Installation
7
7
  ===
8
8
 
9
- >gem install motion-util
9
+ gem install motion-util
10
10
 
11
11
 
12
12
  Usage
13
13
  ===
14
14
 
15
- >motion-util generate model Foo
16
- >motion-util generate controller Foo
17
- >motion-util generate view Foo
18
- >motion-util generate view_controller Foo
19
- >motion-util generate table_view_controller Foo
20
- >motion-util generate navigation_controller Foo
21
- >motion-util generate tab_bar_controller Foo
22
-
23
- You can also describe like this:
24
- >motion-util g model Foo
25
- >motion-util generation model Foo
15
+ Generate class file.
16
+
17
+ motion-util generate model Foo
18
+ motion-util generate controller Foo
19
+ motion-util generate view Foo
20
+ motion-util generate view_controller Foo
21
+ motion-util generate table_view_controller Foo
22
+ motion-util generate navigation_controller Foo
23
+ motion-util generate tab_bar_controller Foo
24
+
25
+ You can also describe like this:
26
+ motion-util g model Foo
27
+ motion-util generation model Foo
28
+
29
+ Generate Objective-C header file for Interface Builder.
30
+
31
+ motion-util ib_header
32
+
33
+ You can also describe like this:
34
+ motion-util ibh
26
35
 
27
36
  Licence
28
37
  ===
data/lib/motion-util.rb CHANGED
@@ -5,3 +5,4 @@ require "fileutils"
5
5
  require "motion-util/version"
6
6
  require "motion-util/util"
7
7
  require "motion-util/command/generator"
8
+ require "motion-util/command/ib_header"
@@ -0,0 +1,95 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Motion
3
+ module Util
4
+
5
+ class IbHeaderGenerator
6
+ def run
7
+ project_dir = Dir.pwd
8
+ dst_dir = File.join project_dir, "tmp", "header"
9
+ FileUtils.mkdir_p dst_dir
10
+ Dir.glob("app/**/*.rb") do |f|
11
+ ib = IbHeader.new
12
+ ib.source = File.read(f)
13
+ dst_name = ib.dst_name_of f
14
+ dst = File.join(dst_dir, dst_name)
15
+ FileUtils.mkdir_p File.dirname(dst)
16
+ File.open(dst, "w") do |f|
17
+ f.write(ib.context)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ class IbHeader
24
+
25
+ attr_reader :class_name, :super_name, :properties
26
+
27
+ def source= source
28
+ @source = source
29
+ parse
30
+ end
31
+
32
+ def context
33
+ lines = []
34
+ lines << "@interface #{@class_name} : #{@super_name || 'NSObject'}"
35
+ properties.each do |k, v|
36
+ if v[:readonly]
37
+ lines << "@property (strong, nonatomic, readonly) IBOutlet #{type_name_of v[:type]}#{k};"
38
+ else
39
+ lines << "@property (strong, nonatomic) IBOutlet #{type_name_of v[:type]}#{k};"
40
+ end
41
+ end
42
+ lines << "@end"
43
+ lines << ""
44
+ lines.join "\n"
45
+ end
46
+
47
+ def dst_name_of path
48
+ a = path.split("/")
49
+ a.pop
50
+ a << "#{class_name}.h"
51
+ a.join("/")
52
+ end
53
+
54
+ private
55
+
56
+ def parse
57
+ /class\s+(\w+)\s*(<\s*(\w+))?/ =~ @source
58
+ @class_name = $1
59
+ @super_name = $3
60
+
61
+ @properties = {}
62
+ @source.split("\n").each do |l|
63
+ /(attr_accessor|attr_reader)\s+([A-Za-z0-9:, ]+)(#\s*(@type_info\s+(\w+)?)?)?/ =~ l
64
+ type = type_of $5
65
+ $2.split(",").each do |p|
66
+ p = eval(p.strip)
67
+ @properties[p] = { type:type }
68
+ @properties[p].merge!(readonly:true) if $1 == "attr_reader"
69
+ end if $2
70
+ end
71
+ end
72
+
73
+ def type_of t
74
+ case t
75
+ when nil, "id"
76
+ "id"
77
+ when "String"
78
+ "NSString *"
79
+ when "Hash"
80
+ "NSDictionary *"
81
+ else
82
+ "#{t} *"
83
+ end
84
+ end
85
+
86
+ def type_name_of type
87
+ type == "id" ? "id " : type
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+ end
94
+
95
+
@@ -14,9 +14,13 @@ module Motion
14
14
  def usage
15
15
  <<-EOF
16
16
  Usage:
17
- motion-util: command [options]
17
+ motion-util: command [subcommand] [options]
18
18
 
19
- Usage:
19
+ command:
20
+ generate Generate class and spec files.
21
+ You can use g or generator instead of generate.
22
+ ib_header Generate Objective-C header files for Interface Builder.
23
+ You can use ibh instead of ib_header.
20
24
 
21
25
  EOF
22
26
  end
@@ -30,6 +34,8 @@ Usage:
30
34
  case ARGV[0]
31
35
  when "generate", "generator", "g"
32
36
  cmd = Generator.new
37
+ when "ib_header", "ibh"
38
+ cmd = IbHeaderGenerator.new
33
39
  end
34
40
  if cmd
35
41
  cmd.run
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  module Motion
3
3
  module Util
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
6
6
  end
@@ -142,7 +142,7 @@ end
142
142
  describe Foo do
143
143
 
144
144
  it "should be successful" do
145
- true.should be_true
145
+ true.should == true
146
146
  end
147
147
 
148
148
  end
@@ -0,0 +1,79 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'test-unit'
3
+ require 'motion-util'
4
+
5
+ include Motion::Util
6
+
7
+ class TestIbHeader < Test::Unit::TestCase
8
+
9
+ def setup
10
+ ARGV.replace %w(ib_header)
11
+ @ib = IbHeader.new
12
+ end
13
+
14
+
15
+ # --- parse class/super
16
+ test "class_name should be 'Foo' with 'class Foo'" do
17
+ @ib.source = "class Foo"
18
+ assert_equal ["Foo", nil], [@ib.class_name, @ib.super_name]
19
+ end
20
+
21
+ test "names should be ['Foo', 'Bar'] with 'class Foo < Bar'" do
22
+ @ib.source = "class Foo < Bar"
23
+ assert_equal %w(Foo Bar), [@ib.class_name, @ib.super_name]
24
+ end
25
+
26
+ test "names should be ['Foo', 'Bar'] with 'class Foo < Bar '" do
27
+ @ib.source = "class Foo < Bar"
28
+ assert_equal %w(Foo Bar), [@ib.class_name, @ib.super_name]
29
+ end
30
+
31
+ test "names should be ['Foo', 'Bar'] with 'class Foo<Bar'" do
32
+ @ib.source = "class Foo < Bar"
33
+ assert_equal %w(Foo Bar), [@ib.class_name, @ib.super_name]
34
+ end
35
+
36
+
37
+ # --- parse properties
38
+ test "parse properties" do
39
+ @ib.source = "attr_accessor :foo, :bar"
40
+ assert_equal( { foo: { type: "id" }, bar: { type: "id"} }, @ib.properties )
41
+ end
42
+
43
+ test "parse readonly properties" do
44
+ @ib.source = "attr_reader :foo, :bar"
45
+ assert_equal( { foo: { type: "id", readonly: true }, bar: { type: "id", readonly: true} }, @ib.properties )
46
+ end
47
+
48
+ test "parse property with tyep_info string" do
49
+ @ib.source = "attr_accessor :foo, :bar # @type_info String"
50
+ assert_equal( { foo: { type: "NSString *" }, bar: { type: "NSString *"} }, @ib.properties )
51
+ end
52
+
53
+ test "context" do
54
+ @ib.source = <<-EOF
55
+ class Foo
56
+ attr_accessor :foo # no type
57
+ attr_accessor :bar # @type_info UILabel
58
+ attr_reader :hoge # @type_info UIView
59
+ end
60
+ EOF
61
+
62
+ expected = <<-EOF
63
+ @interface Foo : NSObject
64
+ @property (strong, nonatomic) IBOutlet id foo;
65
+ @property (strong, nonatomic) IBOutlet UILabel *bar;
66
+ @property (strong, nonatomic, readonly) IBOutlet UIView *hoge;
67
+ @end
68
+ EOF
69
+
70
+ assert_equal expected, @ib.context
71
+ end
72
+
73
+ test "dst_name_of" do
74
+ @ib.source = "class AppDelegate"
75
+ assert_equal "app/AppDelegate.h", @ib.dst_name_of("app/app_delegate.rb")
76
+ end
77
+
78
+
79
+ end
@@ -0,0 +1,31 @@
1
+ class TestIbHeaderGenerator < Test::Unit::TestCase
2
+
3
+ def setup
4
+ @original_dir = File.expand_path Dir.pwd
5
+ dst_dir = File.join @original_dir, "tmp"
6
+ FileUtils.mkdir_p dst_dir
7
+ Dir.chdir dst_dir
8
+ `motion create sample`
9
+ Dir.chdir "sample"
10
+ IbHeaderGenerator.new.run
11
+ end
12
+
13
+ def teardown
14
+ Dir.chdir @original_dir
15
+ FileUtils.rm_rf File.join "tmp", "sample"
16
+ end
17
+
18
+ test "AppDelegate.h should be generated" do
19
+ assert File.exist?("tmp/header/app/AppDelegate.h")
20
+ end
21
+
22
+ test "AppDelegate.h's context should be" do
23
+ expected = <<-EOF
24
+ @interface AppDelegate : NSObject
25
+ @end
26
+ EOF
27
+
28
+ assert_equal expected, File.read("tmp/header/app/AppDelegate.h")
29
+ end
30
+
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-19 00:00:00.000000000 Z
12
+ date: 2012-06-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: It generate class and spec files.
15
15
  email:
@@ -27,6 +27,7 @@ files:
27
27
  - bin/motion-util
28
28
  - lib/motion-util.rb
29
29
  - lib/motion-util/command/generator.rb
30
+ - lib/motion-util/command/ib_header.rb
30
31
  - lib/motion-util/util.rb
31
32
  - lib/motion-util/version.rb
32
33
  - motion-util.gemspec
@@ -40,6 +41,8 @@ files:
40
41
  - template/class/view_controller.rb
41
42
  - template/spec/spec.rb
42
43
  - test/test_generator.rb
44
+ - test/test_ib_header.rb
45
+ - test/test_ib_header_generator.rb
43
46
  homepage: https://github.com/katsuyoshi/motion-util
44
47
  licenses: []
45
48
  post_install_message:
@@ -66,3 +69,5 @@ specification_version: 3
66
69
  summary: Convenient command set for Ruby Motion.
67
70
  test_files:
68
71
  - test/test_generator.rb
72
+ - test/test_ib_header.rb
73
+ - test/test_ib_header_generator.rb