Class: Yapper::Document::Criteria

Inherits:
Object
  • Object
show all
Defined in:
lib/yapper/document/selection.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, criteria, options) ⇒ Criteria

Returns a new instance of Criteria.



77
78
79
80
81
# File 'lib/yapper/document/selection.rb', line 77

def initialize(klass, criteria, options)
  @klass    = klass
  @criteria = criteria
  @options  = options
end

Instance Method Details

#asc(*fields) ⇒ Object



126
127
128
# File 'lib/yapper/document/selection.rb', line 126

def asc(*fields)
  sort(fields, :asc)
end

#countObject



83
84
85
86
87
# File 'lib/yapper/document/selection.rb', line 83

def count
  count_ptr = Pointer.new(:uint)
  @klass.db.execute { |txn| txn.ext("#{@klass._type}_IDX").getNumberOfRows(count_ptr, matchingQuery: query) }
  count_ptr.value
end

#desc(*fields) ⇒ Object



130
131
132
# File 'lib/yapper/document/selection.rb', line 130

def desc(*fields)
  sort(fields, :desc)
end

#each(&block) ⇒ Object



109
110
111
# File 'lib/yapper/document/selection.rb', line 109

def each(&block)
  to_a.each(&block)
end

#firstObject



89
90
91
# File 'lib/yapper/document/selection.rb', line 89

def first
  asc(:id).limit(1).to_a.first
end

#lastObject



93
94
95
# File 'lib/yapper/document/selection.rb', line 93

def last
  desc(:id).limit(1).to_a.first
end

#limit(limit) ⇒ Object



117
118
119
# File 'lib/yapper/document/selection.rb', line 117

def limit(limit)
  self.class.new(@klass, @criteria, @options.merge(:limit => limit))
end

#sort(fields, direction) ⇒ Object



121
122
123
124
# File 'lib/yapper/document/selection.rb', line 121

def sort(fields, direction)
  order = @options[:order].to_a + fields.map { |f| [f,direction] }
  self.class.new(@klass, @criteria, @options.merge(:order => order))
end

#to_aObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/yapper/document/selection.rb', line 97

def to_a
  results = []

  each_result_proc = proc do |collection, key, attrs, stop|
    results << object_for_attrs(attrs)
  end

  @klass.db.execute { |txn| txn.ext("#{@klass._type}_IDX").enumerateKeysAndObjectsMatchingQuery(query, usingBlock: each_result_proc) }

  results
end

#where(criteria) ⇒ Object



113
114
115
# File 'lib/yapper/document/selection.rb', line 113

def where(criteria)
  self.class.new(@klass, @criteria.merge(criteria), @options)
end