Module: DB::Model::Record
- Defined in:
- lib/db/model/record.rb
Defined Under Namespace
Modules: Base
Classes: SerializationError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
163
164
165
|
# File 'lib/db/model/record.rb', line 163
def attributes
@attributes
end
|
#cache ⇒ Object
Returns the value of attribute cache.
165
166
167
|
# File 'lib/db/model/record.rb', line 165
def cache
@cache
end
|
#changed ⇒ Object
Returns the value of attribute changed.
164
165
166
|
# File 'lib/db/model/record.rb', line 164
def changed
@changed
end
|
#context ⇒ Object
Returns the value of attribute context.
162
163
164
|
# File 'lib/db/model/record.rb', line 162
def context
@context
end
|
Class Method Details
.included(klass) ⇒ Object
151
152
153
|
# File 'lib/db/model/record.rb', line 151
def self.included(klass)
klass.extend(Base)
end
|
Instance Method Details
#assign(changed) ⇒ Object
179
180
181
182
183
|
# File 'lib/db/model/record.rb', line 179
def assign(changed)
@changed = changed
return self
end
|
#eql?(other) ⇒ Boolean
229
230
231
232
233
234
|
# File 'lib/db/model/record.rb', line 229
def eql?(other)
return false unless self.class.eql?(other.class)
return false unless key = self.persisted?
return key.eql?(other.persisted?)
end
|
#hash ⇒ Object
236
237
238
239
240
241
242
|
# File 'lib/db/model/record.rb', line 236
def hash
if key = self.persisted?
key.hash
else
raise KeyError, "Record is not persisted!"
end
end
|
#initialize(context, attributes, cache = nil) ⇒ Object
155
156
157
158
159
160
|
# File 'lib/db/model/record.rb', line 155
def initialize(context, attributes, cache = nil)
@context = context
@attributes = attributes
@changed = changed
@cache = cache
end
|
#inspect ⇒ Object
171
172
173
174
175
176
177
|
# File 'lib/db/model/record.rb', line 171
def inspect
if @changed&.any?
"\#<#{self.class.type} #{@attributes.inspect} changed=#{@changed.inspect}>"
else
to_s
end
end
|
#new_record? ⇒ Boolean
198
199
200
|
# File 'lib/db/model/record.rb', line 198
def new_record?
!persisted?
end
|
#persisted? ⇒ Boolean
A record which has a valid primary key is considered to be persisted.
192
193
194
195
196
|
# File 'lib/db/model/record.rb', line 192
def persisted?
self.class.key_columns.map do |field|
@attributes[field] or return false
end
end
|
#reload(context = @context) ⇒ Object
185
186
187
188
189
|
# File 'lib/db/model/record.rb', line 185
def reload(context = @context)
if key = self.persisted?
self.class.find(context, *key)
end
end
|
#save(context: @context) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/db/model/record.rb', line 202
def save(context: @context)
return unless attributes = self.flatten!
if key = persisted?
statement = Statement::Update.new(self.class,
Statement::Assignment.new(attributes),
self.class.find_predicate(*key)
)
else
statement = Statement::Insert.new(self.class,
Statement::Fields.new(attributes.keys),
Statement::Tuple.new(attributes.values)
)
end
statement.call(@context) do |attributes|
@attributes.update(attributes)
end
return self
end
|
#scope(model, attributes) ⇒ Object
225
226
227
|
# File 'lib/db/model/record.rb', line 225
def scope(model, attributes)
Scope.new(@context, model, attributes, @cache)
end
|
#to_s ⇒ Object
167
168
169
|
# File 'lib/db/model/record.rb', line 167
def to_s
"\#<#{self.class.type} #{@attributes.inspect}>"
end
|