Module: NanoStore::FinderMethods
- Included in:
- Model
- Defined in:
- lib/nano_store/finder.rb
Instance Method Summary collapse
- #all(*args) ⇒ Object
-
#find(*arg) ⇒ Object
find model by criteria.
-
#find_keys(*arg) ⇒ Object
find model keys by criteria.
Instance Method Details
#all(*args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/nano_store/finder.rb', line 3 def all(*args) if args[0].is_a?(Hash) = args[0][:sort] || {} else = {} end if .empty? self.store.objectsOfClassNamed(self.to_s) else sort_descriptors = () self.store.objectsOfClassNamed(self.to_s, usingSortDescriptors:sort_descriptors) end end |
#find(*arg) ⇒ Object
find model by criteria
Return array of models
Examples:
User.find(:name, NSFEqualTo, "Bob") # => [<User#1>]
User.find(:name => "Bob") # => [<User#1>]
User.find(:name => {NSFEqualTo => "Bob"}) # => [<User#1>]
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/nano_store/finder.rb', line 27 def find(*arg) if arg[0].is_a?(Hash) # hash style = arg[0] if arg[1] && arg[1].is_a?(Hash) = arg[1][:sort] || {} else = {} end elsif arg[0] && arg[1] && arg[2] # standard way to find = {arg[0] => {arg[1] => arg[2]}} if arg[4] && arg[4].is_a?(Hash) = arg[4][:sort] || {} else = {} end else raise "unexpected parameters #{arg}" end search = NSFNanoSearch.searchWithStore(self.store) unless .empty? expressions = () search.expressions = expressions end sort_descriptors = () search.sort = sort_descriptors error_ptr = Pointer.new(:id) searchResults = search.searchObjectsWithReturnType(NSFReturnObjects, error:error_ptr) raise NanoStoreError, error_ptr[0].description if error_ptr[0] # workaround until we find way to only query specific class searchResults.select {|obj| obj.class == self } end |
#find_keys(*arg) ⇒ Object
find model keys by criteria
Return array of keys
Examples:
User.find_keys(:name, NSFEqualTo, "Bob") # => ["1"]
User.find_keys(:name => "Bob") # => ["1"]
User.find_keys(:name => {NSFEqualTo => "Bob"}) # => ["1"]
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/nano_store/finder.rb', line 74 def find_keys(*arg) if arg[0].is_a?(Hash) # hash style = arg[0] if arg[1] && arg[1].is_a?(Hash) = arg[1][:sort] || {} else = {} end elsif arg[0] && arg[1] && arg[2] # standard way to find = {arg[0] => {arg[1] => arg[2]}} if arg[4] && arg[4].is_a?(Hash) = arg[4][:sort] || {} else = {} end else raise "unexpected parameters #{arg}" end search = NSFNanoSearch.searchWithStore(self.store) unless .empty? expressions = () search.expressions = expressions end sort_descriptors = () search.sort = sort_descriptors error_ptr = Pointer.new(:id) search.attributesToBeReturned = ["NSFObjectClass", "NSFKey"] searchResults = search.searchObjectsWithReturnType(NSFReturnObjects, error:error_ptr) raise NanoStoreError, error_ptr[0].description if error_ptr[0] # workaround until we find way to only query specific class searchResults.select {|obj| obj.class == self }.collect(&:key) end |