Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix bug that find() and all() returning objects of any classes. Revis…

…e all() to use store.objectsOfClassNamed() method.
  • Loading branch information...
commit 7b39928022e5ef237c72c99f12f17e506d582c27 1 parent a404f97
Francis Chong authored May 23, 2012
6  Gemfile.lock
... ...
@@ -1,9 +1,9 @@
1 1
 PATH
2 2
   remote: .
3 3
   specs:
4  
-    nano-store (0.3.2)
  4
+    nano-store (0.3.5)
5 5
       bubble-wrap (>= 0.1.1)
6  
-      motion-cocoapods (>= 1.0.1)
  6
+      motion-cocoapods (>= 1.0.2)
7 7
 
8 8
 GEM
9 9
   remote: https://rubygems.org/
@@ -11,7 +11,7 @@ GEM
11 11
     bubble-wrap (0.1.1)
12 12
     cocoapods (0.5.1)
13 13
       xcodeproj (~> 0.1.0)
14  
-    motion-cocoapods (1.0.1)
  14
+    motion-cocoapods (1.0.2)
15 15
       cocoapods (>= 0.5.1)
16 16
     motion-redgreen (0.0.2)
17 17
     xcodeproj (0.1.0)
26  lib/nano_store/finder.rb
... ...
@@ -1,7 +1,18 @@
1 1
 module NanoStore
2 2
   module FinderMethods
3 3
     def all(*args)
4  
-      find({}, *args)
  4
+      if args[0].is_a?(Hash)
  5
+        sort_options = args[0][:sort] || {}
  6
+      else
  7
+        sort_options = {}
  8
+      end
  9
+      
  10
+      if sort_options.empty?
  11
+        self.store.objectsOfClassNamed(self.to_s)
  12
+      else
  13
+        sort_descriptors = sort_descriptor_with_options(sort_options)
  14
+        self.store.objectsOfClassNamed(self.to_s, usingSortDescriptors:sort_descriptors)
  15
+      end
5 16
     end
6 17
     
7 18
     # find model by criteria
@@ -46,7 +57,9 @@ def find(*arg)
46 57
       error_ptr = Pointer.new(:id)
47 58
       searchResults = search.searchObjectsWithReturnType(NSFReturnObjects, error:error_ptr)
48 59
       raise NanoStoreError, error_ptr[0].description if error_ptr[0]
49  
-      searchResults
  60
+      
  61
+      # workaround until we find way to only query specific class
  62
+      searchResults.select {|obj| obj.class == self }
50 63
     end
51 64
     
52 65
     # find model keys by criteria
@@ -90,9 +103,13 @@ def find_keys(*arg)
90 103
       search.sort = sort_descriptors
91 104
 
92 105
       error_ptr = Pointer.new(:id)
93  
-      searchResults = search.searchObjectsWithReturnType(NSFReturnKeys, error:error_ptr)
  106
+
  107
+      search.attributesToBeReturned = ["NSFObjectClass", "NSFKey"]
  108
+      searchResults = search.searchObjectsWithReturnType(NSFReturnObjects, error:error_ptr)
94 109
       raise NanoStoreError, error_ptr[0].description if error_ptr[0]
95  
-      searchResults
  110
+      
  111
+      # workaround until we find way to only query specific class
  112
+      searchResults.select {|obj| obj.class == self }.collect(&:key)
96 113
     end
97 114
     
98 115
     protected
@@ -101,7 +118,6 @@ def expressions_with_options(options)
101 118
       options.each do |key, val|
102 119
         attribute = NSFNanoPredicate.predicateWithColumn(NSFAttributeColumn, matching:NSFEqualTo, value:key.to_s)
103 120
         expression = NSFNanoExpression.expressionWithPredicate(attribute)
104  
-
105 121
         if val.is_a?(Hash)
106 122
           val.each do |operator, sub_val|
107 123
             value = NSFNanoPredicate.predicateWithColumn(NSFValueColumn, matching:operator, value:sub_val)
2  lib/nano_store/version.rb
... ...
@@ -1,3 +1,3 @@
1 1
 module NanoStore
2  
-  VERSION = "0.3.5"
  2
+  VERSION = "0.3.6"
3 3
 end
2  nano-store.gemspec
@@ -15,6 +15,6 @@ Gem::Specification.new do |gem|
15 15
   gem.version       = NanoStore::VERSION
16 16
 
17 17
   gem.add_dependency 'bubble-wrap', '>= 0.1.1'
18  
-  gem.add_dependency 'motion-cocoapods', '>= 1.0.1'
  18
+  gem.add_dependency 'motion-cocoapods', '>= 1.0.2'
19 19
   gem.add_development_dependency 'motion-redgreen'
20 20
 end
11  spec/finder_spec.rb
@@ -127,12 +127,19 @@ def stub_user(name, age, created_at)
127 127
     users.size.should == 9
128 128
     users.first.name.should == "Carl"
129 129
   end
130  
-  
131  
-  it "find only a class itself" do
  130
+
  131
+  it "#all only return objects of the class" do
132 132
     Car.create(:name => "Honda")
133 133
     Car.count.should == 1
134 134
     Car.all.size.should == 1
135 135
   end
136 136
   
  137
+  it "#find only return objects of the class" do
  138
+    Car.create(:name => "Honda")
  139
+    Car.count.should == 1
  140
+    Car.find({}).size.should == 1
  141
+    Car.find_keys({}).size.should == 1
  142
+  end
  143
+  
137 144
 
138 145
 end

0 notes on commit 7b39928

Please sign in to comment.
Something went wrong with that request. Please try again.