module GdatastoreMapper::QueryMethods
Public Instance Methods
find(id)
click to toggle source
# File lib/gdatastore_mapper/relation/query_methods.rb, line 15 def find id return nil if id.nil? query = Google::Cloud::Datastore::Key.new in_class, id.to_i entities = GdatastoreMapper::Session.dataset.lookup query from_entity entities.first if entities.any? end
find_by(condition)
click to toggle source
# File lib/gdatastore_mapper/relation/query_methods.rb, line 22 def find_by condition return nil unless condition.is_a?(Hash) where(condition)&.first end
find_or_create(condition)
click to toggle source
# File lib/gdatastore_mapper/relation/query_methods.rb, line 27 def find_or_create condition return nil unless condition.is_a?(Hash) if record = where(condition)&.first record else create condition end end
limit(condition)
click to toggle source
# File lib/gdatastore_mapper/relation/query_methods.rb, line 36 def limit condition return nil unless condition.is_a?(Fixnum) self[0..condition-1] end
order(condition, &block)
click to toggle source
# File lib/gdatastore_mapper/relation/query_methods.rb, line 5 def order condition, &block return nil unless condition.is_a?(Hash) dataset_run(order_query(condition), &block) end
where(condition, &block)
click to toggle source
# File lib/gdatastore_mapper/relation/query_methods.rb, line 10 def where condition, &block return nil unless condition.is_a?(Hash) dataset_run(where_query(condition), &block) end
Private Instance Methods
dataset_run(query, &block)
click to toggle source
# File lib/gdatastore_mapper/relation/query_methods.rb, line 63 def dataset_run query, &block entities = GdatastoreMapper::Session.dataset.run query result = GdatastoreMapper::Relation.new(in_class, self.association) entities.each do |entity| record = from_entity(entity) block.call(record) if block_given? result << record if record end result end
from_entity(entity)
click to toggle source
# File lib/gdatastore_mapper/relation/query_methods.rb, line 74 def from_entity entity record = self.first.class.new record.id = entity.key.id entity.properties.to_hash.each do |name, value| record.send "#{name}=", value if record.respond_to? "#{name}=" end record end
in_class()
click to toggle source
# File lib/gdatastore_mapper/relation/query_methods.rb, line 43 def in_class self&.first&.class&.to_s end
order_query(condition)
click to toggle source
# File lib/gdatastore_mapper/relation/query_methods.rb, line 47 def order_query condition query = Google::Cloud::Datastore::Query.new.kind(in_class) condition.each do |property, value| query.order(property.to_s, value) end query end
where_query(condition)
click to toggle source
# File lib/gdatastore_mapper/relation/query_methods.rb, line 55 def where_query condition query = Google::Cloud::Datastore::Query.new.kind(in_class) condition.each do |property, value| query.where(property.to_s, '=', value) end query end