Module: Toy::Persistence

Extended by:
ActiveSupport::Concern
Included in:
DirtyStore, Store
Defined in:
lib/toy/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#adapterObject



35
36
37
# File 'lib/toy/persistence.rb', line 35

def adapter
  self.class.adapter
end

#deleteObject



82
83
84
85
# File 'lib/toy/persistence.rb', line 82

def delete
  @_destroyed = true
  adapter.delete(id)
end

#destroyObject



78
79
80
# File 'lib/toy/persistence.rb', line 78

def destroy
  delete
end

#destroyed?Boolean

Returns:



61
62
63
# File 'lib/toy/persistence.rb', line 61

def destroyed?
  @_destroyed == true
end

#initialize(attrs = {}) ⇒ Object



39
40
41
42
# File 'lib/toy/persistence.rb', line 39

def initialize(attrs={})
  @_new_record = true
  super
end

#initialize_copy(other) ⇒ Object



51
52
53
54
55
# File 'lib/toy/persistence.rb', line 51

def initialize_copy(other)
  super
  @_new_record = true
  @_destroyed  = false
end

#initialize_from_database(attrs = {}) ⇒ Object



44
45
46
47
48
49
# File 'lib/toy/persistence.rb', line 44

def initialize_from_database(attrs={})
  @_new_record = false
  initialize_attributes
  send("attributes=", attrs, false)
  self
end

#new_record?Boolean

Returns:



57
58
59
# File 'lib/toy/persistence.rb', line 57

def new_record?
  @_new_record == true
end

#persisted?Boolean

Returns:



65
66
67
# File 'lib/toy/persistence.rb', line 65

def persisted?
  !new_record? && !destroyed?
end

#saveObject



69
70
71
# File 'lib/toy/persistence.rb', line 69

def save(*)
  new_record? ? create : update
end

#update_attributes(attrs) ⇒ Object



73
74
75
76
# File 'lib/toy/persistence.rb', line 73

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