Module: Dinamo::Model::Persistence::ClassMethods

Defined in:
lib/dinamo/model/persistence.rb

Instance Method Summary collapse

Instance Method Details

#create(attributes = nil, &block) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/dinamo/model/persistence.rb', line 23

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) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/dinamo/model/persistence.rb', line 32

def create!(attributes = nil, &block)
  object = new(**attributes, &block)
  object.new_record = true
  object.with_callback :create do
    object.save!
    object
  end
end

#get(**keys) ⇒ Object



12
13
14
# File 'lib/dinamo/model/persistence.rb', line 12

def get(**keys)
  get!(**keys) rescue nil
end

#get!(**keys) ⇒ Object



16
17
18
19
20
21
# 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
  new(**symbolize(item))
end

#symbolize(attrs) ⇒ Object



41
42
43
44
45
# File 'lib/dinamo/model/persistence.rb', line 41

def symbolize(attrs)
  attrs.each_with_object({}) do |(key, val), new_attrs|
    new_attrs[key.to_sym] = val
  end
end