Code Monkey home page Code Monkey logo

core-data-in-motion's Introduction

Core Data in Motion

A light-weight wrapper for Core Data that tries its hardest to be like ActiveRecord.

Features

Defining models

class Device < CDIM::Model
  # options can be :required => true/false (defaults to false) and/or :default => ...
  property :name, :string, :required => true
  property :ios_version, :float, :default => 6.0, :required => true
  property :udid, :string

  # an enum takes an additional option, an array of possible values
  # it can take a default as well, :default => :mac
  property :type, :enum, :values => [:iphone, :ipad, :mac], :required => true # transparently stored as an :int16
  
  # adds created_at & updated_at properties
  timestamp_properties
end

Creating records

iphone = Device.create(:name => 'iPhone', :type => :iphone, :udid => '...')
# or
ipad = Device.new
ipad.name = 'iPad'
ipad.type = :ipad
ipad.ios_version = 6.1
ipad.save

Updating records

iphone.update_attributes(:ios_version => 6.1)
# or
ipad.name = "Brendan's" + ipad.name
ipad.save

Finding records

devices = Device.all
# since Core Data has no concept of an id, first/last will try to use created_at to order items
# if that's not available, it will just return the first or last item from a fetch request, which has no guarentee of order
# (so specify a column or use timestamp_properties)
first_device = Device.first
# you can also specify your own column to order by for first or last
newest_ios = Device.last(:ios_version)

Destroying records

ipad.destroy

Has-one relationship

class Device
  has_one :owner
end

class Owner
  belongs_to :device
end

device = Device.create(:owner => Owner.new(:name => 'test'))
device.owner.name # 'test'

# assignment saves the object being assigned if the parent isn't a new record, but it doesn't save the association between the two
device.owner = nil
device.owner = Owner.new
Device.all.first.owner # still the old object

# create owner saves the association
device.create_owner(:name => 'new owner')

# build saves the parent object to have a nil relation
device.build_owner(:name => 'unsaved')
Device.all.first.owner # nil
device.save
Device.all.first.owner.name # 'unsaved'

Has-many relationship

class Manager
  has_many :employees
end
  
class Employee
  belongs_to :manager
end

manager = Manager.create
manager.employees.create # saves
manager.employees.build(:name => '..') # doesn't save
# saves the employee and relation right away unless manager.new_record?
# aliases: <<, :concat, can take an array as well
manager.employees.push(Employee.create)
manager.employees.clear # doesn't delete employees
first = manager.employees.first
manager.employees.destroy(first) # or delete
manager.employees.destroy_all # or delete_all

manager.employees << Device.create # raises an exception

Belongs-to relationship

Exactly the same as a has_one, but has_one/has_many relationships won't function without the presence of an inverse belongs_to relationship.

Data Types

  • :int16/:integer16
  • :int32/:integer32
  • :int64/:integer64
  • :double
  • :float
  • :string
  • :bool/:boolean
  • :binary
  • :enum (pass in an array of values - this is not built in to Core Data)

Installation

Create a new RubyMotion project.

motion create myapp

Open it in your favorite editor, then go into your Rakefile and modify the top to look like the following:

# -*- coding: utf-8 -*-
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project/template/ios'

begin
  require 'bundler'
  Bundler.require
rescue LoadError
end

require 'motion-support/concern'
require 'motion-support/core_ext/hash'
require 'motion-support/inflector'

Create a Gemfile and add the following lines:

source 'https://rubygems.org'
gem 'rake'
gem 'core-data-in-motion', :git => 'git://github.com/brendanjcaffrey/core-data-in-motion.git'
gem 'motion-support', :require => false

Run bundle install in Terminal to install Core Data In Motion.

To-Do

  • Model.create
  • Model.update_attributes
  • Model.all
  • Model.save
  • Model.destroy/delete
  • one-to-one relationships (has one, belongs to)
  • one-to-many relationships (has_many)
  • DSL for filtering and sorting (Model.where(...).limit(1), etc)
  • apply DSL to has_many collections (model.children.where(..))
  • schema migrations

Thanks to:

core-data-in-motion's People

Contributors

brendanjcaffrey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

core-data-in-motion's Issues

Uninitialized constant CDIM::Entity::MotionSupport (Name Error)

I'm building a simple app with RubyMotion. I modified the Rakefile and created the Gemfile as indicated.

I created a single class:

class Romanum < CDIM::Model
    property :m, :string, :default => ""
    property :d, :string, :default => ""
    property :c, :string, :default => ""
    property :l, :string, :default => ""
    property :x, :string, :default => ""
    property :v, :string, :default => ""
    property :i, :string, :default => ""
    property :roman, :string, :default => ""
    property :arabic, :integer16, :default => 0
end

Here's what I see when I try to compile:

mbp:romancalc Sperberx$ rake
     Build ./build/iPhoneSimulator-6.1-Development
   Compile ./app/controllers/roman_num_controller.rb
      Link ./build/iPhoneSimulator-6.1-Development/romancalc.app/romancalc
    Create ./build/iPhoneSimulator-6.1-Development/romancalc.dSYM
  Simulate ./build/iPhoneSimulator-6.1-Development/romancalc.app
2013-09-10 18:08:54.660 romancalc[25303:14003] uninitialized constant CDIM::Entity::MotionSupport (NameError)
2013-09-10 18:08:54.661 romancalc[25303:14003] *** Terminating app due to uncaught exception 'NameError', reason: 'uninitialized constant CDIM::Entity::MotionSupport (NameError)
'
*** First throw call stack:
(0x2167012 0x80fe7e 0x3ba14a 0x3ba1ff 0x2e89e1 0x2e5456 0x3755eb 0x3afc6f 0x396b93 0x37636a 0x3763a0 0x3b303a 0xd5c05 0xd6039 0xd5f8e 0xd5eac 0xd7fac 0x31c1 0x39ad 0x2d15)
libc++abi.dylib: terminate called throwing an exception
(main)> *** Simulator session ended with error: Error Domain=DTiPhoneSimulatorErrorDomain Code=1 "The simulated application quit." UserInfo=0x10050a920 {NSLocalizedDescription=The simulated application quit., DTiPhoneSimulatorUnderlyingErrorCodeKey=-1}

Thanks,

Roger Sperberg

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.