We're hiring!

We're actively seeking designers and developers for all three of our locations.

Dependency Injection in RubyMotion with Objection

Objection is a popular dependency injection library for Objective-C, created by Atomic Object and yours truly. I’m happy to announce that it has expanded its horizons beyond Objective-C and it can now be used in one of my favorite languages — Ruby.

There’s also Motion Objection, a companion library to Objection that can be used in RubyMotion. It has all of the power and speed of Objection plus the declarative affordances that the Ruby language provides.

I’ve provided an example below that illustrates how Objection can be used in RubyMotion along with an equivalent example in Objective-C.

Ruby

class Car
  include Objection::Compose
  compose_with :engine, :brakes

  awoken do
    App::alert("Awake!")
  end
end

Objective-C

@interface Car
@property(nonatomic, strong) Engine *engine;
@property(nonatomic, strong) Car *car;
@end

@implementation Car
objection_requires(@"car", @"engine")
@synthesize car, engine;

- (void)awakeFromObjection {
  // Alert!
}
@end

You can learn even more about Motion Objection here. Enjoy!
 

Justin DeWind (46 Posts)

This entry was posted in Ruby Motion and tagged , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

3 Comments

  1. Posted September 6, 2013 at 5:31 am

    This looks cool – congrats.

    Its interesting that DI has not gained much traction in Ruby until recently . . . although Jim Weirrich (author of popular tools like Rake and Rspec) wrote a DI container called Dim. So it certainly has some respected advocates.

    Even with Ruby’s dynamism it seems it would still be helpful to have some “rails” around the DI pattern. (‘scuse the pun). . .

    Nice to see a solution that hits Ruby Motion’s sweet spots.

    • Justin DeWind
      Posted September 6, 2013 at 8:10 am

      Jasper,

      Thanks!

      Ruby is dynamic enough where the need for a container isn’t always necessary to use dependency injection. In the case of RubyMotion that isn’t entirely true because it is bridged and bound to the Objective-C runtime.

  2. Posted October 11, 2013 at 5:55 am

    Justin,

    Indeed – I posted about that in my FAQ when I released Typhoon framework.

    “The pattern applies, the framework is not necessary”

    . . . nonetheless, I found Jim’s container interesting.