Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Wrapping iOS with RubyMotion

Wrapping iOS with RubyMotion

All about writing wrappers for RubyMotion

From RubyMotion #inspect 2013

Clay Allsopp

March 29, 2013
Tweet

More Decks by Clay Allsopp

Other Decks in Programming

Transcript

  1. module Location module_function def get(&callback) @callback = callback @location_manager =

    CLLocationManager.alloc.init @location_manager.delegate = self @location_manager.startUpdatingLocation end def locationManager(manager, didUpdateToLocation:new, fromLocation:old) @callback.call to: newLocation, from: oldLocation end end
  2. AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success: ^(NSURLRequest *request, NSHTTPURLResponse *response,

    id JSON) { NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]); } failure: ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON ) { NSLog(@"Error: %@", [error localizedDescription]); }]; AFNetworking
  3. operation = AFJSONRequestOperation.JSONRequestOperationWithRequest(request, success: lambda { |request, response, json| p

    "IP Address: #{json.valueForKeyPath('origin')}" }, failure: lambda { |request, response, error, json| { p "Error: #{error.localizedDescription}" })
  4. operation = AFJSONRequestOperation.JSONRequestOperationWithRequest(request, success: -> (request, response, json) { p

    "IP Address: #{json.valueForKeyPath('origin')}" }, failure: -> (request, response, error, json) { p "Error: #{error.localizedDescription}" })
  5. module AFMotion module JSON def self.for_request(request, &callback) operation = AFJSONRequestOperation.JSONRequestOperationWithRequest(request,

    success: -> (request, response, json) { result = AFMotion::HTTPResult.new(operation, json, nil) callback.call(result) }, failure: -> (request, response, error, json) { result = AFMotion::HTTPResult.new(operation, json, error) callback.call(result) } ) end end end
  6. [AFJSONRequestOperation JSONRequestOperationWithRequest:request success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

    NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]); } failure: ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON ) { NSLog(@"Error: %@", [error localizedDescription]); }];
  7. picker = UIImagePickerController.alloc.init picker.sourceType = UIImagePickerControllerSourceTypeCamera view = UIView.alloc.initWithFrame(frame) view.autoresizingMask

    = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingPleaseNoMoreNotAgain
  8. class UIView def autoresizing_mask=(mask) self.autoresizingMask = Constants.get("UIViewAutoresizing", mask) end end

    picker.source_type = :camera view.autoresizing_mask = [:flexible_width, :flexible_height]
  9. module Constants module_function def get(base, value) case value when Integer

    value when NSArray value.reduce { |i, j| const_int_get(base, i) | const_int_get(base, j) } else value = value.to_s.camelize Kernel.const_get("#{base}#{value}") end end end github.com/clayallsopp/inspect-2013
  10. • Need to hard-code all possible constants as a method

    (PUDIBT class AppDelegate private def constants_hack [UIViewAutoresizingFlexibleHeight, UIViewAutoresizingFlexibleWidth, UIViewAutoresizingFlexibleTopMargin, etc] end end
  11. • camelCase snake_case • NSNames Module::Names • isEnabled enabled? •

    setTitle #title= • .alloc.init .new • (Under the hood, still use designated initializer i.e. initWithFrame:, initWithNibNamed:bundle:)
  12. []= HttpClient.headers["X-Authentication"] = "123123" class HttpClient class HeaderDSL def initialize(http_client)

    @http_client = http_client end def []=(header, value) @http_client.setDefaultHeader(header, value: value) end end def headers @header_dsl ||= HeaderDSL.new(self) end end
  13. class NSOperationQueue class << self alias_method :current, :currentQueue end end

    OperationQueue = NSOperationQueue OperationQueue.current == NSOperationQueue.currentQueue