Giter Club home page Giter Club logo

ruby-xcdm's Introduction

ruby-xcdm

This is a tool for generating the same xcdatamodeld files that Xcode does when designing a datamodel for Core Data. It is written in pure ruby, but it will be of particular interest to RubyMotion developers. It offers the essential features that Xcode does, plus a text-based workflow and some niceties, like automatic inverse relationships.

Dependency Status Build Status Gem Version

ruby-xcdm is maintained by Infinite Red, a web and mobile development company based in Portland, OR and San Francisco, CA.

Installation

Add this line to your application's Gemfile:

  gem 'ruby-xcdm'

And then execute:

  $ bundle

Or install it yourself as:

  $ gem install ruby-xcdm

Usage (RubyMotion)

  1. Make a directory called "schemas" inside your RubyMotion project
  2. Create one schema version per file within the directory
  3. To build the schema, run rake schema:build

If you want to build the schema every time you run the simulator, add this to your Rakefile:

task :"build:simulator" => :"schema:build"

You can override the name of the datamodel file, if you need to, using a config variable:

  app.xcdm.name = "custom"

Usage (Plain Ruby)

  1. Make a directory to hold your schemas (a.k.a. data model in XCode parlance)
  2. Create one schema version per file within the directory
  3. Run the command to generate a datamodel:
  xcdm MyApplicationName ./schema ./resources

Schema File Format

Here's a sample schema file:

  schema "001" do

    entity "Article" do

      string    :body,        optional: false
      integer32 :length
      boolean   :published,   default: false
      datetime  :publishedAt
      string    :title,       optional: false

      belongs_to :author
    end

    entity "Author" do
      float :fee
      string :name, optional: false
      has_many :articles 
    end

  end

All the built-in data types are supported:

  • integer16
  • integer32
  • integer64
  • decimal (See note below)
  • double
  • float
  • string
  • boolean
  • datetime
  • binary
  • transformable

NSDecimal is not well-supported in RubyMotion as of this writing. They are converted to floats and lose precision. HipByte is aware of the issue and intends to fix it, but until they do, you will need to use something else for storing currency. For an example, see here.

Inverse relationships are generated automatically. If the inverse relationship cannot be derived from the association name, you can use the :inverse option:

  entity "Game" do
    belongs_to :away_team, inverse: "Team.away_games"
    belongs_to :home_team, inverse: "Team.home_games"
  end

  entity "Team" do
    has_many :away_games, inverse: "Game.away_team"
    has_many :home_games, inverse: "Game.home_team"
  end

Many-to-many relationships are supported via the :plural_inverse option:

  entity "Person" do
    has_many :threads, plural_inverse: true
  end

  entity "Thread" do
    has_many :people, plural_inverse: true
  end

In this mode, Core Data will automatically create a relation table behind the scenes. If you want more control, you can make the intermediate table yourself:

  entity "Person" do
    has_many :postings
  end

  entity "Thread" do
    has_many :postings
  end

  entity "Posting" do
    belongs_to :person
    belongs_to :thread

    datetime :joined_at
  end

You can also have symmetric one-to-one relationships via has_one:

  entity "Person" do
    has_one :ego
  end

  entity "Ego" do
    has_one :person
  end

Deletion rules can be easily set on relationships and the default rule is "Nullify":

  entity "Discussion" do
    has_many :messages, deletionRule: "Cascade"
  end

  entity "Message" do
    belongs_to :discusion
  end

  # Example:
  # Discussion.first.messages.count => 10
  # Messages.count => 10
  # Discussion.first.destroy
  # cdq.save
  # Messages.count => 0

Core Data has no equivalent of :through in ActiveRecord, so you'll need to handle that relation yourself.

If you need to set some of the more esoteric options on properties or relationships, you can include the raw parameters from NSEntityDescription and NSAttributeDescription, like renamingIdentifier or defaultValueString.

Additionally, if you need to set some userInfo properties, you can do so by adding a user_info_entry to an entity:

  entity "Person" do
    user_info_entry 'my_key', 'my_value'
  end

Versioning

To create new versions, simply copy the old version, increase the version string (the last one in sort order is always interpreted to be the current version) and make your changes. So long as they conform to the automatic versioning rules, everything should work seamlessly.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

ruby-xcdm's People

Contributors

atastor avatar brianpattison avatar kemiller avatar kevinvangelder avatar markrickert avatar markvillacampa avatar pietbrauer avatar ruanwz avatar twe4ked avatar willianvdv avatar wndxlori avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ruby-xcdm's Issues

Does not appear to support parent entities?

I have an existing CD model with parent and child entities. ex. snippet from the xml file:

<entity name="EmailAccount" 
    representedClassName="EmailAccount" 
    parentEntity="ServiceAccount" 
    syncable="YES">

Looking at entity.rb it appears that parent/child is not supported. Is this the case or did I miss something?

thanks.

README command-line example

I think the examples should be copy-pastable and use the same values as other suggestions in the README. For instance, the README states to ‘make a directory called "schemas"’ and the default place for assets is the resources dir. Therefore I would suggest a version like so:

$ xcdm MyApplicationName ./schemas ./resources

A second issue is that the command seems to require 4 arguments, of which I believe the last one is the ‘Xcode application version’.

/Library/Ruby/Gems/2.0.0/gems/ruby-xcdm-0.0.7/lib/xcdm/schema.rb:87:in `initialize': wrong number of arguments (3 for 4) (ArgumentError)
    from /Library/Ruby/Gems/2.0.0/gems/ruby-xcdm-0.0.7/bin/xcdm:6:in `new'
    from /Library/Ruby/Gems/2.0.0/gems/ruby-xcdm-0.0.7/bin/xcdm:6:in `<top (required)>'
    from /usr/bin/xcdm:23:in `load'
    from /usr/bin/xcdm:23:in `<main>'

This is what I ended up using:

$ bundle exec xcdm MyApplicationName ./schemas ./resources 5.1

Whitespace issues

I've been having some problems using CDQ when the name (App.config.name) has a whitespace, and traced it back here.

It appears that CDQ can't find the generated model when the .xcdatamodeld is created with a name containing whitespaces. Instead of trying to patch CDQ, what to you think about patching ruby-xcdm and make it remove whitespaces from the app name before creating a file?

I thought on something as simple as using .delete(" ") on the proper places in lib/ruby-xcdm.rb

RubyMotion specify multiple resource directories

Hi

I'm working on a project that uses CDQ inside a Framework that is then used within both an App and an Extension. As far as I can tell, both the App and the Extension need the .xcdatamodel file within their own resources directory which is fine. Currently to make this work I have to have a folder layout as follows:

MainApp            gem ruby-xcdm
  Extension        gem ruby-xcdm
    resources
    schemas
  Framework        gem cdq
    app
  resources
  schemas

This works but it needs the two schemas folders to be maintained identically and runs the rake task twice to build the .xcdatamodel file in both resource folders for the MainApp and the Extension

It would be great if I could change this to the following:

App
  Extension
    resources
  Framework
    app
    schemas
  resources

If xcdm had a way of specifying in the Rakefile, the location of the resources folders to be used, I think this would work perfectly. Something like app.xcdm.targets = ['../resources', '../Extension/resources'] would be perfect.

Custom Migrations

Is there anything in the pipeline to allow Custom Migrations? Or is that unrealistic?

Specifically for creating a Mapping Model.

I came across an article on objc.io that discusses the concept of a progressive migration. In a nutshell the idea is to avoid having to create a Mapping Model for every schema version, which seems like a neat approach.

I also came across another project on Github called CoreDataInCode that has actually implemented a Mapping Model and Migration Policy in Objective-C.

Specifying class names on relationships

I don't know CoreData well enough yet to know exactly what's possible and what isn't... But after playing with ruby-xcdm, I feel like I should be able to do this:

entity "Game" do
  datetime :game_start

  belongs_to :away_team, class_name: "Team"
  belongs_to :home_team, class_name: "Team"
end

entity "Team" do
  string :name, optional: false

  has_many :away_games, class_name: "Game"
  has_many :home_games, class_name: "Game"
end

I know class_name: doesn't work, but I'm curious about possibly adding it or at least adding an example in the Readme for how to accomplish this. I'm more than happy to submit a Pull Request, but I'm afraid I don't know what I'm doing yet!

Let me know what you think and how I can help. Thanks!

Time for a new gem version

Looks like there's an important bugfix that has been merged into the master branch. I'm not sure who has access to release new gem versions, but it would be nice to release a new version of the gem. New Red Potion projects currently fail to compile if you have Xcode 10 installed.

xcdm classify-s "regimen" to "Regiman"

I tried adding an inflection rule but it's not working.

# config/initializers/inflections.rb
require 'active_support/all'

ActiveSupport::Inflector.inflections do |inflect|
  inflect.plural /^(regimen)$/, '\1s'
  inflect.singular /^(regimen)s$/, '\1'
end
# Rakefile
# -*- coding: utf-8 -*-
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project/template/ios'

begin
  require 'bundler'
  Bundler.require
rescue LoadError
end

require './config/initializers/inflections'

Motion::Project::App.setup do |app|
  # ...
end

Binary data field must have a defined type error

I'm trying to create a model with a binary field:

schema "0001 initial" do
  entity "Image" do
    binary :image, optional: false
  end
end

When compiling, this will result in:

..../0001 initial.xcdatamodel:Image.image: error: Image.image must have a defined type [2]

I think this has to do with the usage of Binary data as type instead of Binary. The generated data model:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model name="" userDefinedModelVersionIdentifier="0001 initial" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="2061" systemVersion="12D78" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
  <entity name="Image" syncable="YES" representedClassName="Image">
    <attribute optional="NO" syncable="YES" attributeType="Binary Data" name="image"/>
  </entity>
</model>

When I try to create the same entity with XCode I get:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="3401" systemVersion="13B42" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
    <entity name="Entity" syncable="YES">
        <attribute name="attribute" optional="YES" attributeType="Binary" syncable="YES"/>
    </entity>
</model>

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.