Module: MotionDataWrapper::Relation::FinderMethods

Included in:
MotionDataWrapper::Relation
Defined in:
lib/motion_data_wrapper/relation/finder_methods.rb

Instance Method Summary collapse

Instance Method Details

#allObject



5
6
7
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 5

def all
  to_a
end

#countObject



9
10
11
12
13
14
15
16
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 9

def count
  return to_a.count if fetchOffset > 0
  old_result_type = self.resultType
  self.resultType = NSCountResultType
  count = to_a[0]
  self.resultType = old_result_type
  return count
end

#destroy_allObject



18
19
20
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 18

def destroy_all
  all.map &:destroy
end

#except(query_part) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 22

def except(query_part)
  case query_part.to_sym
   when :where
     self.predicate = nil
   when :order
     self.sortDescriptors = nil
   when :limit
     self.fetchLimit = 0
   else
     raise ArgumentError, "unsupport query part '#{query_part}'"
   end
   self
end

#firstObject



36
37
38
39
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 36

def first
  self.fetchLimit = 1
  to_a[0]
end

#lastObject



41
42
43
44
45
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 41

def last
  self.fetchOffset = self.count - 1 unless self.count < 1
  self.fetchLimit = 1
  to_a[0]
end

#limit(l) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 47

def limit(l)
  l = l.to_i
  raise ArgumentError, "limit '#{l}' cannot be less than zero. Use zero for no limit." if l < 0
  self.fetchLimit = l
  self
end

#offset(o) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
59
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 54

def offset(o)
  o = o.to_i
  raise ArgumentError, "offset '#{o}' cannot be less than zero." if o < 0
  self.fetchOffset = o
  self
end

#order(column, opts = {}) ⇒ Object



61
62
63
64
65
66
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 61

def order(column, opts={})
  descriptors = sortDescriptors.nil? ? [] : sortDescriptors.mutableCopy
  descriptors << NSSortDescriptor.sortDescriptorWithKey(column.to_s, ascending:opts.fetch(:ascending, true))
  self.sortDescriptors = descriptors
  self
end

#pluck(column) ⇒ Object

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 68

def pluck(column)
  self.resultType = NSDictionaryResultType

   attribute_description = entity.attributesByName[column]
   raise ArgumentError, "#{column} not a valid column name" if attribute_description.nil?

   self.propertiesToFetch = [attribute_description]
   to_a.collect { |r| r[column] }
end

#uniqObject



78
79
80
81
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 78

def uniq
  self.returnsDistinctResults = true
  self
end

#where(format, *args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 83

def where(format, *args)
  new_predicate = NSPredicate.predicateWithFormat(format.gsub("?", "%@"), argumentArray:args)

  if self.predicate
    self.predicate = NSCompoundPredicate.andPredicateWithSubpredicates([predicate, new_predicate])
  else
    self.predicate = new_predicate
  end

  self
end