module Dinamo::Model::Persistence::ClassMethods

Public Instance Methods

create(attributes = nil, &block) click to toggle source
# File lib/dinamo/model/persistence.rb, line 44
def create(attributes = nil, &block)
  object = new(**attributes, &block)
  object.new_record = true
  object.with_callback :create do
    object.save
    object
  end
end
create!(attributes = nil, &block) click to toggle source
# File lib/dinamo/model/persistence.rb, line 53
def create!(attributes = nil, &block)
  object = new(**attributes, &block)
  object.new_record = true
  object.with_callback :create do
    object.save!
    object
  end
end
exist?(**keys) click to toggle source
# File lib/dinamo/model/persistence.rb, line 40
def exist?(**keys)
  adapter.exist?(**keys)
end
get(**keys) click to toggle source
# File lib/dinamo/model/persistence.rb, line 12
def get(**keys)
  get!(**keys) rescue nil
end
get!(**keys) click to toggle source
# File lib/dinamo/model/persistence.rb, line 16
def get!(**keys)
  item = adapter.get(**keys).item 
  fail Exceptions::RecordNotFoundError,
    "Corresponding record (%p) can not be found" % keys unless item
  object = new(**symbolize(item))
  object.new_record = false
  object
end
partition(**conditions) click to toggle source
# File lib/dinamo/model/persistence.rb, line 25
def partition(**conditions)
  partition!(**conditions) rescue nil
end
partition!(**conditions) click to toggle source
# File lib/dinamo/model/persistence.rb, line 29
def partition!(**conditions)
  items = adapter.partition(**conditions).items
  fail Exceptions::RecordNotFoundError,
    "Corresponding record (%p) can not be found" % conditions if items.empty?
  items.map do |item|
    object = new(**symbolize(item))
    object.new_record = false
    object
  end
end
symbolize(attrs) click to toggle source
# File lib/dinamo/model/persistence.rb, line 62
def symbolize(attrs)
  attrs.each_with_object({}) do |(key, val), new_attrs|
    new_attrs[key.to_sym] = val
  end
end