Module: NanoStore::FinderMethods

Included in:
Model
Defined in:
lib/nano_store/finder.rb

Instance Method Summary collapse

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)
    sort_options = args[0][:sort] || {}
  else
    sort_options = {}
  end
  
  if sort_options.empty?
    self.store.objectsOfClassNamed(self.to_s)
  else
    sort_descriptors = sort_descriptor_with_options(sort_options)
    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>]

Raises:



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
    options = arg[0]
    if arg[1] && arg[1].is_a?(Hash)
      sort_options = arg[1][:sort] || {}
    else
      sort_options = {}
    end
  elsif arg[0] && arg[1] && arg[2]
    # standard way to find
    options = {arg[0] => {arg[1] => arg[2]}}
    if arg[4] && arg[4].is_a?(Hash)
      sort_options = arg[4][:sort] || {}
    else
      sort_options = {}
    end
  else
    raise "unexpected parameters #{arg}"
  end
  search = NSFNanoSearch.searchWithStore(self.store)

  unless options.empty?
    expressions = expressions_with_options(options)
    search.expressions = expressions
  end
  
  sort_descriptors = sort_descriptor_with_options(sort_options)
  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"]

Raises:



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
    options = arg[0]
    if arg[1] && arg[1].is_a?(Hash)
      sort_options = arg[1][:sort] || {}
    else
      sort_options = {}
    end
  elsif arg[0] && arg[1] && arg[2]
    # standard way to find
    options = {arg[0] => {arg[1] => arg[2]}}        
    if arg[4] && arg[4].is_a?(Hash)
      sort_options = arg[4][:sort] || {}
    else
      sort_options = {}
    end
  else
    raise "unexpected parameters #{arg}"
  end
  
  search = NSFNanoSearch.searchWithStore(self.store)

  unless options.empty?
    expressions = expressions_with_options(options)
    search.expressions = expressions
  end

  sort_descriptors = sort_descriptor_with_options(sort_options)
  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