Module: Yapper::Document::Selection::ClassMethods

Defined in:
lib/yapper/document/selection.rb

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object



6
7
8
# File 'lib/yapper/document/selection.rb', line 6

def all(options={})
  Criteria.new(self, {}, options)
end

#asc(*fields) ⇒ Object



14
15
16
# File 'lib/yapper/document/selection.rb', line 14

def asc(*fields)
  Criteria.new(self, {}, {}).sort(fields, :asc)
end

#countObject



22
23
24
# File 'lib/yapper/document/selection.rb', line 22

def count
  all.count
end

#desc(*fields) ⇒ Object



18
19
20
# File 'lib/yapper/document/selection.rb', line 18

def desc(*fields)
  Criteria.new(self, {}, {}).sort(fields, :desc)
end

#find(id) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/yapper/document/selection.rb', line 39

def find(id)
  return nil if id.nil?

  attrs = db.execute { |txn| txn.objectForKey(id, inCollection: self._type) }

  attrs ? object_for_attrs(attrs) : nil
end

#search(query) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yapper/document/selection.rb', line 26

def search(query)
  return nil if query.nil?

  results = []
  each_result_proc = proc do |collection, id, attrs, stop|
    results << object_for_attrs(attrs)
  end

  db.execute { |txn| txn.ext("#{self._type}_SIDX").enumerateKeysAndObjectsMatching(query, usingBlock: each_result_proc) }

  results
end

#when(id, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/yapper/document/selection.rb', line 47

def when(id, &block)
  observer = nil
  observer_block = proc do |data|
    observer = nil
    NSNotificationCenter.defaultCenter.removeObserver(observer)
    block.call(data.object)
  end
  observer = NSNotificationCenter.
               defaultCenter.
               addObserverForName("yapper:#{self.model_name}:save",
                                  object: nil,
                                  queue: NSOperationQueue.mainQueue,
                                  usingBlock: observer_block) 
  if result = self.find(id)
    unless observer.nil?
      NSNotificationCenter.defaultCenter.removeObserver(observer)
      block.call(result) 
    end
  end
end

#where(criteria, options = {}) ⇒ Object



10
11
12
# File 'lib/yapper/document/selection.rb', line 10

def where(criteria, options={})
  Criteria.new(self, criteria, options)
end