Skip to content

Instantly share code, notes, and snippets.

@clarkware
Created September 27, 2010 15:22
Show Gist options
  • Save clarkware/599206 to your computer and use it in GitHub Desktop.
Save clarkware/599206 to your computer and use it in GitHub Desktop.
#
# Quick hack to map an iOS app's Core Data managed SQLite database into ActiveRecord
#
class CoreDataBase < ActiveRecord::Base
self.abstract_class = true
set_primary_key "Z_PK"
# Map all ZXXX attributes to XXX
def method_missing(method, *args)
column_name = "Z#{method.to_s.upcase}"
respond_to?(column_name) ? send(column_name, *args) : super
end
end
class Event < CoreDataBase
set_table_name "ZEVENT"
belongs_to :track, :foreign_key => "ZTRACK"
has_many :sessions, :foreign_key => "ZEVENT"
end
class Track < CoreDataBase
set_table_name "ZTRACK"
has_many :events, :foreign_key => "ZTRACK"
end
class Session < CoreDataBase
set_table_name "ZSESSION"
belongs_to :event, :foreign_key => "ZEVENT"
has_many :laps, :foreign_key => "ZSESSION"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment