Module: Foobara::Entity::Concerns::Persistence
- Includes:
- Concern
- Included in:
- Foobara::Entity
- Defined in:
- foobara-0.0.110/projects/entity/src/concerns/persistence.rb
Defined Under Namespace
Modules: ClassMethods
Classes: CannotUpdateHardDeletedRecordError, UnknownIfPersisted
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Concern
foobara_class_methods_module_for, foobara_concern?, included
Instance Attribute Details
#is_built ⇒ Object
Returns the value of attribute is_built.
10
11
12
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 10
def is_built
@is_built
end
|
#is_created ⇒ Object
Returns the value of attribute is_created.
10
11
12
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 10
def is_created
@is_created
end
|
#is_hard_deleted ⇒ Object
Returns the value of attribute is_hard_deleted.
10
11
12
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 10
def is_hard_deleted
@is_hard_deleted
end
|
#is_loaded ⇒ Object
Returns the value of attribute is_loaded.
10
11
12
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 10
def is_loaded
@is_loaded
end
|
#is_persisted ⇒ Object
Returns the value of attribute is_persisted.
10
11
12
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 10
def is_persisted
@is_persisted
end
|
#persisted_attributes ⇒ Object
Returns the value of attribute persisted_attributes.
10
11
12
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 10
def persisted_attributes
@persisted_attributes
end
|
Instance Method Details
#built? ⇒ Boolean
TODO: rename, maybe #detatched? or something?
58
59
60
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 58
def built?
is_built
end
|
#created? ⇒ Boolean
49
50
51
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 49
def created?
is_created
end
|
#dirty? ⇒ Boolean
129
130
131
132
133
134
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 129
def dirty?
return true unless persisted?
return false unless loaded?
persisted_attributes != to_persisted_attributes(attributes)
end
|
#hard_delete! ⇒ Object
18
19
20
21
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 18
def hard_delete!
hard_delete_without_callbacks!
fire(:hard_deleted)
end
|
#hard_delete_without_callbacks! ⇒ Object
23
24
25
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 23
def hard_delete_without_callbacks!
@is_hard_deleted = true
end
|
#hard_deleted? ⇒ Boolean
136
137
138
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 136
def hard_deleted?
@is_hard_deleted
end
|
#load_if_necessary!(attribute_name_or_attributes) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 62
def load_if_necessary!(attribute_name_or_attributes)
return if built?
return if loaded?
return unless persisted?
attribute_name = if attribute_name_or_attributes.is_a?(::Hash)
if attribute_name_or_attributes.keys.size == 1
attribute_name_or_attributes.keys.first.to_sym
end
elsif attribute_name_or_attributes
attribute_name_or_attributes.to_sym
end
return if attribute_name == primary_key_attribute.to_sym
transaction = Foobara::Persistence::EntityBase::Transaction.open_transaction_for(self)
unless transaction
raise NoCurrentTransactionError, "Trying to load a #{entity_name} outside of a transaction."
end
unless transaction.loading?(self)
transaction.load(self)
end
end
|
#loaded? ⇒ Boolean
53
54
55
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 53
def loaded?
is_loaded
end
|
#persisted? ⇒ Boolean
Persisted means it is currently written to the database
45
46
47
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 45
def persisted?
is_persisted
end
|
#restore!(skip_callbacks: false) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 107
def restore!(skip_callbacks: false)
if persisted?
if hard_deleted?
unhard_delete!(skip_callbacks:)
end
if loaded?
if skip_callbacks
write_attributes_without_callbacks(persisted_attributes)
else
write_attributes(persisted_attributes)
end
end
else
hard_delete!
end
end
|
#restore_without_callbacks! ⇒ Object
125
126
127
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 125
def restore_without_callbacks!
restore!(skip_callbacks: true)
end
|
#save_persisted_attributes ⇒ Object
27
28
29
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 27
def save_persisted_attributes
self.persisted_attributes = to_persisted_attributes(attributes)
end
|
#to_persisted_attributes(object) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 31
def to_persisted_attributes(object)
case object
when ::Hash
object.to_h do |k, v|
[to_persisted_attributes(k), to_persisted_attributes(v)]
end
when ::Array
object.map { |v| to_persisted_attributes(v) }
else
object.dup
end
end
|
#unhard_delete!(skip_callbacks: false) ⇒ Object
99
100
101
102
103
104
105
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 99
def unhard_delete!(skip_callbacks: false)
self.is_hard_deleted = false
unless skip_callbacks
fire(:unhard_deleted)
end
end
|
#verify_not_hard_deleted! ⇒ Object
92
93
94
95
96
97
|
# File 'foobara-0.0.110/projects/entity/src/concerns/persistence.rb', line 92
def verify_not_hard_deleted!
if hard_deleted?
raise CannotUpdateHardDeletedRecordError,
"Cannot make further updates to this record because it has been hard deleted"
end
end
|