Module: NanoStore::FinderMethods
- Included in:
- Model
- Defined in:
- lib/nano_store/finder.rb
Instance Method Summary collapse
- #all(*args) ⇒ Object
- #bare_class_name ⇒ Object
-
#find(*arg) ⇒ Object
find model by criteria.
-
#find_by_key(key) ⇒ Object
find a model by key.
-
#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.) else sort_descriptors = () self.store.objectsOfClassNamed(self., usingSortDescriptors:sort_descriptors) end end |
#bare_class_name ⇒ Object
140 141 142 |
# File 'lib/nano_store/finder.rb', line 140 def self.to_s.split("::").last 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 64 65 66 67 68 |
# 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 elsif arg.empty? = {} = {} else raise "unexpected parameters #{arg}" end search = NSFNanoSearch.searchWithStore(self.store) expressions = () search.expressions = expressions sort_descriptors = () search.sort = sort_descriptors search.filterClass = self. error_ptr = Pointer.new(:id) searchResults = search.searchObjectsWithReturnType(NSFReturnObjects, error:error_ptr) raise NanoStoreError, error_ptr[0].description if error_ptr[0] if searchResults.is_a?(NSDictionary) searchResults.values else searchResults end end |
#find_by_key(key) ⇒ Object
find a model by key
Return an object or nil (if not found)
Examples: User.find_by_key(my_key)
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/nano_store/finder.rb', line 129 def find_by_key(key) search = NSFNanoSearch.searchWithStore(self.store) search.key = key error_ptr = Pointer.new(:id) searchResult = search.searchObjectsWithReturnType(NSFReturnObjects, error:error_ptr).first raise NanoStoreError, error_ptr[0].description if error_ptr[0] searchResult.last if searchResult 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"]
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 114 115 116 117 118 119 120 121 |
# File 'lib/nano_store/finder.rb', line 79 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 elsif arg.empty? = {} = {} else raise "unexpected parameters #{arg}" end search = NSFNanoSearch.searchWithStore(self.store) expressions = () search.expressions = expressions sort_descriptors = () search.sort = sort_descriptors search.filterClass = self. error_ptr = Pointer.new(:id) searchResults = search.searchObjectsWithReturnType(NSFReturnKeys, error:error_ptr) raise NanoStoreError, error_ptr[0].description if error_ptr[0] if searchResults.is_a?(NSDictionary) searchResults.values else searchResults end end |