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.bare_class_name)
  else
    sort_descriptors = sort_descriptor_with_options(sort_options)
    self.store.objectsOfClassNamed(self.bare_class_name, usingSortDescriptors:sort_descriptors)
  end
end

#bare_class_nameObject



140
141
142
# File 'lib/nano_store/finder.rb', line 140

def bare_class_name
  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>]

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
64
65
66
67
68
# 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
  elsif arg.empty?
    options = {}
    sort_options = {}
  else
    raise "unexpected parameters #{arg}"
  end
  search = NSFNanoSearch.searchWithStore(self.store)

  expressions = expressions_with_options(options)
  search.expressions = expressions

  sort_descriptors = sort_descriptor_with_options(sort_options)
  search.sort = sort_descriptors
  search.filterClass = self.bare_class_name

  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)

Raises:



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"]

Raises:



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
    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
  elsif arg.empty?
    options = {}
    sort_options = {}
  else
    raise "unexpected parameters #{arg}"
  end

  search = NSFNanoSearch.searchWithStore(self.store)

  expressions = expressions_with_options(options)
  search.expressions = expressions

  sort_descriptors = sort_descriptor_with_options(sort_options)
  search.sort = sort_descriptors
  search.filterClass = self.bare_class_name

  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