Module: CurlyMustache::Crud::InstanceMethods

Defined in:
lib/curly_mustache/crud.rb

Instance Method Summary collapse

Instance Method Details

#destroyObject

Delete a record from the data store, invoking the *_destroy callbacks.



133
134
135
# File 'lib/curly_mustache/crud.rb', line 133

def destroy
  delete
end

#initialize(attributes = {}) ⇒ Object

Make a new record in memory with supplied attributes.



103
104
105
106
107
# File 'lib/curly_mustache/crud.rb', line 103

def initialize(attributes = {})
  @attributes = {}
  @new_record = true
  self.attributes = attributes
end

#new_record?Boolean

Returns true if the record has been saved yet.

Returns:

  • (Boolean)


110
111
112
# File 'lib/curly_mustache/crud.rb', line 110

def new_record?
  !!@new_record
end

#reloadObject

Reload the record from the data store, overwriting any attribute changes.



115
116
117
# File 'lib/curly_mustache/crud.rb', line 115

def reload
  returning(self){ read }
end

#saveObject

Save the record to the data store. Returns false if validation fails.



120
121
122
123
# File 'lib/curly_mustache/crud.rb', line 120

def save
  new_record? ? create : update
  (errors.count > 0) ? false : self
end

#save!Object

Save the record to the data store. Raises RecordInvalid if validation fails.



126
127
128
129
130
# File 'lib/curly_mustache/crud.rb', line 126

def save!
  returning(save) do
    errors.count > 0 and raise(RecordInvalid, "Validation failed: #{errors.full_messages.join(', ')}")
  end
end