class RearORM

Attributes

model[R]
pkey[R]

Public Class Methods

new(model, pkey = :id) click to toggle source
# File lib/rear/orm.rb, line 6
def initialize model, pkey = :id
  @model, @pkey, @orm = model, pkey, orm(model)
end

Public Instance Methods

[](id) click to toggle source
# File lib/rear/orm.rb, line 10
def [] id
  sequel? ?
    model[id] :
    model.first(conditions: {pkey => id})
end
assoc_count(assoc, item, conditions) click to toggle source
# File lib/rear/orm.rb, line 41
def assoc_count assoc, item, conditions
  if sequel?
    sequel_dataset(item.send('%s_dataset' % assoc), conditions).count
  else
    if result = item.send(assoc)
      result.respond_to?(:count) ? result.count(conditions) : 1
    else
      0
    end
  end
end
assoc_filter(assoc, item, conditions) click to toggle source
# File lib/rear/orm.rb, line 32
def assoc_filter assoc, item, conditions
  if sequel?
    sequel_dataset(item.send('%s_dataset' % assoc), conditions).all
  else
    result = item.send(assoc, conditions)
    result.respond_to?(:size) ? result : [result].compact
  end
end
count(conditions = {}) click to toggle source
# File lib/rear/orm.rb, line 16
def count conditions = {}
  if sequel?
    sequel_dataset(model, conditions).count
  else
    model.count(conditions)
  end
end
delete_multiple(*ids) click to toggle source
# File lib/rear/orm.rb, line 53
def delete_multiple *ids
  ids.flatten!
  model.destroy(ids) if @orm == :ar
  model.all(pkey => ids).destroy! if @orm == :dm
  model.filter(pkey => ids).destroy if @orm == :sq
end
filter(conditions = {}) click to toggle source
# File lib/rear/orm.rb, line 24
def filter conditions = {}
  if sequel?
    sequel_dataset(model, conditions).all
  else
    model.all(conditions)
  end
end
sequel?() click to toggle source
# File lib/rear/orm.rb, line 60
def sequel?; @orm == :sq end
sequel_dataset(dataset, conditions = {}) click to toggle source
# File lib/rear/orm.rb, line 62
def sequel_dataset dataset, conditions = {}
  filters, limit, offset, order =
    conditions.values_at(:conditions, :limit, :offset, :order)
  ds = limit ? dataset.limit(*[limit, offset].compact) : dataset
  ds.filter(filters || {}).order(*[order].compact)
end
singularize(smth) click to toggle source
# File lib/rear/orm.rb, line 69
def singularize smth
  model.send(:singularize, smth) if sequel?
end