Class: TinyDS::Query

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

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ Query

Returns a new instance of Query.



3
4
5
6
# File 'lib/tiny_ds/query.rb', line 3

def initialize(model_class)
  @model_class = model_class
  @q = AppEngine::Datastore::Query.new(@model_class.kind)
end

Instance Method Details

#all(opts = {}) ⇒ Object

todo(tx=nil)



48
49
50
51
52
53
54
# File 'lib/tiny_ds/query.rb', line 48

def all(opts={}) #todo(tx=nil)
  models = []
  @q.each(opts) do |entity|
    models << @model_class.new_from_entity(entity)
  end
  models
end

#ancestor(anc) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/tiny_ds/query.rb', line 7

def ancestor(anc)
  anc = case anc
        when Base;   anc.key
        when String; LowDS::KeyFactory.stringToKey(anc)
        when AppEngine::Datastore::Key; anc
        else raise "unknown type. anc=#{anc.inspect}"
        end
  @q.set_ancestor(anc)
  self
end

#collect(opts = {}) ⇒ Object

todo(tx=nil)



60
61
62
63
64
65
66
# File 'lib/tiny_ds/query.rb', line 60

def collect(opts={}) #todo(tx=nil)
  collected = []
  @q.each(opts) do |entity|
    collected << yield(@model_class.new_from_entity(entity))
  end
  collected
end

#countObject

todo(tx=nil)



42
43
44
# File 'lib/tiny_ds/query.rb', line 42

def count #todo(tx=nil)
  @q.count
end

#each(opts = {}) ⇒ Object

todo(tx=nil)



55
56
57
58
59
# File 'lib/tiny_ds/query.rb', line 55

def each(opts={}) #todo(tx=nil)
  @q.each(opts) do |entity|
    yield(@model_class.new_from_entity(entity))
  end
end

#filter(*args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tiny_ds/query.rb', line 17

def filter(*args)
  if args.size==1 && args.first.kind_of?(Hash)
    args.first.each do |k,v|
      filter(k,"==",v)
    end
  else
    name, operator, value = *args
    @model_class.property_definition(name) # check exist?
    @q.filter(name, operator, value)
  end
  self
end

#keys(opts = {}) ⇒ Object

todo(tx=nil)



67
68
69
70
71
72
73
74
# File 'lib/tiny_ds/query.rb', line 67

def keys(opts={}) #todo(tx=nil)
  keys = []
  self.keys_only
  @q.each(opts) do |entity|
    keys << entity.key
  end
  keys
end

#keys_onlyObject



38
39
40
41
# File 'lib/tiny_ds/query.rb', line 38

def keys_only
  @q.java_query.setKeysOnly
  self
end

#oneObject

todo(tx=nil)



45
46
47
# File 'lib/tiny_ds/query.rb', line 45

def one #todo(tx=nil)
  @model_class.new_from_entity(@q.entity)
end

#sort(name, dir = :asc) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/tiny_ds/query.rb', line 29

def sort(name, dir=:asc)
  @model_class.property_definition(name) # check exist?
  direction = {
    :asc  => AppEngine::Datastore::Query::ASCENDING,
    :desc => AppEngine::Datastore::Query::DESCENDING
  }[dir]
  @q.sort(name, direction)
  self
end