Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.
Dan Hassin edited this page Aug 21, 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 this line:
        app.vendor_project('vendor/nsrails', :xcode, :target => 'NSRails', :headers_dir => 'source')
        # OR these lines, if you wish to use NSRails with CoreData
        #app.vendor_project('vendor/nsrails', :xcode, :target => 'NSRailsCD', :headers_dir => 'source')
            #app.frameworks << "CoreData"
    
        ...
    end
  1. Make a Ruby class for your Rails model and have it subclass NSRRemoteObject, or NSRRemoteManagedObject with CoreData:

    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.rootURL = NSURL.URLWithString("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, the fun! Here's an example of how you can use pointers/blocks in Ruby, but see the Objective-C examples in the Readme 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