DbMotion 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/DbMotion.gemspec +21 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +20 -0
- data/LICENSE +22 -0
- data/README.md +34 -0
- data/lib/DbMotion.rb +10 -0
- data/lib/DbMotion/database.rb +85 -0
- data/lib/DbMotion/version.rb +3 -0
- metadata +54 -0
data/.gitignore
ADDED
data/DbMotion.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/DbMotion/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Stephan Toggweiler"]
|
6
|
+
gem.email = ["stephan@rheoli.net"]
|
7
|
+
gem.description = "DbMotion a simple adaption of CoreData."
|
8
|
+
gem.summary = "
|
9
|
+
DbMotion a simple adaption of CoreData.
|
10
|
+
"
|
11
|
+
gem.homepage = "https://github.com/rheoli/DbMotion"
|
12
|
+
|
13
|
+
gem.files = `git ls-files`.split($\)
|
14
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
16
|
+
gem.name = "DbMotion"
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
gem.version = DbMotion::VERSION
|
19
|
+
#gem.add_runtime_dependency("formotion", "~> 1.1.4")
|
20
|
+
end
|
21
|
+
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ProMotion-formotion (0.0.1)
|
5
|
+
ProMotion (~> 0.4.1)
|
6
|
+
formotion (~> 1.1.4)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
ProMotion (0.4.1)
|
12
|
+
bubble-wrap (1.1.5)
|
13
|
+
formotion (1.1.5)
|
14
|
+
bubble-wrap (>= 1.1.4)
|
15
|
+
|
16
|
+
PLATFORMS
|
17
|
+
ruby
|
18
|
+
|
19
|
+
DEPENDENCIES
|
20
|
+
ProMotion-formotion!
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Stephan Toggweiler
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# DbMotion
|
2
|
+
|
3
|
+
Simple CoreData Adapter for RubyMotion
|
4
|
+
|
5
|
+
## Getting Started
|
6
|
+
|
7
|
+
### Create a MOM CoreData Model
|
8
|
+
Create a XCode "Empty Project" add there a CoreData Model.
|
9
|
+
|
10
|
+
|
11
|
+
### Adding DbMotion to your RubyMotion project
|
12
|
+
|
13
|
+
in Gemfile
|
14
|
+
`gem 'DbMotion'`
|
15
|
+
then run `bundle update`
|
16
|
+
|
17
|
+
in AppDelegate add the following to open the specified Database:
|
18
|
+
```ruby
|
19
|
+
DbMotion::Database.new.open("MyCoreDataModel_%s.mom", "MyCoreData.sqlite")
|
20
|
+
```
|
21
|
+
|
22
|
+
Search for Items in the Database:
|
23
|
+
```ruby
|
24
|
+
items=DbMotion::Database.run.find_entry("Item")
|
25
|
+
```
|
26
|
+
|
27
|
+
Save something in the Database:
|
28
|
+
```ruby
|
29
|
+
item=DbMotion::Database.run.create_entry("Item")
|
30
|
+
item.name=form[:name]
|
31
|
+
if DbMotion::Database.run.save
|
32
|
+
# item saved
|
33
|
+
end
|
34
|
+
```
|
data/lib/DbMotion.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
unless defined?(Motion::Project::Config)
|
2
|
+
raise "This file must be required within a RubyMotion project Rakefile."
|
3
|
+
end
|
4
|
+
|
5
|
+
require "DbMotion/version"
|
6
|
+
|
7
|
+
Motion::Project::App.setup do |app|
|
8
|
+
app.files = Dir.glob(File.join(File.dirname(__FILE__), 'DbMotion/**/*.rb')) | app.files
|
9
|
+
app.frameworks += ['CoreData']
|
10
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module DbMotion
|
2
|
+
class Database
|
3
|
+
|
4
|
+
def self.run
|
5
|
+
@@db
|
6
|
+
end
|
7
|
+
|
8
|
+
def create_entry(_name)
|
9
|
+
NSEntityDescription.insertNewObjectForEntityForName(_name, inManagedObjectContext:@context)
|
10
|
+
end
|
11
|
+
|
12
|
+
def find_entry(_name)
|
13
|
+
request = NSFetchRequest.alloc.init
|
14
|
+
request.entity = NSEntityDescription.entityForName(_name, inManagedObjectContext:@context)
|
15
|
+
#request.sortDescriptors = [NSSortDescriptor.alloc.initWithKey('creation_date', ascending:false)]
|
16
|
+
error_ptr = Pointer.new(:object)
|
17
|
+
@context.executeFetchRequest(request, error:error_ptr)
|
18
|
+
end
|
19
|
+
|
20
|
+
def save
|
21
|
+
error_ptr = Pointer.new(:object)
|
22
|
+
@context.save(error_ptr)
|
23
|
+
end
|
24
|
+
|
25
|
+
def open(_mom_file, _db_file)
|
26
|
+
@mom_file=_mom_file % "cur"
|
27
|
+
@mom_url = NSURL.fileURLWithPath("#{NSBundle.mainBundle.resourcePath}/#{@mom_file}")
|
28
|
+
@model=NSManagedObjectModel.alloc.initWithContentsOfURL(@mom_url)
|
29
|
+
@store = NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel(@model)
|
30
|
+
@store_file = File.join(NSHomeDirectory(), 'Documents', _db_file)
|
31
|
+
@store_url = NSURL.fileURLWithPath(@store_file)
|
32
|
+
unless compatible?
|
33
|
+
migrate(_mom_file)
|
34
|
+
end
|
35
|
+
@context = NSManagedObjectContext.alloc.init
|
36
|
+
@context.persistentStoreCoordinator = @store
|
37
|
+
error_ptr = Pointer.new(:object)
|
38
|
+
unless @store.addPersistentStoreWithType(NSSQLiteStoreType, configuration:nil, URL:@store_url, options:nil, error:error_ptr)
|
39
|
+
raise "Can't add persistent SQLite store: #{error_ptr[0].description}"
|
40
|
+
end
|
41
|
+
@@db=self
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
def compatible?
|
46
|
+
error_ptr = Pointer.new(:object)
|
47
|
+
metadata=NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(NSSQLiteStoreType, URL:@store_url, error:error_ptr)
|
48
|
+
compatible=true
|
49
|
+
if metadata
|
50
|
+
compatible=@model.isConfiguration(nil, compatibleWithStoreMetadata:metadata)
|
51
|
+
end
|
52
|
+
compatible
|
53
|
+
end
|
54
|
+
|
55
|
+
def migrate(_old_mom_files)
|
56
|
+
old_mom_files=_old_mom_files % '%03d'
|
57
|
+
error_ptr = Pointer.new(:object)
|
58
|
+
metadata=NSPersistentStoreCoordinator.metadataForPersistentStoreOfType(NSSQLiteStoreType, URL:@store_url, error:error_ptr)
|
59
|
+
return false unless metadata
|
60
|
+
0.upto(999) do |version|
|
61
|
+
old_mom_file=old_mom_files % version
|
62
|
+
p old_mom_file
|
63
|
+
old_mom_url = NSURL.fileURLWithPath("#{NSBundle.mainBundle.resourcePath}/#{old_mom_file}")
|
64
|
+
p old_mom_url
|
65
|
+
old_model=NSManagedObjectModel.alloc.initWithContentsOfURL(old_mom_url)
|
66
|
+
compatible=old_model.isConfiguration(nil, compatibleWithStoreMetadata:metadata)
|
67
|
+
next unless compatible
|
68
|
+
p "Found version #{version}"
|
69
|
+
migmgr=NSMigrationManager.alloc.initWithSourceModel(old_model, destinationModel:@model)
|
70
|
+
mapping_model = NSMappingModel.inferredMappingModelForSourceModel(old_model, destinationModel:@model, error:error_ptr)
|
71
|
+
new_store_url = NSURL.fileURLWithPath(@store_url.path+".new")
|
72
|
+
p migmgr.migrateStoreFromURL(@store_url, type:NSSQLiteStoreType, options:nil,
|
73
|
+
withMappingModel:mapping_model, toDestinationURL:new_store_url,
|
74
|
+
destinationType:NSSQLiteStoreType, destinationOptions:nil,
|
75
|
+
error:error_ptr)
|
76
|
+
NSFileManager.defaultManager.removeItemAtPath(@store_url.path, error:error_ptr)
|
77
|
+
#-TODO: evtl. move statt loeschen
|
78
|
+
NSFileManager.defaultManager.moveItemAtPath(new_store_url.path, toPath:@store_url.path, error:error_ptr)
|
79
|
+
break
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: DbMotion
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Stephan Toggweiler
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-06 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: DbMotion a simple adaption of CoreData.
|
15
|
+
email:
|
16
|
+
- stephan@rheoli.net
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- DbMotion.gemspec
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- lib/DbMotion.rb
|
28
|
+
- lib/DbMotion/database.rb
|
29
|
+
- lib/DbMotion/version.rb
|
30
|
+
homepage: https://github.com/rheoli/DbMotion
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.23
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: DbMotion a simple adaption of CoreData.
|
54
|
+
test_files: []
|