Module: MotionPrime::ModelFinderMixin::ClassMethods

Included in:
NSFNanoBag
Defined in:
motion-prime/models/_finder_mixin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bare_classObject

Returns the value of attribute bare_class.



6
7
8
# File 'motion-prime/models/_finder_mixin.rb', line 6

def bare_class
  @bare_class
end

Instance Method Details

#all(*args) ⇒ Object

Find all models



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'motion-prime/models/_finder_mixin.rb', line 11

def all(*args)
  return [] unless self.store

  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



155
156
157
158
# File 'motion-prime/models/_finder_mixin.rb', line 155

def bare_class_name
  subject = @bare_class || self
  subject.to_s.split("::").last
end

#find(*arg) ⇒ Object

Find model by criteria

@example:

User.find(1) # => [<User#1>]
User.find(:name => "Bob") # => [<User#1>]
User.find(:name => {NSFEqualTo => "Bob"}) # => [<User#1>]

Raises:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'motion-prime/models/_finder_mixin.rb', line 50

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[0].is_a?(Integer)
    return find(id: arg[0])
  elsif arg.empty?
    raise "Please use Model#all to find all records."
  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 StoreError, error_ptr[0].description if error_ptr[0]

  if searchResults.is_a?(NSDictionary)
    searchResults.values
  else
    searchResults
  end
end

#find_by_key(key) ⇒ Object, Nil

Find a model by key

@example:

User.find_by_key(my_key)

Returns:

  • (Object, Nil)

    an object or nil (if not found)

Raises:



144
145
146
147
148
149
150
151
152
153
# File 'motion-prime/models/_finder_mixin.rb', line 144

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 StoreError, error_ptr[0].description if error_ptr[0]

  searchResult.last if searchResult
end

#find_keys(*arg) ⇒ Object

Find model keys by criteria

@example:

User.find_keys(:name, NSFEqualTo, "Bob") # => ["1"]
User.find_keys(:name => "Bob") # => ["1"]
User.find_keys(:name => {NSFEqualTo => "Bob"}) # => ["1"]

Raises:



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'motion-prime/models/_finder_mixin.rb', line 94

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 StoreError, error_ptr[0].description if error_ptr[0]

  if searchResults.is_a?(NSDictionary)
    searchResults.values
  else
    searchResults
  end
end

#firstObject

Find first model model by default order



38
39
40
# File 'motion-prime/models/_finder_mixin.rb', line 38

def first
  all.first
end

#lastObject

Find last model model by default order



31
32
33
# File 'motion-prime/models/_finder_mixin.rb', line 31

def last
  all.last
end