Module: Architect4r::Model::Persistency::InstanceMethods

Defined in:
lib/architect4r/model/persistency.rb

Instance Method Summary collapse

Instance Method Details

#create!(options = {}) ⇒ Object

Creates the document in the db. Raises an exception if the document is not created properly.



14
15
16
# File 'lib/architect4r/model/persistency.rb', line 14

def create!(options = {})
  self.fail_validate!(self) unless self.create(options)
end

#destroyed?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/architect4r/model/persistency.rb', line 43

def destroyed?
  !!@_destroyed
end

#idObject Also known as: to_key, to_param



27
28
29
30
31
32
33
# File 'lib/architect4r/model/persistency.rb', line 27

def id
  @id ||= if raw_data && raw_data['self'].present?
    raw_data['self'].split('/').last.to_i
  else
    nil
  end
end

#new?Boolean Also known as: new_record?

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/architect4r/model/persistency.rb', line 37

def new?
  # Persisted objects always have an id.
  id.nil?
end

#persisted?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/architect4r/model/persistency.rb', line 47

def persisted?
  !new? && !destroyed?
end

#save(options = {}) ⇒ Object



18
19
20
# File 'lib/architect4r/model/persistency.rb', line 18

def save(options = {})
  self.new? ? create(options) : update(options)
end

#save!Object



22
23
24
25
# File 'lib/architect4r/model/persistency.rb', line 22

def save!
  self.class.fail_validate!(self) unless self.save
  true
end

#update_attributes(hash) ⇒ Object

Update the document’s attributes and save. For example:



52
53
54
55
# File 'lib/architect4r/model/persistency.rb', line 52

def update_attributes(hash)
  update_attributes_without_saving hash
  save
end