Created
May 25, 2014 21:42
-
-
Save dblandin/4456c1ac27c1a7225737 to your computer and use it in GitHub Desktop.
Example RubyMotion app using the Routable gem with a Rails-like routes.rb file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/alphabet_controller.rb | |
class AlphabetController < UITableViewController | |
def viewDidLoad | |
super | |
self.title = 'Alphabet' | |
view.backgroundColor = UIColor.whiteColor | |
end | |
# UITableViewDataSource | |
def tableView(table_view, numberOfRowsInSection: section) | |
letters.count | |
end | |
def tableView(table_view, cellForRowAtIndexPath: index_path) | |
UITableViewCell.alloc.initWithStyle(UITableViewStylePlain, reuseIdentifier: 'LetterCell').tap do |cell| | |
cell.textLabel.text = letters[index_path.row] | |
end | |
end | |
# UITableViewDelegate | |
def tableView(tableView, didSelectRowAtIndexPath: index_path) | |
Routable::Router.router.open("letters/#{letters[index_path.row]}") | |
end | |
private | |
def letters | |
@letters ||= ('A'..'Z').to_a | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/app_delegate.rb | |
class AppDelegate | |
def application(application, didFinishLaunchingWithOptions: launch_options) | |
true | |
router.navigation_controller = UINavigationController.new | |
initialize_main_controller | |
Routable::Router.router.open('alphabet') | |
end | |
private | |
def initialize_main_controller | |
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) | |
@window.setRootViewController(router.navigation_controller) | |
@window.makeKeyAndVisible | |
end | |
def router | |
Routable::Router.router | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Gemfile | |
source 'https://rubygems.org' | |
gem 'rake' | |
gem 'routable' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/letter_controller.rb | |
class LetterController < UIViewController | |
def initWithParams(params = {}) | |
init.tap do |controller| | |
controller.instance_variable_set(:@letter, params[:letter]) | |
end | |
end | |
def viewDidLoad | |
super | |
self.title = letter_label.text = @letter | |
view.backgroundColor = UIColor.whiteColor | |
view.addSubview(letter_label) | |
setup_constraints | |
end | |
def setup_constraints | |
[NSLayoutAttributeCenterX, NSLayoutAttributeCenterY].each do |attribute| | |
view.addConstraint( | |
NSLayoutConstraint.constraintWithItem( | |
letter_label, | |
attribute: attribute, | |
relatedBy: NSLayoutRelationEqual, | |
toItem: view, | |
attribute: attribute, | |
multiplier: 1.0, | |
constant: 0.0 | |
) | |
) | |
end | |
end | |
def letter_label | |
@letter_label ||= UILabel.new.tap do |label| | |
label.font = UIFont.fontWithName('Helvetica', size: 96.0) | |
label.translatesAutoresizingMaskIntoConstraints = false | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/routes.rb | |
RoutesExample::Application.routes do |r| | |
r.map('alphabet', controller: AlphabetController, resets: true, shared: true) | |
r.map('letters/:letter', controller: LetterController) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/routes_manager.rb | |
module RoutesExample | |
module Application | |
def self.routes | |
@@instance ||= RoutesManager.new | |
yield @@instance | |
end | |
class RoutesManager | |
def map(key, options = {}, &block) | |
router.map(key, options[:controller], options, &block) | |
end | |
private | |
def router | |
@router ||= Routable::Router.router | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment