Module: Perry::Persistence::InstanceMethods

Defined in:
lib/perry/persistence.rb

Instance Method Summary collapse

Instance Method Details

#[]=(attribute, value) ⇒ Object



20
21
22
# File 'lib/perry/persistence.rb', line 20

def []=(attribute, value)
  set_attribute(attribute, value)
end

#attributes=(attributes) ⇒ Object



24
25
26
# File 'lib/perry/persistence.rb', line 24

def attributes=(attributes)
  set_attributes(attributes)
end

#destroyObject Also known as: delete

Raises:



46
47
48
49
50
51
# File 'lib/perry/persistence.rb', line 46

def destroy
  raise Perry::PerryError.new("cannot destroy a frozen object") if frozen?
  unless self.new_record? || self[primary_key].nil?
    write_adapter.call(:delete, :object => self).success
  end
end

#destroy!Object Also known as: delete!



54
55
56
# File 'lib/perry/persistence.rb', line 54

def destroy!
  destroy or raise Perry::RecordNotSaved
end

#freezeObject

Calls Object#freeze on the model and on the attributes hash in addition to calling #freeze! to prevent the model from being saved or destroyed.



66
67
68
69
70
# File 'lib/perry/persistence.rb', line 66

def freeze
  freeze!
  attributes.freeze
  super
end

#freeze!Object

Prevents the model from being saved or destroyed in the future while still allowing the model and its attributes hash to be modified.



75
76
77
# File 'lib/perry/persistence.rb', line 75

def freeze!
  @frozen = true
end

#frozen?Boolean

Returns true if ether #freeze or #freeze! has been called on the model.

Returns:

  • (Boolean)


81
82
83
# File 'lib/perry/persistence.rb', line 81

def frozen?
  !!@frozen
end

#reloadObject



59
60
61
# File 'lib/perry/persistence.rb', line 59

def reload
  self.attributes = self.class.where(primary_key => self.send(primary_key)).first.attributes
end

#saveObject

Raises:



28
29
30
31
# File 'lib/perry/persistence.rb', line 28

def save
  raise Perry::PerryError.new("cannot write a frozen object") if frozen?
  write_adapter.call(:write, :object => self).success
end

#save!Object



33
34
35
# File 'lib/perry/persistence.rb', line 33

def save!
  save or raise Perry::RecordNotSaved
end

#update_attributes(attributes) ⇒ Object



37
38
39
40
# File 'lib/perry/persistence.rb', line 37

def update_attributes(attributes)
  self.attributes = attributes
  save
end

#update_attributes!(attributes) ⇒ Object



42
43
44
# File 'lib/perry/persistence.rb', line 42

def update_attributes!(attributes)
  update_attributes(attributes) or raise Perry::RecordNotSaved
end