Module: Morpheus::Mixins::Persistence

Included in:
Base
Defined in:
lib/morpheus/mixins/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/morpheus/mixins/persistence.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#destroyObject



32
33
34
# File 'lib/morpheus/mixins/persistence.rb', line 32

def destroy
  self.class.delete(UrlBuilder.destroy(self.class, id))
end

#save(with_validations = true) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/morpheus/mixins/persistence.rb', line 9

def save(with_validations = true)
  attributes_for_save = { self.class.attributes_root => attributes_without_basic_attributes.reject { |k,v| v.nil? } }

  if with_validations
    return false unless valid?
  end

  if new_record?
    built_object = self.class.post(*UrlBuilder.save(self.class, nil, attributes_for_save))
  else
    built_object = self.class.put(*UrlBuilder.save(self.class, id, attributes_for_save))
  end
  built_object.instance_variables.each do |iv|
    self.instance_variable_set(iv, built_object.instance_variable_get(iv))
  end
  @valid
end

#update_attributes(new_attributes) ⇒ Object



27
28
29
30
# File 'lib/morpheus/mixins/persistence.rb', line 27

def update_attributes(new_attributes)
  merge_attributes(new_attributes)
  save
end