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

#attributesObject (readonly)

Returns the value of attribute attributes.



146
147
148
# File 'lib/db/model/record.rb', line 146

def attributes
  @attributes
end

#cacheObject (readonly)

Returns the value of attribute cache.



148
149
150
# File 'lib/db/model/record.rb', line 148

def cache
  @cache
end

#changedObject (readonly)

Returns the value of attribute changed.



147
148
149
# File 'lib/db/model/record.rb', line 147

def changed
  @changed
end

#contextObject (readonly)

Returns the value of attribute context.



145
146
147
# File 'lib/db/model/record.rb', line 145

def context
  @context
end

Class Method Details

.included(klass) ⇒ Object



134
135
136
# File 'lib/db/model/record.rb', line 134

def self.included(klass)
	klass.extend(Base)
end

Instance Method Details

#assign(changed) ⇒ Object



162
163
164
165
166
# File 'lib/db/model/record.rb', line 162

def assign(changed)
	@changed = changed
	
	return self
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
215
216
217
# File 'lib/db/model/record.rb', line 212

def eql?(other)
	return false unless self.class.eql?(other.class)
	return false unless key = self.persisted?
	
	return key.eql?(other.persisted?)
end

#hashObject



219
220
221
222
223
224
225
# File 'lib/db/model/record.rb', line 219

def hash
	if key = self.persisted?
		key.hash
	else
		raise KeyError, "Record is not persisted!"
	end
end

#initialize(context, attributes, cache = nil) ⇒ Object



138
139
140
141
142
143
# File 'lib/db/model/record.rb', line 138

def initialize(context, attributes, cache = nil)
	@context = context
	@attributes = attributes
	@changed = changed
	@cache = cache
end

#inspectObject



154
155
156
157
158
159
160
# File 'lib/db/model/record.rb', line 154

def inspect
	if @changed&.any?
		"\#<#{self.class.type} #{@attributes.inspect} changed=#{@changed.inspect}>"
	else
		to_s
	end
end

#new_record?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/db/model/record.rb', line 181

def new_record?
	!persisted?
end

#persisted?Boolean

A record which has a valid primary key is considered to be persisted.

Returns:

  • (Boolean)


175
176
177
178
179
# File 'lib/db/model/record.rb', line 175

def persisted?
	self.class.key_columns.map do |field|
		@attributes[field] or return false
	end
end

#reload(context = @context) ⇒ Object



168
169
170
171
172
# File 'lib/db/model/record.rb', line 168

def reload(context = @context)
	if key = self.persisted?
		self.class.find(context, *key)
	end
end

#save(context: @context) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/db/model/record.rb', line 185

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|
		# Only insert will hit this code path:
		@attributes.update(attributes)
	end
	
	return self
end

#scope(model, attributes) ⇒ Object



208
209
210
# File 'lib/db/model/record.rb', line 208

def scope(model, attributes)
	Scope.new(@context, model, attributes, @cache)
end

#to_sObject



150
151
152
# File 'lib/db/model/record.rb', line 150

def to_s
	"\#<#{self.class.type} #{@attributes.inspect}>"
end