Skip to content
This repository was archived by the owner on Mar 23, 2021. It is now read-only.
dan hassin edited this page Jan 9, 2014 · 4 revisions

Getting started – Ruby

  1. In MacRuby, simply drag the Source folder into Xcode in your MacRuby project, and NSRails should be built with your project as normal. For RubyMotion, follow these steps to vendor NSRails:
  • Add a vendor directory on the main level of your RubyMotion app if you don't have one already

  • Copy the nsrails directory (the one with the main Xcode project) into vendor. (You can delete Tests/, but keep Source/ and the Xcode project file).

  • Modify your Rakefile to include NSRails and the CoreData framework:

    Motion::Project::App.setup do |app|
        # Add CoreData as a linked framework (required even if CoreData isn't used)
        app.frameworks << "CoreData"
    
        # Add this line:
        app.vendor_project('vendor/nsrails', :xcode, :target => 'NSRails', :headers_dir => 'Source')
        # OR this line, if you wish to use NSRails with CoreData
        #app.vendor_project('vendor/nsrails', :xcode, :target => 'NSRailsCD', :headers_dir => 'Source')
    
        ...
    end
  1. Make a Ruby class for your Rails model and have it subclass NSRRemoteObject:

    class Post < NSRRemoteObject
      attr_accessor :author, :content, :created_at
    
      # Since the above list of Ruby instance variables can't be accessed from
      # Obj-C, they have to be explicitly defined by overriding 'remoteProperties'
      def remoteProperties
        super + ["author", "content", "created_at"]
      end
    end
  2. Setup. This can go in app_delegate.rb:

    NSRConfig.defaultConfig.appURL = "http://localhost:3000"
    
    # Don't look for camelCase when receiving remote underscored_properties, since we're in Ruby
    NSRConfig.defaultConfig.autoinflectsPropertyNames = false
    
    # If you're using Rails 3
    NSRConfig.defaultConfig.configureToRailsVersion NSRRailsVersion3

Now have fun! These are just examples of how you can use pointers/blocks in Ruby, but see the Objective-C example above for more!

# Get all posts (synchronously)
error_ptr = Pointer.new(:object)
posts = Post.remoteAll(error_ptr)
if !posts
  error = error_ptr[0]
  ...
end

# Get all posts (asynchronously)
Post.remoteAllAsync(lambda do |posts, error| 
                      ...
                    end)

See the documentation for more on what you can do with your new class, or the cookbook for quick NSRRemoteObject recipes.

Clone this wiki locally