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

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

Instance Method Summary collapse

Instance Method Details

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



44
45
46
47
48
49
50
51
# 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) ⇒ Object



53
54
55
56
57
58
59
60
# 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) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/dinamo/model/persistence.rb', line 40

def exist?(**keys)
  adapter.exist?(**keys)
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
22
23
# 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) ⇒ Object



25
26
27
# File 'lib/dinamo/model/persistence.rb', line 25

def partition(**conditions)
  partition!(**conditions) rescue nil
end

#partition!(**conditions) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



62
63
64
65
66
# 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