Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
176 lines (141 sloc) 8.42 KB
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>BubbleWrap - A Cocoa wrapper by Matt Aimonetti</title>
<link rel="stylesheet" href="stylesheets/styles.css">
<link rel="stylesheet" href="stylesheets/pygment_trac.css">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-30927742-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="wrapper">
<header>
<h1>BubbleWrap</h1>
<p>
Cocoa wrappers and helpers for <a href="http://www.rubymotion.com/">RubyMotion</a>.
</p>
<p class="view">
<a href="https://github.com/rubymotion/BubbleWrap">View the Project on GitHub <small>mattetti/BubbleWrap</small></a>
<a href="http://rdoc.info/github/rubymotion/BubbleWrap/master/frames">Read the RDoc<small>rdoc.info</small></a>
<a href="http://rubygems.org/gems/bubble-wrap">Get the Ruby Gem<small>gem 'bubble-wrap'</small></a>
<a href="https://groups.google.com/d/forum/bubblewrap">Join the discussion<small>bubblewrap@googlegroups.com</small></a>
<a href="getting_started.html">Using BubbleWrap<small>A guide to getting started with BubbleWrap</small></a>
<a href="hacking.html">Hacking BubbleWrap<small>A guide to working on BubbleWrap</small></a>
<a href="gem.html">Gem Howto<small>Building RubyMotion gems with BW</small></a>
</p>
</header>
<section>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<h1>Creating a RubyMotion gem with BubbleWrap</h1>
<p>Let's say we want to develop a simple library gem that lists the
people in a user's addressbook.</p>
<p>Let's start by initializing an empty gem directory:</p>
<pre><code>$ gem install bundler
$ bundle gem bw-addressbook
</code></pre>
<p>Add BubbleWrap and Rake to your gem's dependencies in <code>bw-addressbook.gemspec</code>:</p>
<div class="highlight">
<pre><span class="no">Gem</span><span class="o">::</span><span class="no">Specification</span><span class="o">.</span><span class="n">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">gem</span><span class="o">|</span>
<span class="n">gem</span><span class="o">.</span><span class="n">add_dependency</span> <span class="s1">'bubble-wrap'</span>
<span class="n">gem</span><span class="o">.</span><span class="n">add_development_dependency</span> <span class="s1">'rake'</span>
<span class="k">end</span>
</pre>
</div>
<p>Then run <code>bundler</code>:</p>
<pre><code>$ bundle
Fetching gem metadata from https://rubygems.org/..
Using rake (0.9.2.2)
Installing bubble-wrap (0.4.0)
Using bw-addressbook (0.0.1) from source at /Users/jnh/Dev/tmp/bw-addressbook
Using bundler (1.1.4)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
</code></pre>
<p>Modify your <code>lib/bw-addressbook.rb</code> to include:</p>
<div class="highlight">
<pre><span class="nb">require</span> <span class="s1">'bw-addressbook/version'</span>
<span class="no">BW</span><span class="o">.</span><span class="n">require</span> <span class="s1">'motion/address_book.rb'</span>
</pre>
</div>
<p>Edit your project's <code>Rakefile</code> to include:</p>
<div class="highlight">
<pre><span class="c1">#!/usr/bin/env rake</span>
<span class="vg">$:</span><span class="o">.</span><span class="n">unshift</span><span class="p">(</span><span class="s2">"/Library/RubyMotion/lib"</span><span class="p">)</span>
<span class="nb">require</span> <span class="s1">'motion/project'</span>
<span class="nb">require</span> <span class="s2">"bundler/gem_tasks"</span>
<span class="no">Bundler</span><span class="o">.</span><span class="n">setup</span>
<span class="no">Bundler</span><span class="o">.</span><span class="n">require</span>
<span class="nb">require</span> <span class="s1">'bubble-wrap/test'</span>
</pre>
</div>
<p>At this point we should have a working RubyMotion environment able to
compile our code as we write it.</p>
<p>Let's start by creating a spec for our address book gem in <code>spec/address_book_spec.rb</code>:</p>
<div class="highlight">
<pre><span class="n">describe</span> <span class="no">AddressBook</span> <span class="k">do</span>
<span class="n">describe</span> <span class="s1">'.list'</span> <span class="k">do</span>
<span class="n">it</span> <span class="s1">'returns an Enumerable'</span> <span class="k">do</span>
<span class="no">AddressBook</span><span class="o">.</span><span class="n">list</span><span class="o">.</span><span class="n">is_a?</span><span class="p">(</span><span class="no">Enumerable</span><span class="p">)</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="kp">true</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</pre>
</div>
<p>Now if you run <code>rake spec</code> you can watch the spec fail:</p>
<pre><code>2012-06-07 11:19:35.506 Untitled[14987:f803] *** Terminating app due to uncaught exception 'NameError', reason: 'uninitialized constant AddressBook (NameError)'
*** First throw call stack:
(0x8f6022 0x286cd6 0x140054 0x291f 0x2645 0x1)
terminate called throwing an exception
</code></pre>
<p>Let's go and define ourselves an <code>AddressBook</code> class in <code>motion/address_book.rb</code>:</p>
<div class="highlight">
<pre><span class="k">class</span> <span class="nc">AddressBook</span>
<span class="k">end</span>
</pre>
</div>
<p>You'll now get a spec failure:</p>
<pre><code>NoMethodError: undefined method `list' for AddressBook:Class
spec.rb:156:in `block in run_spec_block': .list - returns an Enumerable
4:in `execute_block'
spec.rb:156:in `run_spec_block'
spec.rb:171:in `run'
</code></pre>
<p>Well, we'd better go and define it then, eh?</p>
<pre><code>class AddressBook
def self.list
[]
end
end
</code></pre>
<p>I'm going to leave it here for now, but you're welcome to take a look at the
fully working demonstration project on <a href="http://github.com/jamesotron/bw-addressbook-demo">Github</a>.</p>
</body></html>
</section>
<footer>
<p>This project is maintained by:</p>
<ul>
<li><a href="https://github.com/mattetti"><img src="https://secure.gravatar.com/avatar/c69521d6e22fc0bbd69337ec8b1698df?s=24&d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-24.png">Matt Aimonetti</a><a href="http://twitter.com/merbist"><small>@merbist</small></a></li>
<li><a href="https://github.com/siuying"><img src="https://secure.gravatar.com/avatar/ecdf9a83234e21743d77b75fd308b929?s=24&d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-24.png">Francis Chong</a><a href="http://twitter.com/siuying"><small>@siuying</small></a></li>
<li><a href="https://github.com/mneorr"><img src="https://secure.gravatar.com/avatar/9ee164729035736549e4ae6e6e2d7cfc?s=24&d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-24.png">Marin Usalj</a><a href="http://twitter.com/mneorr"><small>@mneorr</small></a></li>
<li><a href="https://github.com/jamesotron"><img src="https://secure.gravatar.com/avatar/d468b182b11cdee834dd1f9777645b79?s=24&d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-24.png">James Harton</a><a href="http://twitter.com/jamesotron"><small>@jamesotron</small></a></li>
<li><a href="https://github.com/janweinkauff"><img src="https://secure.gravatar.com/avatar/89a1b3da3c51c9028598e4c703c6acbf?s=24&d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-24.png">Jan Weinkauff</a><a href="http://twitter.com/janweinkauff"><small>@janweinkauff</small></a></li>
</ul>
</footer>
</div>
<script src="javascripts/scale.fix.js"></script>
</body>
</html>
Something went wrong with that request. Please try again.