nano-store 0.4.3 → 0.5.0

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 CHANGED
@@ -1,6 +1,7 @@
1
1
  Gemfile.lock
2
2
  .rake_tasks~
3
3
  vendor/*/build-*
4
+ vendor/*/.build
4
5
  vendor/Pods/Pods.xcodeproj/project.pbxproj
5
6
  pkg/*
6
7
  .repl_history
data/CHANGELOG ADDED
@@ -0,0 +1,40 @@
1
+ # [0.4.3](https://github.com/siuying/NanoStoreInMotion/compare/v0.4.2%E2%80%A6v0.4.3)
2
+
3
+ - Fix bug saving bag to store
4
+
5
+ # [0.4.2](https://github.com/siuying/NanoStoreInMotion/compare/v0.4.1%E2%80%A6v0.4.2)
6
+
7
+ - Supports hashes with string keys in model initializer
8
+
9
+ # [0.4.1](https://github.com/siuying/NanoStoreInMotion/compare/v0.4.0%E2%80%A6v0.4.1)
10
+
11
+ - Fix bug when nil is inserted in field
12
+
13
+ # [0.4.0](https://github.com/siuying/NanoStoreInMotion/compare/v0.3.14%E2%80%A6v0.4.0)
14
+
15
+ - Use define_method in model instead of method_missing hacks (as rubymotion support define_method now)
16
+
17
+ # [0.3.13](https://github.com/siuying/NanoStoreInMotion/compare/v0.3.12%E2%80%A6v0.3.13)
18
+
19
+ - Add find_by_key
20
+ - Fix NameError when there's exception in store extension
21
+ - Update motion-cocoapods and bubble-wrap
22
+
23
+ # [0.3.12](https://github.com/siuying/NanoStoreInMotion/compare/v0.3.11%E2%80%A6v0.3.12)
24
+
25
+ - Update NanoStore pods to 2.1.4
26
+
27
+ # [0.3.0 - 0.3.11](https://github.com/siuying/NanoStoreInMotion/compare/v0.3.0%E2%80%A6v0.3.11)
28
+
29
+ - Fix crash on find_keys
30
+ - Fix finder
31
+ - Updated to NanoStore 2.1.3
32
+ - Add bulk delete
33
+ - Fix bug that #find and #all returning objects of any classes
34
+ - Add support array as parameter of finder (OR)
35
+ - Add sort to all finder
36
+ - Improved finder: now support specify multiple criteria with Array and Hash, as well as using hash for equality
37
+
38
+ # 0.2.3
39
+
40
+ - Initial gem release
data/README.md CHANGED
@@ -170,6 +170,31 @@ bag.save
170
170
  bags = store.bags
171
171
  ```
172
172
 
173
+ ### Association
174
+
175
+ Use ```bag``` to declare a Bag that associated with a Model.
176
+
177
+ ````ruby
178
+
179
+ class User < NanoStore::Model
180
+ attribute :name
181
+ attribute :age
182
+ attribute :created_at
183
+ bag :cars
184
+ end
185
+
186
+ class Car < NanoStore::Model
187
+ attribute :name
188
+ attribute :age
189
+ end
190
+
191
+ user = User.new(:name => "Peter", :age => 20, :created_at => Time.now)
192
+ user.cars << Car.new(:name => "Mini", :age => 0)
193
+ user.save
194
+
195
+ user.cars # => #<NanoStore::Bag:0x7411410>
196
+ ````
197
+
173
198
  ## Performance Tips
174
199
 
175
200
  NanoStore by defaults saves every object to disk one by one. To speed up inserts and edited objects, increase NSFNanoStore's ```saveInterval``` property.
data/Rakefile CHANGED
@@ -2,7 +2,6 @@ require "bundler/gem_tasks"
2
2
  $:.unshift("/Library/RubyMotion/lib")
3
3
  require 'motion/project'
4
4
  require 'motion-cocoapods'
5
- require 'bubble-wrap'
6
5
  require 'motion-redgreen'
7
6
 
8
7
  Motion::Project::App.setup do |app|
@@ -0,0 +1,51 @@
1
+ module NanoStore
2
+ module AssociationClassMethods
3
+ def bag(name)
4
+ define_method(name) do |*args, &block|
5
+ return _bags[name] if _bags[name]
6
+
7
+ bag_key = self.info[name]
8
+ if bag_key.nil?
9
+ bag = Bag.bag
10
+ self.info[name] = bag.key
11
+ else
12
+ bag = self.class.store.bagsWithKeysInArray([bag_key]).first
13
+ end
14
+
15
+ _bags[name] = bag
16
+
17
+ bag
18
+ end
19
+
20
+ define_method((name + "=").to_sym) do |*args, &block|
21
+ bag = self.send(name)
22
+ case args[0]
23
+ when Bag
24
+ bag.clear
25
+ bag += args[0].saved.values
26
+ when Array
27
+ bag.clear
28
+ bag += args[0]
29
+ else
30
+ raise NanoStoreError, "Unexpected type assigned to bags, must be an Array or NanoStore::Bag, now: #{args[0].class}"
31
+ end
32
+ bag
33
+ end
34
+ end
35
+ end
36
+
37
+ module AssociationInstanceMethods
38
+ def _bags
39
+ @_bags ||= {}
40
+ end
41
+
42
+ def save
43
+ _bags.values.each do |bag|
44
+ bag.store = self.class.store
45
+ bag.save
46
+ end
47
+ super
48
+ end
49
+ end
50
+
51
+ end
@@ -1,9 +1,16 @@
1
1
  module NanoStore
2
- class Bag < NSFNanoBag
3
- ## Accessors
4
- alias_method :saved, :savedObjects
5
- alias_method :unsaved, :unsavedObjects
6
- alias_method :removed, :removedObjects
2
+ module BagInstanceMethods
3
+ def self.included(base)
4
+ base.class_eval do
5
+ alias_method :saved, :savedObjects
6
+ alias_method :unsaved, :unsavedObjects
7
+ alias_method :removed, :removedObjects
8
+ alias_method :size, :count
9
+ alias_method :clear, :removeAllObjects
10
+ alias_method :inflate, :inflateBag
11
+ alias_method :deflate, :deflateBag
12
+ end
13
+ end
7
14
 
8
15
  def originalClassString
9
16
  'NSFNanoBag'
@@ -13,6 +20,10 @@ module NanoStore
13
20
  self.hasUnsavedChanges
14
21
  end
15
22
 
23
+ def to_a
24
+ self.savedObjects.values + self.unsavedObjects.values
25
+ end
26
+
16
27
  ## Adding and Removing Objects
17
28
 
18
29
  # Add an object to bag
@@ -24,9 +35,6 @@ module NanoStore
24
35
  self
25
36
  end
26
37
 
27
- # Clear the bag - remove all objects
28
- alias_method :clear, :removeAllObjects
29
-
30
38
  # Add an object or array of objects to bag
31
39
  # Return the bag
32
40
  def +(object_or_array)
@@ -58,7 +66,7 @@ module NanoStore
58
66
  self
59
67
  end
60
68
 
61
- # Add an object or array of objects to bag
69
+ # Remove an object or array of objects from bag
62
70
  # Return the bag
63
71
  def -(object_or_array)
64
72
  error_ptr = Pointer.new(:id)
@@ -72,8 +80,13 @@ module NanoStore
72
80
  end
73
81
 
74
82
  ## Saving, Reloading and Undoing
75
-
83
+
84
+ def store=(store)
85
+ store.addObject(self, error:nil)
86
+ end
87
+
76
88
  def save
89
+ self.store = NanoStore.shared_store unless self.store
77
90
  error_ptr = Pointer.new(:id)
78
91
  result = self.saveAndReturnError(error_ptr)
79
92
  raise NanoStoreError, error_ptr[0].description if error_ptr[0]
@@ -93,10 +106,11 @@ module NanoStore
93
106
  raise NanoStoreError, error_ptr[0].description if error_ptr[0]
94
107
  result
95
108
  end
96
-
97
- ## Inflating and Deflating
98
-
99
- alias_method :inflate, :inflateBag
100
- alias_method :deflate, :deflateBag
101
109
  end
110
+
111
+ Bag = ::NSFNanoBag
112
+ end
113
+
114
+ class NSFNanoBag
115
+ include NanoStore::BagInstanceMethods
102
116
  end
@@ -39,7 +39,7 @@ module NanoStore
39
39
  end
40
40
 
41
41
  def attribute(name)
42
- @attributes << name
42
+ attributes << name
43
43
 
44
44
  define_method(name) do |*args, &block|
45
45
  self.info[name]
@@ -55,7 +55,7 @@ module NanoStore
55
55
  end
56
56
 
57
57
  def attributes
58
- @attributes
58
+ @attributes ||= []
59
59
  end
60
60
 
61
61
  def store
@@ -77,16 +77,15 @@ module NanoStore
77
77
  keys = find_keys(*args)
78
78
  self.store.delete_keys(keys)
79
79
  end
80
-
81
- def inherited(subclass)
82
- subclass.instance_variable_set(:@attributes, [])
83
- subclass.instance_variable_set(:@store, nil)
84
- end
85
80
  end
86
81
 
87
82
  class Model < NSFNanoObject
88
83
  include ModelInstanceMethods
89
84
  extend ModelClassMethods
90
85
  extend ::NanoStore::FinderMethods
86
+
87
+ include AssociationInstanceMethods
88
+ extend AssociationClassMethods
91
89
  end
90
+
92
91
  end
@@ -1,3 +1,3 @@
1
1
  module NanoStore
2
- VERSION = "0.4.3"
2
+ VERSION = "0.5.0"
3
3
  end
data/nano-store.gemspec CHANGED
@@ -14,7 +14,6 @@ Gem::Specification.new do |gem|
14
14
  gem.require_paths = ["lib"]
15
15
  gem.version = NanoStore::VERSION
16
16
 
17
- gem.add_dependency 'bubble-wrap', '~> 1.1.4'
18
17
  gem.add_dependency 'motion-cocoapods', '>= 1.2.0'
19
18
  gem.add_development_dependency 'motion-redgreen'
20
19
  end
@@ -0,0 +1,59 @@
1
+ describe "Associations" do
2
+ class Todo < NanoStore::Model
3
+ attribute :title
4
+ bag :items
5
+ end
6
+
7
+ class TodoItem < NanoStore::Model
8
+ attribute :completed
9
+ attribute :text
10
+ end
11
+
12
+ before do
13
+ NanoStore.shared_store = NanoStore.store
14
+ end
15
+
16
+ after do
17
+ NanoStore.shared_store = nil
18
+ end
19
+
20
+ describe "#bag" do
21
+ it "adds a reader to the class" do
22
+ todo = Todo.create(:title => "Today Tasks")
23
+ todo.items.is_a?(NanoStore::Bag).should == true
24
+ todo.items.size.should == 0
25
+ end
26
+
27
+ it "adds a writer to the class that can take an array" do
28
+ todo = Todo.create(:title => "Today Tasks")
29
+ todo.items = [TodoItem.new(:text => "Hi"), TodoItem.new(:text => "Foo"), TodoItem.new(:text => "Bar")]
30
+ todo.items.is_a?(NanoStore::Bag).should == true
31
+ todo.items.size.should == 3
32
+ end
33
+
34
+ it "adds a writer to the class that can take a Bag" do
35
+ todo = Todo.create(:title => "Today Tasks")
36
+ todo.items = NanoStore::Bag.bag
37
+ todo.items.is_a?(NanoStore::Bag).should == true
38
+ todo.items.size.should == 0
39
+ end
40
+ end
41
+
42
+ describe "#save" do
43
+ it "save a model also save associated fields" do
44
+ todo = Todo.create(:title => "Today Tasks")
45
+ todo.items = [TodoItem.new(:text => "Hi"), TodoItem.new(:text => "Foo"), TodoItem.new(:text => "Bar")]
46
+ todo.items.is_a?(NanoStore::Bag).should == true
47
+ todo.save
48
+
49
+ todo2 = Todo.find(:title => "Today Tasks").first
50
+ todo2.should.not.be.nil
51
+ todo2.items.is_a?(NSFNanoBag).should == true
52
+ todo2.items.key.should == todo.items.key
53
+ todo2.items.size.should == 3
54
+ todo2.items.to_a.each do |item|
55
+ item.is_a?(TodoItem).should.be.true
56
+ end
57
+ end
58
+ end
59
+ end
data/spec/bag_spec.rb CHANGED
@@ -16,7 +16,6 @@ describe NanoStore::Bag do
16
16
 
17
17
  it "should add objects to bag" do
18
18
  bag = Bag.bag
19
- NanoStore.shared_store.addObject(bag, error:nil)
20
19
 
21
20
  # use << method to add object to bag
22
21
  page = Page.new
@@ -41,7 +40,6 @@ describe NanoStore::Bag do
41
40
 
42
41
  it "should delete object from bag" do
43
42
  bag = Bag.bag
44
- NanoStore.shared_store.addObject(bag, error:nil)
45
43
 
46
44
  # use << method to add object to bag
47
45
  page = Page.new
@@ -67,7 +65,6 @@ describe NanoStore::Bag do
67
65
  before_count = NanoStore.shared_store.bags.count
68
66
 
69
67
  bag = Bag.bag
70
- NanoStore.shared_store.addObject(bag, error:nil)
71
68
 
72
69
  # use << method to add object to bag
73
70
  page = Page.new
@@ -78,4 +75,19 @@ describe NanoStore::Bag do
78
75
  bag.save
79
76
  NanoStore.shared_store.bags.count.should.be == before_count + 1
80
77
  end
78
+
79
+ describe "#to_a" do
80
+ it "convert a bag to array" do
81
+ bag = Bag.bag
82
+ bag << Page.new(:text => "1", :index => 1)
83
+ bag << Page.new(:text => "2", :index => 2)
84
+
85
+ bag.to_a.is_a?(Array).should.be.true
86
+ bag.to_a.size.should == 2
87
+ bag.to_a[0].text.should == "1"
88
+ bag.to_a[1].text.should == "2"
89
+ bag.save
90
+ bag.to_a.size.should == 2
91
+ end
92
+ end
81
93
  end
data/spec/model_spec.rb CHANGED
@@ -10,6 +10,10 @@ describe NanoStore::Model do
10
10
  attribute :name
11
11
  attribute :age
12
12
  end
13
+
14
+ class Listing < NanoStore::Model
15
+ attribute :name
16
+ end
13
17
 
14
18
  def stub_user(name, age, created_at)
15
19
  user = User.new
@@ -115,5 +119,15 @@ describe NanoStore::Model do
115
119
  Plane.count.should == 1
116
120
  end
117
121
 
122
+ # see github issue #15
123
+ # https://github.com/siuying/NanoStoreInMotion/issues/15
124
+ it "should handle some class name with conflicts" do
125
+ listing = Listing.new
126
+ listing.name = "A"
127
+ listing.save
128
+
129
+ Listing.count.should == 1
130
+ Listing.all.size.should == 1
131
+ end
118
132
 
119
133
  end
@@ -3,6 +3,10 @@ describe NanoStore do
3
3
  attribute :name
4
4
  end
5
5
 
6
+ def documents_path
7
+ NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0]
8
+ end
9
+
6
10
  it "create :memory store" do
7
11
  store = NanoStore.store
8
12
  store.filePath.should == ":memory:"
@@ -12,11 +16,11 @@ describe NanoStore do
12
16
  end
13
17
 
14
18
  it "create :persistent store" do
15
- path = App.documents_path + "/nano.db"
19
+ path = documents_path + "/nano.db"
16
20
  store = NanoStore.store :persistent, path
17
21
  store.filePath.should == path
18
22
 
19
- path = App.documents_path + "/nano.db"
23
+ path = documents_path + "/nano.db"
20
24
  store = NanoStore.store :file, path
21
25
  store.filePath.should == path
22
26
  end
data/vendor/Podfile.lock CHANGED
@@ -8,4 +8,4 @@ DEPENDENCIES:
8
8
  SPEC CHECKSUMS:
9
9
  NanoStore: 440272829dfe13db7f1c68de74fc3562475463fe
10
10
 
11
- COCOAPODS: 0.15.2
11
+ COCOAPODS: 0.16.0
@@ -11,6 +11,10 @@ install_resource()
11
11
  echo "ibtool --errors --warnings --notices --output-format human-readable-text --compile ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/`basename $1 .xib`.nib ${PODS_ROOT}/$1 --sdk ${SDKROOT}"
12
12
  ibtool --errors --warnings --notices --output-format human-readable-text --compile "${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/`basename $1 .xib`.nib" "${PODS_ROOT}/$1" --sdk "${SDKROOT}"
13
13
  ;;
14
+ *.framework)
15
+ echo "rsync -rp ${PODS_ROOT}/$1 ${CONFIGURATION_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}"
16
+ rsync -rp "${PODS_ROOT}/$1" "${CONFIGURATION_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}"
17
+ ;;
14
18
  *)
15
19
  echo "cp -R ${PODS_ROOT}/$1 ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"
16
20
  cp -R "${PODS_ROOT}/$1" "${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"
@@ -1,7 +1,7 @@
1
- PODS_ROOT = ${SRCROOT}/Pods
2
- PODS_HEADERS_SEARCH_PATHS = ${PODS_PUBLIC_HEADERS_SEARCH_PATHS}
3
1
  ALWAYS_SEARCH_USER_PATHS = YES
4
2
  OTHER_LDFLAGS = -ObjC -lsqlite3
5
3
  HEADER_SEARCH_PATHS = ${PODS_HEADERS_SEARCH_PATHS}
4
+ PODS_ROOT = ${SRCROOT}/Pods
6
5
  PODS_BUILD_HEADERS_SEARCH_PATHS = "${PODS_ROOT}/BuildHeaders" "${PODS_ROOT}/BuildHeaders/NanoStore"
7
- PODS_PUBLIC_HEADERS_SEARCH_PATHS = "${PODS_ROOT}/Headers" "${PODS_ROOT}/Headers/NanoStore"
6
+ PODS_PUBLIC_HEADERS_SEARCH_PATHS = "${PODS_ROOT}/Headers" "${PODS_ROOT}/Headers/NanoStore"
7
+ PODS_HEADERS_SEARCH_PATHS = ${PODS_PUBLIC_HEADERS_SEARCH_PATHS}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nano-store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-09 00:00:00.000000000 Z
12
+ date: 2012-12-15 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: bubble-wrap
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: 1.1.4
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: 1.1.4
30
14
  - !ruby/object:Gem::Dependency
31
15
  name: motion-cocoapods
32
16
  requirement: !ruby/object:Gem::Requirement
@@ -70,6 +54,7 @@ files:
70
54
  - .gitignore
71
55
  - .gitmodules
72
56
  - .rvmrc
57
+ - CHANGELOG
73
58
  - Gemfile
74
59
  - LICENSE
75
60
  - README.md
@@ -77,6 +62,7 @@ files:
77
62
  - app/app_delegate.rb
78
63
  - lib/nano-store.rb
79
64
  - lib/nano_store.rb
65
+ - lib/nano_store/association.rb
80
66
  - lib/nano_store/bag.rb
81
67
  - lib/nano_store/finder.rb
82
68
  - lib/nano_store/model.rb
@@ -85,6 +71,7 @@ files:
85
71
  - lib/nano_store/version.rb
86
72
  - nano-store.gemspec
87
73
  - resources/.gitignore
74
+ - spec/association_spec.rb
88
75
  - spec/bag_spec.rb
89
76
  - spec/finder_spec.rb
90
77
  - spec/model_spec.rb
@@ -188,12 +175,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
188
175
  - - ! '>='
189
176
  - !ruby/object:Gem::Version
190
177
  version: '0'
178
+ segments:
179
+ - 0
180
+ hash: 1759206666722295826
191
181
  required_rubygems_version: !ruby/object:Gem::Requirement
192
182
  none: false
193
183
  requirements:
194
184
  - - ! '>='
195
185
  - !ruby/object:Gem::Version
196
186
  version: '0'
187
+ segments:
188
+ - 0
189
+ hash: 1759206666722295826
197
190
  requirements: []
198
191
  rubyforge_project:
199
192
  rubygems_version: 1.8.24
@@ -202,6 +195,7 @@ specification_version: 3
202
195
  summary: Wrapper for NanoStore, a lightweight schema-less key-value document database
203
196
  based on sqlite, for RubyMotion.
204
197
  test_files:
198
+ - spec/association_spec.rb
205
199
  - spec/bag_spec.rb
206
200
  - spec/finder_spec.rb
207
201
  - spec/model_spec.rb