• Stars
    star
    317
  • Rank 127,237 (Top 3 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

BloodMagic is a framework, which gives you a way to create custom property attributes.

BloodMagic

License MIT Β  Build status Β  Version Β  Platform

Objective-C is a powerful language, but sometimes it lacks of custom property attributes, like these:

@property (nonatomic, strong, lazy) ProgressViewService *progressView;
@property (nonatomic, strong, partial) HeaderView *headerView;
@property (nonatomic, strong, final) NSString *almostImmutable;
@property (nonatomic, strong, preference) NSString *authToken;
@property (nonatomic, strong, injectable) id<NetworkClient> client;

@property (nonatomic, strong, anything_you_want) AwesomeView *someAwesomeView;

We can't implement these attributes without hacking on clang, but fortunately, we're able to achieve these effects by means of BloodMagic' spells

FAQ

Blog-post

Presentation by AlexDenisov

Presentation by Ievgen Solodovnykov

Embark on the Dark

CocoaPods:

  pod 'BloodMagic', :git => 'https://github.com/railsware/BloodMagic.git'

Components

$ mkdir -p ./Components.make
# iOS
wget https://raw.githubusercontent.com/AlexDenisov/Components/master/Components.make/BloodMagic/1.0.0/BloodMagic-iOS.make -O ./Components.make/BloodMagic-iOS.make
# OSX
wget https://raw.githubusercontent.com/AlexDenisov/Components/master/Components.make/BloodMagic/1.0.0/BloodMagic-OSX.make -O ./Components.make/BloodMagic-OSX.make

Manually

Alternatively you can use built frameworks for iOS and OSX.

Just drag&drop framework into your project and don't forget to add -all_load, -ObjC and -lc++ or -lstdc++ to OTHER_LINKER_FLAGS

Available Spells

Lazy Initialization

Dependency Injection

Partial Views

Assign-once properties

Preferences (NSUserDefaults wrapper)

BloodMagic has been designed to be extensible, so few more spells will be available soon.

====

Lazy initialization

  pod 'BloodMagic/Lazy', :git => 'https://github.com/railsware/BloodMagic.git'

Initializes object on demand.

If you use Objective-C, then you should be familiar with this code:

@interface ViewController : UIViewController

@property (nonatomic, strong) ProgressViewService *progressViewService;

@end
- (ProgressViewService *)progressViewService
{
    if (_progressViewService == nil) {
      _progressViewService = [ProgressViewService new];
    }
  
    return _progressViewService;
}

But we are able to automate this routine!

Just add BMLazy protocol to your class:

@interface ViewController : NSObject
  <BMLazy>

@property (nonatomic, strong, bm_lazy) ProgressViewService *progressViewService;

@end

and mark any property as @dynamic:

@implementation ViewController

@dynamic progressViewService;

@end

Object progressViewService will be initialized on the first call

self.progressViewService
// or
yourViewController.progressViewService

or when you try to get value for key

[self valueForKey:@"progressViewService"]
// or
[yourViewController valueForKey:@"progressViewService"]

By default it creates an instance with the +new class' method.

In this case progressViewService will be deallocated as a usual property.

Dependency Injection

  pod 'BloodMagic/Injectable', :git => 'https://github.com/railsware/BloodMagic.git'

During the creation of Lazy Initialization spell an interesting side effect was found - Dependency Injection.

It behaves the same way as BMLazy, but uses another approach to instantiate object.

For example, if you need to initialize progressViewService in a special way, you should provide initializer:

BMInitializer *initializer = [BMInitializer injectableInitializer];
initializer.propertyClass = [ProgressViewService class]; // optional, uses NSObject by default
initializer.containerClass = [ViewController class]; // optional, uses NSObject by default
initializer.initializer = ^id (id sender){
    return [[ProgressViewService alloc] initWithViewController:sender];
};
[initializer registerInitializer];

Note: containerClass doesn't apply on derived classes, to achieve such behavior you should specify containerClass explicitly.

This spell is very useful when dealing with the singleton

BMInitializer *initializer = [BMInitializer injectableInitializer];
initializer.propertyClass = [RequestManager class];
initializer.initializer = ^id (id sender){
    static id singleInstance = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
      singleInstance = [RequestManager new];
    });
    return singleInstance;
};
[initializer registerInitializer];

Thus, neither the RequestManager nor the class that uses it, will not be aware about his singleton nature.

Adepts of SRP school must approve ;)

Also, you're able to use @protocols as well

BMInitializer *initializer = [BMInitializer injectableInitializer];
initializer.protocols = @[ @protocol(ProgressViewServiceProtocol) ];
initializer.initializer = ^id (id sender){
    return [[ProgressViewService alloc] initWithViewController:sender];
};
[initializer registerInitializer];
Injection hooks

BMInjectable module provides a hook system to catch the object creation. To enable these hooks just create instance method named propertyNameInjected:.

For example:

@implementation ViewController

@injectable(progressViewService)

- (void)progressViewServiceInjected:(ProgressViewService *service)
{
    service.title = self.title;
}

@end

Partial Views

  pod 'BloodMagic/Partial', :git => 'https://github.com/railsware/BloodMagic.git'

Instantiates view from xib on demand, similar to Lazy module. This spell might be helpful if you have reusable views.

For example:

You need to show the same user info in table cells (UsersListViewController) and in some header view (UserProfileViewController). It makes sense to create one UserView.xib associated with UserView class and use it through the whole app.

So it may looks like this:

// Cell used from UsersListViewController
// Created manually
@implementation UserViewCell
{
    UserView *_userView;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSString *nibName = NSStringFromClass([UserView class]);
        UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
        _userView = [[nib instantiateWithOwner:nil options:nil] lastObject];
        [self addSubview:_userView];
    }
    return self;
}

@end

// View used from UserProfileViewController
// Created from xib
@implementation UserHeaderView
{
    UserView *_userView;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    NSString *nibName = NSStringFromClass([UserView class]);
    UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
    _userView = [[nib instantiateWithOwner:nil options:nil] lastObject];
    [self addSubview:_userView];
}

@end

Both cases use the same, similar code. So, BloodMagic does nothing special, just hides this boilerplate:

#import <BloodMagic/Partial.h>

@interface UserViewCell ()
    <BMPartial>

@property (nonatomic, strong, bm_partial) UserView *userView;

@end

@implementation UserViewCell

@dynamic userView;

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addSubview:self.userView];
    }
    return self;
}

@end

// ...

@interface UserHeaderView ()
    <BMPartial>

@property (nonatomic, strong, bm_partial) UserView *userView;

@end

@implementation UserHeaderView

@dynamic userView;

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self addSubview:self.userView];
}

@end

Assign-once properties

  pod 'BloodMagic/Final', :git => 'https://github.com/railsware/BloodMagic.git'

Java provides final keyword, which determines (at least) that value can't be changed after initialization.

From now this feature available in Objective-C, via BloodMagic.

#import <BloodMagic/Final.h>

@interface FinalizedObject : NSObject
    <BMFinal>

@property (nonatomic, strong, bm_final) NSString *almostImmutableProperty;

@end

@implementation FinalizedObject

@dynamic almostImmutableProperty;

@end

// ...

FinalizedObject *object = [FinalizedObject new];
object.almostImmutableProperty = @"Initial value"; // everything is fine
object.almostImmutableProperty = @"Another value"; // exception will be thrown

Preferences

pod 'BloodMagic/Preference', :git => 'https://github.com/railsware/BloodMagic.git'

Enjoy the simplest way to deal with NSUserDefaults

#import <BloodMagic/Preference.h>

@interface Settings : NSObject
    <BMPreference>

@property (nonatomic, strong, bm_preference) NSString *nickname;

@end

@implementation Settings

@dynamic nickname;

@end

// ...

Settings *settings = [Settings new];
settings.nickname = @"AlexDenisov"; // @"AlexDenisov" goes to [NSUserDefaults standardUserDefaults] with key "nickname"
NSLog(@"My name is: %@", settings.nickname); // reads object for key "nickname" from [NSUserDefaults standardUserDefaults]

Side effects (aka bugs)

BloodMagic may have side effects, if you find one, please, open issue or send us a pull request.

Those actions will help us to protect you from mutilation.

More Repositories

1

upterm

A terminal emulator for the 21st century.
TypeScript
19,318
star
2

js-routes

Brings Rails named routes to javascript
Ruby
1,592
star
3

Sleipnir

BDD-style framework for Swift
Swift
847
star
4

bozon

πŸ›  Command line tool for building, testing and publishing modern Electron applications
JavaScript
757
star
5

global

"Global" provides accessor methods for your configuration data
Ruby
282
star
6

applepie

Semantic and Modular CSS Toolkit
CSS
280
star
7

rack_session_access

Rack middleware that provides access to rack.session environment
Ruby
259
star
8

caphub

Generate centralized capistrano skeleton for multiple deployment
Ruby
230
star
9

smt_rails

Shared mustache templates for rails 3.
Ruby
110
star
10

http_logger

Log your http api calls just like SQL queries
Ruby
106
star
11

rspec-example_steps

Given/When/Then steps for RSpec examples
Ruby
85
star
12

sht_rails

Shared handlebars templates for Rails 3
Ruby
76
star
13

actionmailer-balancer

A Ruby gem to send your ActionMailer mail through one of several delivery methods, selected by weight.
Ruby
76
star
14

capistrano-multiconfig

Capistrano extension that allows to use multiple configurations
Ruby
66
star
15

passenger-initscript

Manage multiple passenger instances
Shell
54
star
16

skypekit

Ruby FFI interface to libskypekit C library
Ruby
47
star
17

newrelic_platform_plugins

Ruby
41
star
18

piro

PiRo - it's Rocket for you Pivotal Tracker account
CoffeeScript
40
star
19

zero_deploy

Significantly improves typical deployment speed
Ruby
40
star
20

mailtrap-nodejs

Official mailtrap.io Node.js client
TypeScript
35
star
21

capistrano-calendar

Deployment event creation on (google) calendar service
Ruby
29
star
22

capistrano-patch

Capistrano patch recipes
Ruby
29
star
23

scaffy.railsware.com

Approach for writing and organizing your CSS for large-scale projects
JavaScript
29
star
24

mailtrap-php

The official mailtrap.io PHP client
PHP
20
star
25

libskypekit

Thread-safe C library with synchronous API using asynchronous C++ SkypeKit SDK
C
19
star
26

backfiller

The backfill machine for database records with null columns
Ruby
16
star
27

capistrano-ci

Ruby
16
star
28

indeed

Indeed.com integration plugin
Ruby
16
star
29

mailtrap-ruby

The official mailtrap.io Ruby client
Ruby
15
star
30

RBRouteBuilder

Build routes without strings and headache
C++
15
star
31

sprinkle_recipes

Railsware sprinkle recipes
Ruby
14
star
32

capistrano-uptodate

Capistrano extension that automatically check local repository with remote repository
Ruby
14
star
33

activeresource-persistent

HTTP persistent connection support for ActiveResource
Ruby
11
star
34

haproxy-slow-fast-request-balancer

HAProxy as "Slow-Fast" Request Balancer
10
star
35

generator-electron-app

Yeoman generator to scaffold Electron app
JavaScript
9
star
36

scout-app-plugins

Useful plugins for the Scout Server Monitoring and Reporting Tool
Ruby
9
star
37

mailtrap-python

Official mailtrap.io Python client
Python
8
star
38

web-bundler

WebResourceBundler bundling particular resource (css or js) in one file. Encoding images in base64 and putting then in css directly.
Ruby
8
star
39

gcal4ruby

Author: Mike Reich. GCal4Ruby is a Ruby Gem that can be used to interact with the current version of the Google Calendar API. GCal4Ruby provides the following features: Create and edit calendar events, Add and invite users to events, Set reminders, Make recurring events.
Ruby
8
star
40

db_structure_ext

Extended rails tasks db:structure:dump/load that supports mysql views/triggers/routines
Ruby
7
star
41

github-actions

A collection of GitHub actions used at Railsware
JavaScript
7
star
42

jdt

JSON Driven Templates
JavaScript
7
star
43

shelltoad

Command line interface for airbrake (http://airbrake.io/)
Ruby
7
star
44

i18n_template

18nTemplate is made to extract phrases and translate html/xhtml/xml document or erb templates
Ruby
6
star
45

capistrano-changelog

Chagelog based on Git commits with a Pivotal story tags
Ruby
5
star
46

go-global

Golang configuration reader for AWS Parameter Store and more
Go
4
star
47

chain_flow

Helps to refactor complex data processing
Ruby
4
star
48

fakes3server

Fake AWS S3 server for local development
Go
4
star
49

jOverlay

Overlay jQuery plugin
JavaScript
4
star
50

dev_vagrant_box

Vagrant box for development (ruby, rails)
4
star
51

yaml_settings

YamlSettings is a simple configuration / settings solution that uses an ERB enabled YAML file.
Ruby
3
star
52

multiversion

Use Bundler and RVM to test your library against different gem versions and/or ruby versions.
Ruby
3
star
53

ui-library

UI Library Template
CSS
2
star
54

clicktale

Clicktale rails plugin by Railsware
Ruby
2
star
55

railsware.github.com

CSS
2
star
56

capybara-feature_helpers

Ruby
2
star
57

backbone_showcase

Ruby
2
star
58

em-rest-client

EventMachine::HttpRequest adapter for HTTP REST client
Ruby
2
star
59

newrelic_em_http

New Relic EM::HTTP instrumentation
Ruby
2
star
60

capistrano-strategy-copy-partial

Capistrano deploy strategy to transfer subdirectory of repository
Ruby
2
star
61

hbase-driver

Small and handy Ruby driver for HBase
Ruby
2
star
62

acme-aws-lambda

AWS Lambda function to generate Letsencrypt certificates (need AWS S3 and Route53)
Ruby
2
star
63

showroom

Ruby
2
star
64

email_templates

HTML
2
star
65

rest_facebook

Lightweight ruby facebook client
Ruby
1
star
66

plunger

Code review tool
Python
1
star
67

cat-aws-ssm-param

Go
1
star
68

rw-study-wishlist

Ruby
1
star
69

template

Rails project template
JavaScript
1
star
70

chef-rwci

Ruby
1
star
71

office-iot

Objective-C
1
star
72

simple_on_couch

Example of CouchApp application for RW articles
JavaScript
1
star
73

gdoc_mapreduce

JavaScript
1
star
74

scaffy

Repo moved to scaffy.railsware.com
1
star
75

highrise_assist

Assist for 37signals' highrise
Ruby
1
star
76

sidekiq_unique_retries

Unique Retries for Sidekiq
Ruby
1
star
77

skypekit_pure

Skypekit on pure ruby
Ruby
1
star
78

aws-ecs-tools

Ruby
1
star
79

capybara_mock

CapybaraMock
Ruby
1
star
80

handlebars_assets_i18n

Very simple handlebars_assets internationalization for .hamlbars and .slimbars templates
Ruby
1
star
81

paypal-sdk-http-adapters

HTTP Adapters for PayPal SDK
Ruby
1
star
82

harvest_report

Harvert reports for non-admin harvest users
Ruby
1
star
83

rest-client-adapters

RestClient Adapters
Ruby
1
star
84

versioned_item

Ruby
1
star
85

cordova-baxi-plugin

Java
1
star
86

column_info_reset

Reset ActiveRecord column info when unknown column exception occurs
Ruby
1
star
87

gdoc_pivotal

Gdoc updater for pivotal
JavaScript
1
star
88

mailtrap-examples

Mailtrap repo examples
HTML
1
star
89

mailtrap-elixir

The official mailtrap.io Elixir client
Elixir
1
star