Module: ActiveRemote::Persistence::InstanceMethods

Defined in:
lib/active_remote/persistence.rb

Overview

Instance methods

Instance Method Summary collapse

Instance Method Details

#delete ⇒ Object

Deletes the record from the service (the service determines if the record is hard or soft deleted) and freezes this instance to indicate that no changes should be made (since they can't be persisted). If the record was not deleted, it will have error messages indicating what went wrong. Returns the frozen instance.



76
77
78
79
80
81
# File 'lib/active_remote/persistence.rb', line 76

def delete
  raise ReadOnlyRemoteRecord if readonly?
  execute(:delete, "guid" => read_attribute("guid"))

  return success? ? freeze : false
end

#delete! ⇒ Object

Deletes the record from the service (the service determines if the record is hard or soft deleted) and freezes this instance to indicate that no changes should be made (since they can't be persisted). If the record was not deleted, an exception will be raised. Returns the frozen instance.

Raises:



89
90
91
92
# File 'lib/active_remote/persistence.rb', line 89

def delete!
  delete
  raise ActiveRemoteError.new(errors.to_s) if has_errors?
end

#destroy ⇒ Object

Destroys (hard deletes) the record from the service and freezes this instance to indicate that no changes should be made (since they can't be persisted). If the record was not deleted, it will have error messages indicating what went wrong. Returns the frozen instance.



99
100
101
102
103
104
# File 'lib/active_remote/persistence.rb', line 99

def destroy
  raise ReadOnlyRemoteRecord if readonly?
  execute(:destroy, "guid" => read_attribute("guid"))

  return success? ? freeze : false
end

#destroy! ⇒ Object

Destroys (hard deletes) the record from the service and freezes this instance to indicate that no changes should be made (since they can't be persisted). If the record was not deleted, an exception will be raised. Returns the frozen instance.

Raises:



111
112
113
114
# File 'lib/active_remote/persistence.rb', line 111

def destroy!
  destroy
  raise ActiveRemoteError.new(errors.to_s) if has_errors?
end

#has_errors? ⇒ Boolean

Returns true if the record has errors; otherwise, returns false.

Returns:

  • (Boolean)


118
119
120
# File 'lib/active_remote/persistence.rb', line 118

def has_errors?
  return respond_to?(:errors) && errors.present?
end

#instantiate(record) ⇒ Object

Instantiate a record with the given remote attributes. Generally used when retrieving records that already exist, so @new_record is set to false.



125
126
127
128
129
130
131
132
133
134
# File 'lib/active_remote/persistence.rb', line 125

def instantiate(record)
  skip_dirty_tracking do
    assign_attributes(record)
  end

  run_callbacks :initialize

  @new_record = false
  self
end

#new_record? ⇒ Boolean

Returns true if the remote record hasn't been saved yet; otherwise, returns false.

Returns:

  • (Boolean)


139
140
141
# File 'lib/active_remote/persistence.rb', line 139

def new_record?
  @new_record
end

#persisted? ⇒ Boolean

Returns true if the remote record has been saved; otherwise, returns false.

Returns:

  • (Boolean)


145
146
147
# File 'lib/active_remote/persistence.rb', line 145

def persisted?
  return ! new_record?
end

#readonly? ⇒ Boolean

Returns true if the remote class or remote record is readonly; otherwise, returns false.

Returns:

  • (Boolean)


150
151
152
# File 'lib/active_remote/persistence.rb', line 150

def readonly?
  self.class.readonly? || @readonly
end

#save ⇒ Object

Saves the remote record.

If it is a new record, it will be created through the service, otherwise the existing record gets updated.

The service will run any validations and if any of them fail, will return the record with error messages indicating what went wrong.

Also runs any before/after save callbacks that are defined.



164
165
166
167
168
# File 'lib/active_remote/persistence.rb', line 164

def save
  run_callbacks :save do
    create_or_update
  end
end

#save! ⇒ Object

Saves the remote record.

If it is a new record, it will be created through the service, otherwise the existing record gets updated.

The service will run any validations. If any of them fail (e.g. error messages are returned), an ActiveRemote::RemoteRecordNotSaved is raised.

Also runs any before/after save callbacks that are defined.



180
181
182
# File 'lib/active_remote/persistence.rb', line 180

def save!
  save || raise(RemoteRecordNotSaved)
end

#success? ⇒ Boolean

Returns true if the record doesn't have errors; otherwise, returns false.

Returns:

  • (Boolean)


186
187
188
# File 'lib/active_remote/persistence.rb', line 186

def success?
  return ! has_errors?
end

#update_attributes(attributes) ⇒ Object

Updates the attributes of the remote record from the passed-in hash and saves the remote record. If the object is invalid, it will have error messages and false will be returned.



194
195
196
197
# File 'lib/active_remote/persistence.rb', line 194

def update_attributes(attributes)
  assign_attributes(attributes)
  save
end

#update_attributes!(attributes) ⇒ Object

Updates the attributes of the remote record from the passed-in hash and saves the remote record. If the object is invalid, an ActiveRemote::RemoteRecordNotSaved is raised.



203
204
205
206
# File 'lib/active_remote/persistence.rb', line 203

def update_attributes!(attributes)
  assign_attributes(attributes)
  save!
end