simple-view 0.5.1 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,7 +2,37 @@
2
2
 
3
3
  RubyMotion DSL for UIKit.
4
4
 
5
- Demo app: [Currency](https://github.com/seanho/CurrencyApp-RubyMotion)
5
+ ## Change log
6
+
7
+ __Version 0.6__
8
+
9
+ Breaking changes:
10
+
11
+ Block scoping is removed, i.e. no need to pass in locals.
12
+
13
+ Previous
14
+ ````ruby
15
+ class ViewController
16
+ def viewDidLoad
17
+ setup view, controller: self do
18
+ table_view delegate: controller, dataSource: controller
19
+ end
20
+ end
21
+ end
22
+ ````
23
+
24
+ Now
25
+ ````ruby
26
+ class ViewController
27
+ def viewDidLoad
28
+ setup view do
29
+ table_view delegate: self, dataSource: self
30
+ end
31
+ end
32
+ end
33
+ ````
34
+
35
+ `add` is renamed to `add_view` for clarity.
6
36
 
7
37
  ## Installation
8
38
 
@@ -61,7 +91,7 @@ SimpleView provides shorthand methods for most UIKit classes
61
91
  def viewDidLoad
62
92
  setup view do
63
93
  label text: 'Hi there!' # shorthand
64
- add UILabel, text: 'Hi there!' # same as above
94
+ add_view UILabel, text: 'Hi there!' # same as above
65
95
  end
66
96
  end
67
97
  ````
@@ -96,7 +126,7 @@ SimpleView can work with any custom views and controls
96
126
 
97
127
  ````ruby
98
128
  setup view do
99
- add CustomViewClass, name: "custom_view"...
129
+ add_view CustomViewClass, name: "custom_view"...
100
130
  end
101
131
  ````
102
132
 
@@ -168,22 +198,6 @@ setup view do
168
198
  end
169
199
  ````
170
200
 
171
- ### Passing in locals
172
-
173
- Reference to the controller/model or other variables in the setup block
174
-
175
- ````ruby
176
- class ViewController
177
- include SimpleView::Layout
178
-
179
- def viewDidLoad
180
- setup view, controller: self do
181
- table_view delegate: controller, dataSource: controller
182
- end
183
- end
184
- end
185
- ````
186
-
187
201
  ### View tagging with string
188
202
 
189
203
  Tag view with name string and find them with ease
@@ -6,16 +6,16 @@ class SimpleViewController < UIViewController
6
6
  def viewDidLoad
7
7
  super
8
8
 
9
+ self.title = 'Demo'
10
+
9
11
  @demos = [
10
12
  {caption: 'View Anchoring', controller: ViewAnchoringController},
11
13
  {caption: 'User Info', controller: UserInfoController}
12
14
  ]
13
15
 
14
- setup view, controller: self do
15
- controller.title = 'Demo'
16
-
17
- table_view delegate: controller, dataSource: controller, width: 100.percent, height: 100.percent do
18
- view.registerClass UITableViewCell, forCellReuseIdentifier: DEFAULT_CELL_ID
16
+ setup view do
17
+ table_view delegate: self, dataSource: self, width: 100.percent, height: 100.percent do |table|
18
+ table.registerClass UITableViewCell, forCellReuseIdentifier: DEFAULT_CELL_ID
19
19
  end
20
20
  end
21
21
  end
@@ -6,20 +6,20 @@ class UserInfoController < UIViewController
6
6
  def viewDidLoad
7
7
  super
8
8
 
9
+ self.title = 'User Info'
10
+
9
11
  @data = [
10
12
  [{title: 'mobile', text: '123456789'}],
11
13
  [{title: 'email', text: 'nyan.cat@meme.com'}],
12
14
  [{title: 'Facebook', text: 'Nyan Cat'}]
13
15
  ]
14
16
 
15
- setup view, controller: self do
16
- controller.title = 'User Info'
17
-
17
+ setup view do
18
18
  table_view style: UITableViewStyleGrouped,
19
- delegate: controller, dataSource: controller,
20
- width: 100.percent, height: 100.percent do |table_view|
19
+ delegate: self, dataSource: self,
20
+ width: 100.percent, height: 100.percent do |table|
21
21
 
22
- table_view.tableHeaderView = UserInfoHeader.alloc.initWithFrame [[0, 0], [view.width, 80]]
22
+ table.tableHeaderView = UserInfoHeader.alloc.initWithFrame [[0, 0], [view.width, 80]]
23
23
  end
24
24
  end
25
25
  end
@@ -2,13 +2,13 @@ class ViewAnchoringController < UIViewController
2
2
  include SimpleView::Layout
3
3
 
4
4
  def viewDidLoad
5
+ self.title = "View Anchoring"
6
+
5
7
  SimpleView::Styles.define :square,
6
8
  width: 20,
7
9
  height: 20
8
10
 
9
- setup view, controller: self do
10
- controller.title = "View Anchoring"
11
-
11
+ setup view do
12
12
  rect name: 'fill', styles: :square, backgroundColor: "#000", top: 0, left: 0, bottom: 0, right: 0
13
13
 
14
14
  rect name: 'tl', styles: :square, backgroundColor: "#990000", top: 0, left: 0
@@ -1,8 +1,52 @@
1
1
  module SimpleView
2
2
  module Layout
3
- def setup view, locals = {}, &block
4
- proxy = ViewProxy.new view, locals
5
- proxy.instance_eval &block if block_given?
3
+ extend self
4
+
5
+ def setup view, &block
6
+ view_stack.push view
7
+ block.call if block_given?
8
+ view_stack.pop
9
+ end
10
+
11
+ def add_view klass, options = {}, &block
12
+ subview = create_view klass, options, &block
13
+
14
+ view_stack.last.addSubview(subview) unless view_stack.empty?
15
+
16
+ subview
17
+ end
18
+
19
+ def create_view klass, options = {}, &block
20
+ bounds = view_stack.empty? ? CGRectZero : view_stack.last.bounds
21
+ subview = ViewBuilder.view_for klass, bounds, options
22
+
23
+ view_stack.push subview
24
+ block.call(subview) if block_given?
25
+ view_stack.pop
26
+
27
+ subview
28
+ end
29
+
30
+ def view_stack
31
+ @view_stack ||= []
32
+ end
33
+
34
+ def self.included base
35
+ base.class_eval do
36
+ [::UIActionSheet, ::UIActivityIndicatorView, ::UIButton, ::UIDatePicker, ::UIImageView, ::UILabel,
37
+ ::UIPageControl, ::UIPickerView, ::UIProgressView, ::UIScrollView, ::UISearchBar, ::UISegmentedControl,
38
+ ::UISlider, ::UIStepper, ::UISwitch, ::UITabBar, ::UITableView, ::UITableViewCell, ::UITextField, ::UITextView,
39
+ ::UIToolbar, ::UIWebView].each do |klass|
40
+
41
+ shorthand = "#{klass}"[2..-1].underscore.to_sym
42
+
43
+ define_method(shorthand) do |*args, &block|
44
+ options = args[0] || {}
45
+ add_view klass, options, &block
46
+ end
47
+ end
48
+ def rect(options = {}, &block) add_view(::UIView, options, &block); end
49
+ end
6
50
  end
7
51
  end
8
52
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleView
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6"
3
3
  end
data/spec/layout_spec.rb CHANGED
@@ -1,9 +1,10 @@
1
+
1
2
  describe "SimpleView::Layout" do
2
- describe "#setup" do
3
- class DummyController
4
- include SimpleView::Layout
5
- end
3
+ class DummyController
4
+ include SimpleView::Layout
5
+ end
6
6
 
7
+ describe "#setup" do
7
8
  it "should execute the block within view's scope" do
8
9
  view = UIView.alloc.initWithFrame(CGRectZero)
9
10
  DummyController.new.setup view do
@@ -12,4 +13,157 @@ describe "SimpleView::Layout" do
12
13
  view.frame.should == CGRectMake(0, 0, 10, 10)
13
14
  end
14
15
  end
16
+
17
+ describe "#add" do
18
+ before do
19
+ @controller = DummyController.new
20
+ end
21
+
22
+ it "should add view with options" do
23
+ alpha = 0.5
24
+ backgroundColor = UIColor.redColor
25
+
26
+ view = @controller.add_view UIView, top: 0, left: 0, width: 10, height: 10, alpha: alpha, backgroundColor: backgroundColor
27
+ view.frame.should == CGRectMake(0, 0, 10, 10)
28
+ view.alpha.should == alpha
29
+ view.backgroundColor.should == backgroundColor
30
+ end
31
+
32
+ describe "#add with predefined styles" do
33
+ it "should add view with default style" do
34
+ SimpleView::Styles.define UIView, backgroundColor: UIColor.redColor
35
+
36
+ view = @controller.add_view UIView
37
+ view.backgroundColor.should == UIColor.redColor
38
+ end
39
+
40
+ it "should add view with custom style" do
41
+ SimpleView::Styles.define :blue, backgroundColor: UIColor.blueColor
42
+
43
+ view = @controller.add_view UIView, styles: :blue
44
+ view.backgroundColor.should == UIColor.blueColor
45
+ end
46
+
47
+ it "should add view with multiple custom styles" do
48
+ SimpleView::Styles.define :blue, backgroundColor: UIColor.blueColor
49
+ SimpleView::Styles.define :alpha, alpha: 0.5
50
+
51
+ view = @controller.add_view UIView, styles: [:blue, :alpha]
52
+ view.backgroundColor.should == UIColor.blueColor
53
+ view.alpha.should == 0.5
54
+ end
55
+
56
+ it "should add view with custom style overriding default style" do
57
+ SimpleView::Styles.define UIView, backgroundColor: UIColor.redColor
58
+ SimpleView::Styles.define :blue, backgroundColor: UIColor.blueColor
59
+
60
+ view = @controller.add_view UIView, styles: :blue
61
+ view.backgroundColor.should == UIColor.blueColor
62
+ end
63
+ end
64
+
65
+ it "should add view to superview" do
66
+ super_view = UIView.alloc.init
67
+
68
+ @controller.setup super_view do
69
+ @controller.add_view UIView
70
+ end
71
+
72
+ super_view.subviews.first.class.should == UIView
73
+ end
74
+ end
75
+
76
+ describe "shorthand methods" do
77
+ before do
78
+ @controller = DummyController.new
79
+ end
80
+
81
+ it "should create UIActionSheet" do
82
+ @controller.action_sheet.class.should == UIActionSheet
83
+ end
84
+
85
+ it "should create UIActivityIndicatorView" do
86
+ @controller.activity_indicator_view.class.should == UIActivityIndicatorView
87
+ end
88
+
89
+ it "should create UIButton" do
90
+ @controller.button.class.should == UIRoundedRectButton
91
+ end
92
+
93
+ it "should create UIDatePicker" do
94
+ @controller.date_picker.class.should == UIDatePicker
95
+ end
96
+
97
+ it "should create UIImageView" do
98
+ @controller.image_view.class.should == UIImageView
99
+ end
100
+
101
+ it "should create UILabel" do
102
+ @controller.label.class.should == UILabel
103
+ end
104
+
105
+ it "should create UIPageControl" do
106
+ @controller.page_control.class.should == UIPageControl
107
+ end
108
+
109
+ it "should create UIPickerView" do
110
+ @controller.picker_view.class.should == UIPickerView
111
+ end
112
+
113
+ it "should create UIProgressView" do
114
+ @controller.progress_view.class.should == UIProgressView
115
+ end
116
+
117
+ it "should create UIScrollView" do
118
+ @controller.scroll_view.class.should == UIScrollView
119
+ end
120
+
121
+ it "should create UISearchBar" do
122
+ @controller.search_bar.class.should == UISearchBar
123
+ end
124
+
125
+ it "should create UISegmentedControl" do
126
+ @controller.segmented_control.class.should == UISegmentedControl
127
+ end
128
+
129
+ it "should create UISlider" do
130
+ @controller.slider.class.should == UISlider
131
+ end
132
+
133
+ it "should create UIStepper" do
134
+ @controller.stepper.class.should == UIStepper
135
+ end
136
+
137
+ it "should create UISwitch" do
138
+ @controller.switch.class.should == UISwitch
139
+ end
140
+
141
+ it "should create UITabBar" do
142
+ @controller.tab_bar.class.should == UITabBar
143
+ end
144
+
145
+ it "should create UITableView" do
146
+ @controller.table_view.class.should == UITableView
147
+ end
148
+
149
+ it "should create UITableViewCell" do
150
+ @controller.table_view_cell.class.should == UITableViewCell
151
+ end
152
+
153
+ it "should create UITextField" do
154
+ @controller.text_field.class.should == UITextField
155
+ end
156
+
157
+ it "should create UITextView" do
158
+ @controller.text_view.class.should == UITextView
159
+ end
160
+
161
+ it "should create UIToolbar" do
162
+ @controller.toolbar.class.should == UIToolbar
163
+ end
164
+
165
+ it "should create UIWebView" do
166
+ @controller.web_view.class.should == UIWebView
167
+ end
168
+ end
15
169
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: '0.6'
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: 2013-01-30 00:00:00.000000000 Z
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: RubyMotion DSL for UIKit
15
15
  email:
@@ -40,7 +40,6 @@ files:
40
40
  - lib/simple_view/styles.rb
41
41
  - lib/simple_view/version.rb
42
42
  - lib/simple_view/view_builder.rb
43
- - lib/simple_view/view_proxy.rb
44
43
  - resources/test.jpg
45
44
  - simple_view.gemspec
46
45
  - spec/acceptance/view_anchoring_controller_spec.rb
@@ -53,7 +52,6 @@ files:
53
52
  - spec/layout_spec.rb
54
53
  - spec/styles_spec.rb
55
54
  - spec/view_builder_spec.rb
56
- - spec/view_proxy_spec.rb
57
55
  homepage: https://github.com/seanho/SimpleView
58
56
  licenses: []
59
57
  post_install_message:
@@ -89,4 +87,3 @@ test_files:
89
87
  - spec/layout_spec.rb
90
88
  - spec/styles_spec.rb
91
89
  - spec/view_builder_spec.rb
92
- - spec/view_proxy_spec.rb
@@ -1,47 +0,0 @@
1
- module SimpleView
2
- class ViewProxy
3
- attr_reader :view, :locals
4
-
5
- def initialize view = nil, locals = {}
6
- @view = view
7
- @locals = locals
8
-
9
- if !@locals.nil?
10
- @locals.each do |k, v|
11
- self.class.send :attr_accessor, k
12
- self.instance_variable_set "@#{k}", v
13
- end
14
- end
15
- end
16
-
17
- def add klass, options = {}, &block
18
- subview = create klass, options, &block
19
- view.addSubview(subview) unless view.nil?
20
- subview
21
- end
22
-
23
- def create klass, options = {}, &block
24
- bounds = view.nil? ? CGRectZero : view.bounds
25
- subview = ViewBuilder.view_for klass, bounds, options
26
-
27
- if block_given?
28
- child_layout = ViewProxy.new subview, locals
29
- child_layout.instance_exec subview, &block
30
- end
31
-
32
- subview
33
- end
34
-
35
- [::UIActionSheet, ::UIActivityIndicatorView, ::UIButton, ::UIDatePicker, ::UIImageView, ::UILabel,
36
- ::UIPageControl, ::UIPickerView, ::UIProgressView, ::UIScrollView, ::UISearchBar, ::UISegmentedControl,
37
- ::UISlider, ::UIStepper, ::UISwitch, ::UITabBar, ::UITableView, ::UITableViewCell, ::UITextField, ::UITextView,
38
- ::UIToolbar, ::UIWebView].each do |klass|
39
- shorthand = "#{klass}"[2..-1].underscore.to_sym
40
- define_method(shorthand) do |*args, &block|
41
- options = args[0] || {}
42
- add klass, options, &block
43
- end
44
- end
45
- def rect(options = {}, &block) add(::UIView, options, &block); end
46
- end
47
- end
@@ -1,172 +0,0 @@
1
- describe "SimpleView::ViewProxy" do
2
- it "should convert locals to instance variables" do
3
- view = UIView.alloc.init
4
- local_a = Object.new
5
- local_b = Object.new
6
-
7
- proxy = SimpleView::ViewProxy.new(view, local_a: local_a, local_b: local_b)
8
-
9
- proxy.view.should == view
10
- proxy.local_a.should == local_a
11
- proxy.local_b.should == local_b
12
- end
13
-
14
- describe "#add" do
15
- it "should add view with options" do
16
- alpha = 0.5
17
- backgroundColor = UIColor.redColor
18
-
19
- proxy = SimpleView::ViewProxy.new
20
- view = proxy.add UIView, top: 0, left: 0, width: 10, height: 10, alpha: alpha, backgroundColor: backgroundColor
21
- view.frame.should == CGRectMake(0, 0, 10, 10)
22
- view.alpha.should == alpha
23
- view.backgroundColor.should == backgroundColor
24
- end
25
-
26
- describe "#add with predefined styles" do
27
- it "should add view with default style" do
28
- SimpleView::Styles.define UIView, backgroundColor: UIColor.redColor
29
-
30
- proxy = SimpleView::ViewProxy.new
31
- view = proxy.add UIView
32
- view.backgroundColor.should == UIColor.redColor
33
- end
34
-
35
- it "should add view with custom style" do
36
- SimpleView::Styles.define :blue, backgroundColor: UIColor.blueColor
37
-
38
- proxy = SimpleView::ViewProxy.new
39
- view = proxy.add UIView, styles: :blue
40
- view.backgroundColor.should == UIColor.blueColor
41
- end
42
-
43
- it "should add view with multiple custom styles" do
44
- SimpleView::Styles.define :blue, backgroundColor: UIColor.blueColor
45
- SimpleView::Styles.define :alpha, alpha: 0.5
46
-
47
- proxy = SimpleView::ViewProxy.new
48
- view = proxy.add UIView, styles: [:blue, :alpha]
49
- view.backgroundColor.should == UIColor.blueColor
50
- view.alpha.should == 0.5
51
- end
52
-
53
- it "should add view with custom style overriding default style" do
54
- SimpleView::Styles.define UIView, backgroundColor: UIColor.redColor
55
- SimpleView::Styles.define :blue, backgroundColor: UIColor.blueColor
56
-
57
- proxy = SimpleView::ViewProxy.new
58
- view = proxy.add UIView, styles: :blue
59
- view.backgroundColor.should == UIColor.blueColor
60
- end
61
- end
62
-
63
- it "should execute block" do
64
- proxy = SimpleView::ViewProxy.new
65
- view = proxy.add UIView do
66
- label
67
- end
68
- view.subviews.first.class.should == UILabel
69
- end
70
-
71
- it "should add view to superview" do
72
- super_view = UIView.alloc.init
73
- proxy = SimpleView::ViewProxy.new(super_view)
74
- subview = proxy.add UIView
75
- super_view.subviews.first.should == subview
76
- end
77
- end
78
-
79
- describe "shorthand methods" do
80
- before do
81
- @proxy = SimpleView::ViewProxy.new
82
- end
83
-
84
- it "should create UIActionSheet" do
85
- @proxy.action_sheet.class.should == UIActionSheet
86
- end
87
-
88
- it "should create UIActivityIndicatorView" do
89
- @proxy.activity_indicator_view.class.should == UIActivityIndicatorView
90
- end
91
-
92
- it "should create UIButton" do
93
- @proxy.button.class.should == UIRoundedRectButton
94
- end
95
-
96
- it "should create UIDatePicker" do
97
- @proxy.date_picker.class.should == UIDatePicker
98
- end
99
-
100
- it "should create UIImageView" do
101
- @proxy.image_view.class.should == UIImageView
102
- end
103
-
104
- it "should create UILabel" do
105
- @proxy.label.class.should == UILabel
106
- end
107
-
108
- it "should create UIPageControl" do
109
- @proxy.page_control.class.should == UIPageControl
110
- end
111
-
112
- it "should create UIPickerView" do
113
- @proxy.picker_view.class.should == UIPickerView
114
- end
115
-
116
- it "should create UIProgressView" do
117
- @proxy.progress_view.class.should == UIProgressView
118
- end
119
-
120
- it "should create UIScrollView" do
121
- @proxy.scroll_view.class.should == UIScrollView
122
- end
123
-
124
- it "should create UISearchBar" do
125
- @proxy.search_bar.class.should == UISearchBar
126
- end
127
-
128
- it "should create UISegmentedControl" do
129
- @proxy.segmented_control.class.should == UISegmentedControl
130
- end
131
-
132
- it "should create UISlider" do
133
- @proxy.slider.class.should == UISlider
134
- end
135
-
136
- it "should create UIStepper" do
137
- @proxy.stepper.class.should == UIStepper
138
- end
139
-
140
- it "should create UISwitch" do
141
- @proxy.switch.class.should == UISwitch
142
- end
143
-
144
- it "should create UITabBar" do
145
- @proxy.tab_bar.class.should == UITabBar
146
- end
147
-
148
- it "should create UITableView" do
149
- @proxy.table_view.class.should == UITableView
150
- end
151
-
152
- it "should create UITableViewCell" do
153
- @proxy.table_view_cell.class.should == UITableViewCell
154
- end
155
-
156
- it "should create UITextField" do
157
- @proxy.text_field.class.should == UITextField
158
- end
159
-
160
- it "should create UITextView" do
161
- @proxy.text_view.class.should == UITextView
162
- end
163
-
164
- it "should create UIToolbar" do
165
- @proxy.toolbar.class.should == UIToolbar
166
- end
167
-
168
- it "should create UIWebView" do
169
- @proxy.web_view.class.should == UIWebView
170
- end
171
- end
172
- end