Module: IseshimaStore::QueryMethods

Included in:
Relation
Defined in:
lib/iseshima_store/query_methods.rb

Instance Method Summary collapse

Instance Method Details

#allObject



19
20
21
# File 'lib/iseshima_store/query_methods.rb', line 19

def all
  to_a
end

#exists?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/iseshima_store/query_methods.rb', line 23

def exists?
  to_a.length > 0
end

#find(_id) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/iseshima_store/query_methods.rb', line 44

def find(_id)
  entity = where(id: _id).first
  unless entity
    raise EntityNotFound.new("cannot find entity with id #{_id}")
  end
  entity
end

#find_by(hash) ⇒ Object



52
53
54
# File 'lib/iseshima_store/query_methods.rb', line 52

def find_by(hash)
  where(hash).first
end

#inspectObject



40
41
42
# File 'lib/iseshima_store/query_methods.rb', line 40

def inspect
  to_a.inspect
end

#parent(model) ⇒ Object



12
13
14
15
16
17
# File 'lib/iseshima_store/query_methods.rb', line 12

def parent(model)
  if model.respond_to?(:key)
    @parent_key = model.key
  end
  self
end

#to_aObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/iseshima_store/query_methods.rb', line 27

def to_a
  query = Gcloud::Datastore::Query.new
  query.kind(@klass.to_s)
  @where_clause.conditions.each do |condition|
    query.where(*condition)
  end
  if @parent_key
    query.ancestor(@parent_key)
  end
  results = IseshimaStore::Connection.current.run(query)
  results.map { |entity| @klass.from_entity(entity) }
end

#where(condition) ⇒ Object



3
4
5
# File 'lib/iseshima_store/query_methods.rb', line 3

def where(condition)
  spawn.where!(condition)
end

#where!(condition) ⇒ Object



7
8
9
10
# File 'lib/iseshima_store/query_methods.rb', line 7

def where!(condition)
  @where_clause += condition
  self
end