Module: Superstore::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/superstore/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#becomes(klass) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/superstore/persistence.rb', line 122

def becomes(klass)
  became = klass.new
  became.instance_variable_set("@attributes", @attributes)
  became.instance_variable_set("@changed_attributes", changed_attributes || {})
  became.instance_variable_set("@new_record", new_record?)
  became.instance_variable_set("@destroyed", destroyed?)
  became
end

#destroyObject



97
98
99
100
# File 'lib/superstore/persistence.rb', line 97

def destroy
  self.class.delete(id)
  @destroyed = true
end

#destroyed?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/superstore/persistence.rb', line 85

def destroyed?
  @destroyed
end

#new_record?Boolean

Returns:

  • (Boolean)


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

def new_record?
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  !(new_record? || destroyed?)
end

#reloadObject



131
132
133
134
135
# File 'lib/superstore/persistence.rb', line 131

def reload
  clear_association_cache
  @attributes = self.class.find(id).instance_variable_get('@attributes')
  self
end

#saveObject



93
94
95
# File 'lib/superstore/persistence.rb', line 93

def save(*)
  new_record? ? create_self : update_self
end

#update(attributes) ⇒ Object Also known as: update_attributes



108
109
110
111
# File 'lib/superstore/persistence.rb', line 108

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

#update!(attributes) ⇒ Object Also known as: update_attributes!



115
116
117
118
# File 'lib/superstore/persistence.rb', line 115

def update!(attributes)
  self.attributes = attributes
  save!
end

#update_attribute(name, value) ⇒ Object



102
103
104
105
106
# File 'lib/superstore/persistence.rb', line 102

def update_attribute(name, value)
  name = name.to_s
  send("#{name}=", value)
  save(validate: false)
end