Permalink
Browse files

init drop...

  • Loading branch information...
1 parent 4b8fea5 commit 0d773ae5b7514367fd8292b62b06ef505ac54416 @derailed committed May 23, 2012
View
@@ -0,0 +1,5 @@
+.repl_history
+build
+resources/*.nib
+resources/*.momd
+resources/*.storyboardc
View
@@ -0,0 +1,4 @@
+DataMart
+==========
+
+Sample CoreData modeling
View
@@ -0,0 +1,9 @@
+$:.unshift("/Library/RubyMotion/lib")
+require 'motion/project'
+require 'bubble-wrap' # Thanks Matt!
+
+Motion::Project::App.setup do |app|
+ app.name = 'DataMart'
+ app.frameworks += ['CoreData']
+ app.icons = ['db.png']
+end
@@ -0,0 +1,15 @@
+class AppDelegate
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
+ @window = UIWindow.alloc.initWithFrame( UIScreen.mainScreen.bounds )
+
+ @window.rootViewController = UINavigationController
+ .alloc
+ .initWithRootViewController( ContactsController.alloc.init )
+
+ @window.rootViewController.wantsFullScreenLayout = true
+ @window.makeKeyAndVisible
+
+ puts "Path: #{Kernel.documents_path}"
+ true
+ end
+end
@@ -0,0 +1,55 @@
+class ContactsController < UITableViewController
+
+ def cell_id() "contacts"; end
+
+ def viewDidLoad
+ view.dataSource = view.delegate = self
+ end
+
+ def viewWillAppear(animated)
+ navigationItem.title = 'Contacts'
+ navigationItem.leftBarButtonItem = editButtonItem
+ navigationItem.rightBarButtonItem = UIBarButtonItem.alloc
+ .initWithBarButtonSystemItem( UIBarButtonSystemItemAdd,
+ target:self, action:'addContact')
+ navigationItem.rightBarButtonItem.enabled = true
+ end
+
+ def addContact
+ ContactStore.shared.add_contact do |contact|
+ contact.name = "Fernand"
+ contact.age = 21
+ contact.created_at = NSDate.date
+ contact.updated_at = NSDate.date
+ end
+ view.reloadData
+ end
+
+ def tableView(tableView, numberOfRowsInSection:section)
+ ContactStore.shared.contacts.size
+ end
+
+ def tableView(tableView, cellForRowAtIndexPath:indexPath)
+ cell = tableView.dequeueReusableCellWithIdentifier(cell_id) ||
+ UITableViewCell.alloc.initWithStyle(UITableViewCellStyleSubtitle, reuseIdentifier:cell_id)
+
+ contact = ContactStore.shared.contacts[indexPath.row]
+ @date_formatter ||= NSDateFormatter.alloc.init.tap do |df|
+ df.timeStyle = NSDateFormatterMediumStyle
+ df.dateStyle = NSDateFormatterMediumStyle
+ end
+ cell.textLabel.text = @date_formatter.stringFromDate(contact.created_at)
+ cell.detailTextLabel.text = "%10s:%d" % [contact.name, contact.age]
+ cell
+ end
+
+ def tableView(tableView, editingStyleForRowAtIndexPath:indexPath)
+ UITableViewCellEditingStyleDelete
+ end
+
+ def tableView(tableView, commitEditingStyle:editingStyle, forRowAtIndexPath:indexPath)
+ contact = ContactStore.shared.contacts[indexPath.row]
+ ContactStore.shared.remove_contact(contact)
+ tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation:UITableViewRowAnimationFade)
+ end
+end
@@ -0,0 +1,22 @@
+class Contact < NSManagedObject
+ def self.entity
+ @entity ||= begin
+ entity = NSEntityDescription.alloc.init
+ entity.name = 'Contact'
+ entity.managedObjectClassName = 'Contact'
+
+ entity.properties = {
+ name: NSStringAttributeType,
+ age: NSInteger16AttributeType,
+ created_at: NSDateAttributeType,
+ updated_at: NSDateAttributeType}.each_pair.map do |name, type|
+ property = NSAttributeDescription.alloc.init
+ property.name = name.to_s
+ property.attributeType = type
+ property.optional = false
+ property
+ end
+ entity
+ end
+ end
+end
@@ -0,0 +1,58 @@
+class ContactStore
+
+ def self.shared
+ @shared ||= ContactStore.new
+ end
+
+ def store() "Contact"; end
+
+ def contacts
+ @contacts ||= begin
+ request = NSFetchRequest.alloc.init
+ request.entity = NSEntityDescription.entityForName(store, inManagedObjectContext:@context)
+ request.sortDescriptors = [ NSSortDescriptor.alloc.initWithKey('created_at', ascending:false) ]
+
+ error_ptr = Pointer.new(:object)
+ data = @context.executeFetchRequest(request, error:error_ptr)
+ if data == nil
+ raise "Error when fetching data: #{error_ptr[0].description}"
+ end
+ data
+ end
+ end
+
+ def add_contact
+ yield NSEntityDescription.insertNewObjectForEntityForName(store, inManagedObjectContext:@context)
+ save
+ end
+
+ def remove_contact( contact )
+ @context.deleteObject( contact )
+ save
+ end
+
+ private
+
+ def initialize
+ model = NSManagedObjectModel.alloc.init
+ model.entities = [Contact.entity]
+
+ store = NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel(model)
+ store_url = NSURL.fileURLWithPath(File.join(NSHomeDirectory(), 'Documents', 'Contacts.sqlite'))
+ error_ptr = Pointer.new(:object)
+ unless store.addPersistentStoreWithType(NSSQLiteStoreType, configuration:nil, URL:store_url, options:nil, error:error_ptr)
+ raise "Can't add persistent SQLite store: #{error_ptr[0].description}"
+ end
+
+ @context = NSManagedObjectContext.alloc.init
+ @context.persistentStoreCoordinator = store
+ end
+
+ def save
+ error_ptr = Pointer.new(:object)
+ unless @context.save(error_ptr)
+ raise "Error when saving the model: #{error_ptr[0].description}"
+ end
+ @contacts = nil
+ end
+end
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,9 @@
+describe "Application 'plotty'" do
+ before do
+ @app = UIApplication.sharedApplication
+ end
+
+ it "has one window" do
+ @app.windows.size.should == 1
+ end
+end

0 comments on commit 0d773ae

Please sign in to comment.