Class: NoBrainer::GeoResultSet

Inherits:
Object
  • Object
show all
Defined in:
lib/nobrainer_geospatial.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, rql) ⇒ GeoResultSet

Returns a new instance of GeoResultSet.



9
10
11
12
# File 'lib/nobrainer_geospatial.rb', line 9

def initialize(klass, rql)
  self.rql = rql
  self.klass = klass
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



7
8
9
# File 'lib/nobrainer_geospatial.rb', line 7

def klass
  @klass
end

#rqlObject

Returns the value of attribute rql.



7
8
9
# File 'lib/nobrainer_geospatial.rb', line 7

def rql
  @rql
end

Instance Method Details

#[](index) ⇒ Object



77
78
79
# File 'lib/nobrainer_geospatial.rb', line 77

def [](index)
  to_a()[index]
end

#allObject



18
19
20
# File 'lib/nobrainer_geospatial.rb', line 18

def all
  to_a
end

#countObject



32
33
34
# File 'lib/nobrainer_geospatial.rb', line 32

def count
  NoBrainer.run { self.rql.count }
end

#each(&block) ⇒ Object



26
27
28
29
30
# File 'lib/nobrainer_geospatial.rb', line 26

def each(&block)
  NoBrainer.run { self.rql }.each do |result|
    block.call(new_from_db result)
  end
end

#firstObject



22
23
24
# File 'lib/nobrainer_geospatial.rb', line 22

def first
  new_from_db NoBrainer.run { self.rql }.first
end

#limit(n) ⇒ Object



44
45
46
# File 'lib/nobrainer_geospatial.rb', line 44

def limit(n)
  GeoResultSet.new(self.klass, self.rql.limit(n))
end

#order_by(args, order = :asc) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nobrainer_geospatial.rb', line 52

def order_by(args, order = :asc)

  conditions = []

  raise "Invalid sort order: #{order}" unless order == :asc || order == :desc

  if args.class == Array
    conditions = args.map do |arg|
      if order == :asc
        RethinkDB::RQL.new.asc(arg)
      else
        RethinkDB::RQL.new.desc(arg)
      end
    end
  else
    if order == :asc
      conditions = RethinkDB::RQL.new.asc(args)
    else
      conditions = RethinkDB::RQL.new.desc(args)
    end
  end

  GeoResultSet.new(self.klass, self.rql.order_by(*conditions))
end

#skip(n) ⇒ Object



48
49
50
# File 'lib/nobrainer_geospatial.rb', line 48

def skip(n)
  GeoResultSet.new(self.klass, self.rql.skip(n))
end

#to_aObject



14
15
16
# File 'lib/nobrainer_geospatial.rb', line 14

def to_a
  NoBrainer.run { self.rql }.to_a.map {|r| new_from_db(r) }
end

#to_rqlObject



81
82
83
# File 'lib/nobrainer_geospatial.rb', line 81

def to_rql
  self.rql
end

#where(args = nil, &block) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/nobrainer_geospatial.rb', line 36

def where(args = nil, &block)
  if block_given?
    GeoResultSet.new(self.klass, self.rql.filter(block))
  else
    GeoResultSet.new(self.klass, self.rql.filter(args))
  end
end