Module: MotionDataWrapper::Relation::FinderMethods
- Included in:
- MotionDataWrapper::Relation
- Defined in:
- lib/motion_data_wrapper/relation/finder_methods.rb
Instance Method Summary collapse
- #all ⇒ Object
- #count ⇒ Object
- #destroy_all ⇒ Object
- #empty? ⇒ Boolean
- #except(query_part) ⇒ Object
- #exists? ⇒ Boolean
- #first ⇒ Object
- #first! ⇒ Object
- #last ⇒ Object
- #last! ⇒ Object
- #limit(l) ⇒ Object
- #offset(o) ⇒ Object
- #order(column, opts = {}) ⇒ Object
- #pluck(column) ⇒ Object
- #take ⇒ Object
- #take! ⇒ Object
- #uniq ⇒ Object
- #where(format, *args) ⇒ Object
- #with_context(ctx) ⇒ Object
Instance Method Details
#all ⇒ Object
5 6 7 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 5 def all to_a end |
#count ⇒ Object
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_all ⇒ Object
18 19 20 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 18 def destroy_all all.map &:destroy end |
#empty? ⇒ Boolean
22 23 24 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 22 def empty? self.count == 0 end |
#except(query_part) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 26 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 |
#exists? ⇒ Boolean
40 41 42 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 40 def exists? !empty? end |
#first ⇒ Object
44 45 46 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 44 def first take end |
#first! ⇒ Object
48 49 50 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 48 def first! take! end |
#last ⇒ Object
52 53 54 55 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 52 def last self.fetchOffset = self.count - 1 unless self.count < 1 take end |
#last! ⇒ Object
57 58 59 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 57 def last! last or raise RecordNotFound end |
#limit(l) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 61 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
68 69 70 71 72 73 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 68 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
75 76 77 78 79 80 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 75 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
82 83 84 85 86 87 88 89 90 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 82 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 |
#take ⇒ Object
92 93 94 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 92 def take limit(1).to_a[0] end |
#take! ⇒ Object
96 97 98 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 96 def take! take or raise RecordNotFound end |
#uniq ⇒ Object
100 101 102 103 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 100 def uniq self.returnsDistinctResults = true self end |
#where(format, *args) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 105 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 |
#with_context(ctx) ⇒ Object
117 118 119 120 |
# File 'lib/motion_data_wrapper/relation/finder_methods.rb', line 117 def with_context(ctx) @ctx = ctx self end |