3 Comments

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

1
2
3
4
5
6
7
8
class Car
  include Objection::Compose
  compose_with :engine, :brakes
 
  awoken do
    App::alert("Awake!")
  end
end


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

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

Objective-C

1
2
3
4
5
6
7
8
9
10
11
12
13
@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


@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!