Couchbase Lite has been released for sometime now, so I thought it was time to give an update on using CouchbaseLite from RubyMotion.

When I ported ToDoLite-iOS to RubyMotion originally there where some bumps in the road, but it worked over all. For example, there were some problems with RubyMotion not handling lambdas the way CouchbaseLite needs them, but this has been resolved since then. If you don't know what I mean by that, you can be happy and forget all about it, or read up on it.

So what do I need now to get going with Couchbase Lite and iOS?

By now it is possible to go 100% Ruby for a RubyMotion project using Couchbase Lite, which is great. Couchbase Lite has been publicly released, is out of beta and has already received much love in terms of patches to make it work even more reliably cross all platforms. In the case of RubyMotion this made things much easier, and the process is by now:

  • Add couchbase-lite via cocoapods
  • Tell RubyMotion where to find the header files
  • Use it!

Installing Couchbase Lite via Cocoapods

Cocoapods is an awesome package manager for iOS and MacOS projects, and it integrates really well with RubyMotion. All there is todo is add cocoapods and motion-cocoapods to your gemfile:

source ‘https://rubygems.org’

gem ‘rake’

# Build dependencies:
gem ‘cocoapods’, ‘~> 0.33.1’
gem ‘motion-cocoapods’, ‘~> 1.4.0’

# Add your dependencies:
gem ‘bubble-wrap’, ‘~> 1.5.0’

Now you can install any cocoapods by adding them to the Rakefile and running “bundle exec rake pod:install”:

Motion::Project::App.setup do |app|
  # Use `rake config’ to see complete project settings.
  app.name = ‘ToDoLite-Motion’
  app.frameworks += [‘Accounts’, ‘Social’]
  app.identifier = ‘com.couchbase.ToDoLite-Motion’

  # Add config directory files
  app.files << Dir.glob(‘./config/*.rb’)

  # Make sure the CouchbaseLite Headers are found
  # this makes CocoaPods kind of redundant but currently is the only way this works
  #
  # http://equip9.org/2014/03/06/adding-couchbase-in-a-rubymotion-app.html
  # https://groups.google.com/forum/#!topic/rubymotion/wVqdLWQ5uao
  #
  app.vendor_project(‘vendor/Pods/couchbase-lite-ios/CouchbaseLite.framework’,
                     :static,
                     products: [‘CouchbaseLite’],
                     headers_dir: ‘Headers’)

  app.codesign_certificate = ‘iPhone Developer: Philipp Fehre (6W7Y595HZQ)’

  app.pods do
    pod ‘couchbase-lite-ios’, ‘~> 1.0’
  end
end

Important side note, make sure to include:

app.vendor_project(‘vendor/Pods/couchbase-lite-ios/CouchbaseLite.framework’,
                     :static,
                     products: [‘CouchbaseLite’],
                     headers_dir: ‘Headers’)

as the headers are not going to be found otherwise.

Using Couchbase Lite from RubyMotion

You can now use Couchbase Lite like you would any other Obj-C library from RubyMotion. For example, to define a view that grabs all the “lists” in the database you can write this:

 def self.queryListsInDatabase database
    view = database.viewNamed(“lists”)
    if !view.mapBlock
      map = lambda { |doc, emit|
        emit.call(doc[“title”], nil) if doc[“type”] == “list”
      }
      view.setMapBlock map, reduceBlock: nil, version: “2”
    end
    view.createQuery
  end

For more details check out the RubyMotion Sample project on Github.

Author

Posted by Philipp Fehre

Philipp Fehre is a Developer Advocate at Couchbase working on creating applications using Ruby/JRuby (on Rails), Rubymotion, Node.js, Java, and Erlang.

Leave a reply