Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Sean: Removed block scoping

  • Loading branch information...
commit 12895affd5136d9544efc7026cdbeed1ecb5e9f9 1 parent 7faeb3e
@seanhotw seanhotw authored
View
52 README.md
@@ -2,7 +2,37 @@
RubyMotion DSL for UIKit.
-Demo app: [Currency](https://github.com/seanho/CurrencyApp-RubyMotion)
+## Change log
+
+__Version 0.6__
+
+Breaking changes:
+
+Block scoping is removed, i.e. no need to pass in locals.
+
+Previous
+````ruby
+class ViewController
+ def viewDidLoad
+ setup view, controller: self do
+ table_view delegate: controller, dataSource: controller
+ end
+ end
+end
+````
+
+Now
+````ruby
+class ViewController
+ def viewDidLoad
+ setup view do
+ table_view delegate: self, dataSource: self
+ end
+ end
+end
+````
+
+`add` is renamed to `add_view` for clarity.
## Installation
@@ -61,7 +91,7 @@ SimpleView provides shorthand methods for most UIKit classes
def viewDidLoad
setup view do
label text: 'Hi there!' # shorthand
- add UILabel, text: 'Hi there!' # same as above
+ add_view UILabel, text: 'Hi there!' # same as above
end
end
````
@@ -96,7 +126,7 @@ SimpleView can work with any custom views and controls
````ruby
setup view do
- add CustomViewClass, name: "custom_view"...
+ add_view CustomViewClass, name: "custom_view"...
end
````
@@ -168,22 +198,6 @@ setup view do
end
````
-### Passing in locals
-
-Reference to the controller/model or other variables in the setup block
-
-````ruby
-class ViewController
- include SimpleView::Layout
-
- def viewDidLoad
- setup view, controller: self do
- table_view delegate: controller, dataSource: controller
- end
- end
-end
-````
-
### View tagging with string
Tag view with name string and find them with ease
View
10 app/simple_view_controller.rb
@@ -6,16 +6,16 @@ class SimpleViewController < UIViewController
def viewDidLoad
super
+ self.title = 'Demo'
+
@demos = [
{caption: 'View Anchoring', controller: ViewAnchoringController},
{caption: 'User Info', controller: UserInfoController}
]
- setup view, controller: self do
- controller.title = 'Demo'
-
- table_view delegate: controller, dataSource: controller, width: 100.percent, height: 100.percent do
- view.registerClass UITableViewCell, forCellReuseIdentifier: DEFAULT_CELL_ID
+ setup view do
+ table_view delegate: self, dataSource: self, width: 100.percent, height: 100.percent do |table|
+ table.registerClass UITableViewCell, forCellReuseIdentifier: DEFAULT_CELL_ID
end
end
end
View
12 app/user_info_controller.rb
@@ -6,20 +6,20 @@ class UserInfoController < UIViewController
def viewDidLoad
super
+ self.title = 'User Info'
+
@data = [
[{title: 'mobile', text: '123456789'}],
[{title: 'email', text: 'nyan.cat@meme.com'}],
[{title: 'Facebook', text: 'Nyan Cat'}]
]
- setup view, controller: self do
- controller.title = 'User Info'
-
+ setup view do
table_view style: UITableViewStyleGrouped,
- delegate: controller, dataSource: controller,
- width: 100.percent, height: 100.percent do |table_view|
+ delegate: self, dataSource: self,
+ width: 100.percent, height: 100.percent do |table|
- table_view.tableHeaderView = UserInfoHeader.alloc.initWithFrame [[0, 0], [view.width, 80]]
+ table.tableHeaderView = UserInfoHeader.alloc.initWithFrame [[0, 0], [view.width, 80]]
end
end
end
View
6 app/view_anchoring_controller.rb
@@ -2,13 +2,13 @@ class ViewAnchoringController < UIViewController
include SimpleView::Layout
def viewDidLoad
+ self.title = "View Anchoring"
+
SimpleView::Styles.define :square,
width: 20,
height: 20
- setup view, controller: self do
- controller.title = "View Anchoring"
-
+ setup view do
rect name: 'fill', styles: :square, backgroundColor: "#000", top: 0, left: 0, bottom: 0, right: 0
rect name: 'tl', styles: :square, backgroundColor: "#990000", top: 0, left: 0
View
50 lib/simple_view/layout.rb
@@ -1,8 +1,52 @@
module SimpleView
module Layout
- def setup view, locals = {}, &block
- proxy = ViewProxy.new view, locals
- proxy.instance_eval &block if block_given?
+ extend self
+
+ def setup view, &block
+ view_stack.push view
+ block.call if block_given?
+ view_stack.pop
+ end
+
+ def add_view klass, options = {}, &block
+ subview = create_view klass, options, &block
+
+ view_stack.last.addSubview(subview) unless view_stack.empty?
+
+ subview
+ end
+
+ def create_view klass, options = {}, &block
+ bounds = view_stack.empty? ? CGRectZero : view_stack.last.bounds
+ subview = ViewBuilder.view_for klass, bounds, options
+
+ view_stack.push subview
+ block.call(subview) if block_given?
+ view_stack.pop
+
+ subview
+ end
+
+ def view_stack
+ @view_stack ||= []
+ end
+
+ def self.included base
+ base.class_eval do
+ [::UIActionSheet, ::UIActivityIndicatorView, ::UIButton, ::UIDatePicker, ::UIImageView, ::UILabel,
+ ::UIPageControl, ::UIPickerView, ::UIProgressView, ::UIScrollView, ::UISearchBar, ::UISegmentedControl,
+ ::UISlider, ::UIStepper, ::UISwitch, ::UITabBar, ::UITableView, ::UITableViewCell, ::UITextField, ::UITextView,
+ ::UIToolbar, ::UIWebView].each do |klass|
+
+ shorthand = "#{klass}"[2..-1].underscore.to_sym
+
+ define_method(shorthand) do |*args, &block|
+ options = args[0] || {}
+ add_view klass, options, &block
+ end
+ end
+ def rect(options = {}, &block) add_view(::UIView, options, &block); end
+ end
end
end
end
View
2  lib/simple_view/version.rb
@@ -1,3 +1,3 @@
module SimpleView
- VERSION = "0.5.1"
+ VERSION = "0.6"
end
View
47 lib/simple_view/view_proxy.rb
@@ -1,47 +0,0 @@
-module SimpleView
- class ViewProxy
- attr_reader :view, :locals
-
- def initialize view = nil, locals = {}
- @view = view
- @locals = locals
-
- if !@locals.nil?
- @locals.each do |k, v|
- self.class.send :attr_accessor, k
- self.instance_variable_set "@#{k}", v
- end
- end
- end
-
- def add klass, options = {}, &block
- subview = create klass, options, &block
- view.addSubview(subview) unless view.nil?
- subview
- end
-
- def create klass, options = {}, &block
- bounds = view.nil? ? CGRectZero : view.bounds
- subview = ViewBuilder.view_for klass, bounds, options
-
- if block_given?
- child_layout = ViewProxy.new subview, locals
- child_layout.instance_exec subview, &block
- end
-
- subview
- end
-
- [::UIActionSheet, ::UIActivityIndicatorView, ::UIButton, ::UIDatePicker, ::UIImageView, ::UILabel,
- ::UIPageControl, ::UIPickerView, ::UIProgressView, ::UIScrollView, ::UISearchBar, ::UISegmentedControl,
- ::UISlider, ::UIStepper, ::UISwitch, ::UITabBar, ::UITableView, ::UITableViewCell, ::UITextField, ::UITextView,
- ::UIToolbar, ::UIWebView].each do |klass|
- shorthand = "#{klass}"[2..-1].underscore.to_sym
- define_method(shorthand) do |*args, &block|
- options = args[0] || {}
- add klass, options, &block
- end
- end
- def rect(options = {}, &block) add(::UIView, options, &block); end
- end
-end
View
162 spec/layout_spec.rb
@@ -1,9 +1,10 @@
+
describe "SimpleView::Layout" do
- describe "#setup" do
- class DummyController
- include SimpleView::Layout
- end
+ class DummyController
+ include SimpleView::Layout
+ end
+ describe "#setup" do
it "should execute the block within view's scope" do
view = UIView.alloc.initWithFrame(CGRectZero)
DummyController.new.setup view do
@@ -12,4 +13,157 @@ class DummyController
view.frame.should == CGRectMake(0, 0, 10, 10)
end
end
+
+ describe "#add" do
+ before do
+ @controller = DummyController.new
+ end
+
+ it "should add view with options" do
+ alpha = 0.5
+ backgroundColor = UIColor.redColor
+
+ view = @controller.add_view UIView, top: 0, left: 0, width: 10, height: 10, alpha: alpha, backgroundColor: backgroundColor
+ view.frame.should == CGRectMake(0, 0, 10, 10)
+ view.alpha.should == alpha
+ view.backgroundColor.should == backgroundColor
+ end
+
+ describe "#add with predefined styles" do
+ it "should add view with default style" do
+ SimpleView::Styles.define UIView, backgroundColor: UIColor.redColor
+
+ view = @controller.add_view UIView
+ view.backgroundColor.should == UIColor.redColor
+ end
+
+ it "should add view with custom style" do
+ SimpleView::Styles.define :blue, backgroundColor: UIColor.blueColor
+
+ view = @controller.add_view UIView, styles: :blue
+ view.backgroundColor.should == UIColor.blueColor
+ end
+
+ it "should add view with multiple custom styles" do
+ SimpleView::Styles.define :blue, backgroundColor: UIColor.blueColor
+ SimpleView::Styles.define :alpha, alpha: 0.5
+
+ view = @controller.add_view UIView, styles: [:blue, :alpha]
+ view.backgroundColor.should == UIColor.blueColor
+ view.alpha.should == 0.5
+ end
+
+ it "should add view with custom style overriding default style" do
+ SimpleView::Styles.define UIView, backgroundColor: UIColor.redColor
+ SimpleView::Styles.define :blue, backgroundColor: UIColor.blueColor
+
+ view = @controller.add_view UIView, styles: :blue
+ view.backgroundColor.should == UIColor.blueColor
+ end
+ end
+
+ it "should add view to superview" do
+ super_view = UIView.alloc.init
+
+ @controller.setup super_view do
+ @controller.add_view UIView
+ end
+
+ super_view.subviews.first.class.should == UIView
+ end
+ end
+
+ describe "shorthand methods" do
+ before do
+ @controller = DummyController.new
+ end
+
+ it "should create UIActionSheet" do
+ @controller.action_sheet.class.should == UIActionSheet
+ end
+
+ it "should create UIActivityIndicatorView" do
+ @controller.activity_indicator_view.class.should == UIActivityIndicatorView
+ end
+
+ it "should create UIButton" do
+ @controller.button.class.should == UIRoundedRectButton
+ end
+
+ it "should create UIDatePicker" do
+ @controller.date_picker.class.should == UIDatePicker
+ end
+
+ it "should create UIImageView" do
+ @controller.image_view.class.should == UIImageView
+ end
+
+ it "should create UILabel" do
+ @controller.label.class.should == UILabel
+ end
+
+ it "should create UIPageControl" do
+ @controller.page_control.class.should == UIPageControl
+ end
+
+ it "should create UIPickerView" do
+ @controller.picker_view.class.should == UIPickerView
+ end
+
+ it "should create UIProgressView" do
+ @controller.progress_view.class.should == UIProgressView
+ end
+
+ it "should create UIScrollView" do
+ @controller.scroll_view.class.should == UIScrollView
+ end
+
+ it "should create UISearchBar" do
+ @controller.search_bar.class.should == UISearchBar
+ end
+
+ it "should create UISegmentedControl" do
+ @controller.segmented_control.class.should == UISegmentedControl
+ end
+
+ it "should create UISlider" do
+ @controller.slider.class.should == UISlider
+ end
+
+ it "should create UIStepper" do
+ @controller.stepper.class.should == UIStepper
+ end
+
+ it "should create UISwitch" do
+ @controller.switch.class.should == UISwitch
+ end
+
+ it "should create UITabBar" do
+ @controller.tab_bar.class.should == UITabBar
+ end
+
+ it "should create UITableView" do
+ @controller.table_view.class.should == UITableView
+ end
+
+ it "should create UITableViewCell" do
+ @controller.table_view_cell.class.should == UITableViewCell
+ end
+
+ it "should create UITextField" do
+ @controller.text_field.class.should == UITextField
+ end
+
+ it "should create UITextView" do
+ @controller.text_view.class.should == UITextView
+ end
+
+ it "should create UIToolbar" do
+ @controller.toolbar.class.should == UIToolbar
+ end
+
+ it "should create UIWebView" do
+ @controller.web_view.class.should == UIWebView
+ end
+ end
end
View
172 spec/view_proxy_spec.rb
@@ -1,172 +0,0 @@
-describe "SimpleView::ViewProxy" do
- it "should convert locals to instance variables" do
- view = UIView.alloc.init
- local_a = Object.new
- local_b = Object.new
-
- proxy = SimpleView::ViewProxy.new(view, local_a: local_a, local_b: local_b)
-
- proxy.view.should == view
- proxy.local_a.should == local_a
- proxy.local_b.should == local_b
- end
-
- describe "#add" do
- it "should add view with options" do
- alpha = 0.5
- backgroundColor = UIColor.redColor
-
- proxy = SimpleView::ViewProxy.new
- view = proxy.add UIView, top: 0, left: 0, width: 10, height: 10, alpha: alpha, backgroundColor: backgroundColor
- view.frame.should == CGRectMake(0, 0, 10, 10)
- view.alpha.should == alpha
- view.backgroundColor.should == backgroundColor
- end
-
- describe "#add with predefined styles" do
- it "should add view with default style" do
- SimpleView::Styles.define UIView, backgroundColor: UIColor.redColor
-
- proxy = SimpleView::ViewProxy.new
- view = proxy.add UIView
- view.backgroundColor.should == UIColor.redColor
- end
-
- it "should add view with custom style" do
- SimpleView::Styles.define :blue, backgroundColor: UIColor.blueColor
-
- proxy = SimpleView::ViewProxy.new
- view = proxy.add UIView, styles: :blue
- view.backgroundColor.should == UIColor.blueColor
- end
-
- it "should add view with multiple custom styles" do
- SimpleView::Styles.define :blue, backgroundColor: UIColor.blueColor
- SimpleView::Styles.define :alpha, alpha: 0.5
-
- proxy = SimpleView::ViewProxy.new
- view = proxy.add UIView, styles: [:blue, :alpha]
- view.backgroundColor.should == UIColor.blueColor
- view.alpha.should == 0.5
- end
-
- it "should add view with custom style overriding default style" do
- SimpleView::Styles.define UIView, backgroundColor: UIColor.redColor
- SimpleView::Styles.define :blue, backgroundColor: UIColor.blueColor
-
- proxy = SimpleView::ViewProxy.new
- view = proxy.add UIView, styles: :blue
- view.backgroundColor.should == UIColor.blueColor
- end
- end
-
- it "should execute block" do
- proxy = SimpleView::ViewProxy.new
- view = proxy.add UIView do
- label
- end
- view.subviews.first.class.should == UILabel
- end
-
- it "should add view to superview" do
- super_view = UIView.alloc.init
- proxy = SimpleView::ViewProxy.new(super_view)
- subview = proxy.add UIView
- super_view.subviews.first.should == subview
- end
- end
-
- describe "shorthand methods" do
- before do
- @proxy = SimpleView::ViewProxy.new
- end
-
- it "should create UIActionSheet" do
- @proxy.action_sheet.class.should == UIActionSheet
- end
-
- it "should create UIActivityIndicatorView" do
- @proxy.activity_indicator_view.class.should == UIActivityIndicatorView
- end
-
- it "should create UIButton" do
- @proxy.button.class.should == UIRoundedRectButton
- end
-
- it "should create UIDatePicker" do
- @proxy.date_picker.class.should == UIDatePicker
- end
-
- it "should create UIImageView" do
- @proxy.image_view.class.should == UIImageView
- end
-
- it "should create UILabel" do
- @proxy.label.class.should == UILabel
- end
-
- it "should create UIPageControl" do
- @proxy.page_control.class.should == UIPageControl
- end
-
- it "should create UIPickerView" do
- @proxy.picker_view.class.should == UIPickerView
- end
-
- it "should create UIProgressView" do
- @proxy.progress_view.class.should == UIProgressView
- end
-
- it "should create UIScrollView" do
- @proxy.scroll_view.class.should == UIScrollView
- end
-
- it "should create UISearchBar" do
- @proxy.search_bar.class.should == UISearchBar
- end
-
- it "should create UISegmentedControl" do
- @proxy.segmented_control.class.should == UISegmentedControl
- end
-
- it "should create UISlider" do
- @proxy.slider.class.should == UISlider
- end
-
- it "should create UIStepper" do
- @proxy.stepper.class.should == UIStepper
- end
-
- it "should create UISwitch" do
- @proxy.switch.class.should == UISwitch
- end
-
- it "should create UITabBar" do
- @proxy.tab_bar.class.should == UITabBar
- end
-
- it "should create UITableView" do
- @proxy.table_view.class.should == UITableView
- end
-
- it "should create UITableViewCell" do
- @proxy.table_view_cell.class.should == UITableViewCell
- end
-
- it "should create UITextField" do
- @proxy.text_field.class.should == UITextField
- end
-
- it "should create UITextView" do
- @proxy.text_view.class.should == UITextView
- end
-
- it "should create UIToolbar" do
- @proxy.toolbar.class.should == UIToolbar
- end
-
- it "should create UIWebView" do
- @proxy.web_view.class.should == UIWebView
- end
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.