class EasyMapper::Query
Attributes
model[RW]
Public Class Methods
new(model)
click to toggle source
# File lib/easy_mapper/query.rb, line 11 def initialize(model) @model = model @where = {} @order = {} end
Public Instance Methods
all()
click to toggle source
# File lib/easy_mapper/query.rb, line 65 def all where({}).exec end
avg(field)
click to toggle source
# File lib/easy_mapper/query.rb, line 82 def avg(field) @model.repository.aggregate('AVG', field, @where).to_f end
count(field = '*')
click to toggle source
# File lib/easy_mapper/query.rb, line 78 def count(field = '*') @model.repository.aggregate('COUNT', field, @where) end
delete_all()
click to toggle source
# File lib/easy_mapper/query.rb, line 94 def delete_all @model.repository.delete({}) end
each(&block)
click to toggle source
# File lib/easy_mapper/query.rb, line 74 def each(&block) exec.each(&block) end
exec()
click to toggle source
# File lib/easy_mapper/query.rb, line 61 def exec map_to_model_instances @model.repository.find(self) end
inspect()
click to toggle source
# File lib/easy_mapper/query.rb, line 90 def inspect exec.inspect end
limit(value = nil)
click to toggle source
# File lib/easy_mapper/query.rb, line 41 def limit(value = nil) return @limit unless value @limit = value self end
offset(value = nil)
click to toggle source
# File lib/easy_mapper/query.rb, line 49 def offset(value = nil) return @offset unless value @offset = value self end
order(fields = nil)
click to toggle source
# File lib/easy_mapper/query.rb, line 29 def order(fields = nil) return @order unless fields if @order @order.merge!(fields) else @order = fields end self end
single_result()
click to toggle source
# File lib/easy_mapper/query.rb, line 69 def single_result # TODO: return single result, raise exception if none or more exec.first end
sum(field)
click to toggle source
# File lib/easy_mapper/query.rb, line 86 def sum(field) @model.repository.aggregate('SUM', field, @where) end
where(query = nil)
click to toggle source
# File lib/easy_mapper/query.rb, line 17 def where(query = nil) return @where unless query if @where @where.merge!(query) else @where = query end self end
Private Instance Methods
map_associations_to_many(record)
click to toggle source
# File lib/easy_mapper/query.rb, line 109 def map_associations_to_many(record) @model.associations_to_many.map do |assoc_to_many| [ assoc_to_many.name, assoc_to_many.cls.objects.where(assoc_to_many.mapped_by => record[:id]) ] end.to_h end
map_associations_to_one(record)
click to toggle source
# File lib/easy_mapper/query.rb, line 118 def map_associations_to_one(record) @model.associations_to_one.map do |assoc| assoc_record = record .select { |key, _| key.to_s.include? "#{assoc.name}." } .map { |k, v| [k.to_s.gsub("#{assoc.name}.", '').to_sym, v] } .to_h [ assoc.name, assoc.cls.new(assoc_record) ] end.to_h end
map_to_model_instances(records)
click to toggle source
# File lib/easy_mapper/query.rb, line 100 def map_to_model_instances(records) records.map do |record| associations = map_associations_to_many(record) .merge(map_associations_to_one(record)) @model.new(record.merge(associations)) end end