Module: Relaxo::Model::Document

Includes:
Comparable
Defined in:
lib/relaxo/model/document.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(child) ⇒ Object



50
51
52
53
# File 'lib/relaxo/model/document.rb', line 50

def self.included(child)
	child.send(:include, Component)
	child.send(:extend, ClassMethods)
end

Instance Method Details

#<=>(other) ⇒ Object

Equality is done only on id to improve performance.



223
224
225
# File 'lib/relaxo/model/document.rb', line 223

def <=> other
	self.id <=> other.id if other
end

#==(other) ⇒ Object



231
232
233
# File 'lib/relaxo/model/document.rb', line 231

def == other
	self.attributes == other.attributes if other
end

#after_createObject

Set any default values:



219
220
# File 'lib/relaxo/model/document.rb', line 219

def after_create
end

#after_deleteObject



199
200
# File 'lib/relaxo/model/document.rb', line 199

def after_delete
end

#after_fetchObject

Raises:



214
215
216
# File 'lib/relaxo/model/document.rb', line 214

def after_fetch
	raise TypeError.new(self) unless valid_type?
end

#after_saveObject



115
116
# File 'lib/relaxo/model/document.rb', line 115

def after_save
end

#before_deleteObject



196
197
# File 'lib/relaxo/model/document.rb', line 196

def before_delete
end

#before_save(changeset) ⇒ Object

Update any calculations:



112
113
# File 'lib/relaxo/model/document.rb', line 112

def before_save(changeset)
end

#delete(changeset) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/relaxo/model/document.rb', line 202

def delete(changeset)
	before_delete
	
	@changed.clear
	
	paths.each do |path|
		changeset.delete(path)
	end

	after_delete
end

#dup(dataset = @dataset) ⇒ Object

Make a copy of the record, as if calling create.



132
133
134
135
136
137
138
139
# File 'lib/relaxo/model/document.rb', line 132

def dup(dataset = @dataset)
	# Splat already calls dup internally I guess.
	clone = self.class.new(dataset, nil, @changed.dup, **@attributes)
	
	clone.after_create
	
	return clone
end

#empty?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/relaxo/model/document.rb', line 239

def empty?
	@attributes.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/relaxo/model/document.rb', line 227

def eql? other
	self.id.eql?(other.id) if other
end

#hashObject



235
236
237
# File 'lib/relaxo/model/document.rb', line 235

def hash
	self.id.hash
end

#inspectObject



127
128
129
# File 'lib/relaxo/model/document.rb', line 127

def inspect
	"\#<#{self.class}:#{self.id} #{self.attributes.inspect}>"
end

#new_record?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/relaxo/model/document.rb', line 95

def new_record?
	!persisted?
end

#pathsObject



141
142
143
144
145
146
147
148
# File 'lib/relaxo/model/document.rb', line 141

def paths
	return to_enum(:paths) unless block_given?
	
	self.class.keys.each do |name, key|
		# @attributes is not modified until we call self.dump (which flattens @attributes into @changes). When we generate paths, we want to ensure these are done based on the non-mutable state of the object.
		yield key.object_path(self, **@attributes)
	end
end

#persisted?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/relaxo/model/document.rb', line 99

def persisted?
	@object != nil
end

#save(changeset) ⇒ Object

Save the model object.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/relaxo/model/document.rb', line 151

def save(changeset)
	before_save(changeset)
	
	return if persisted? and @changed.empty?
	
	if errors = self.validate(changeset)
		return errors
	end
	
	existing_paths = persisted? ? paths.to_a : []
	
	# Write data, check if any actual changes made:
	object = changeset.append(self.dump)
	return if object == @object
	
	existing_paths.each do |path|
		changeset.delete(path)
	end
	
	paths do |path|
		if changeset.exist?(path)
			raise KeyError, "Dataset already contains path: #{path}, when inserting #{@attributes.inspect}"
		end
			
		changeset.write(path, object)
	end
	
	@dataset = changeset
	@object = object
	
	after_save
	
	return true
end

#save!(changeset) ⇒ Object



186
187
188
189
190
191
192
193
194
# File 'lib/relaxo/model/document.rb', line 186

def save!(changeset)
	result = self.save(changeset)
	
	if result != true
		raise ValidationFailure.new(self, result)
	end
	
	return self
end

#to_sObject

The canonical path to the object in the data store, assuming there is some unique way to identify the object.



119
120
121
122
123
124
125
# File 'lib/relaxo/model/document.rb', line 119

def to_s
	if primary_key = self.class.primary_key
		primary_key.object_path(self)
	else
		super
	end
end

#typeObject



103
104
105
# File 'lib/relaxo/model/document.rb', line 103

def type
	@attributes[:type]
end

#valid_type?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/relaxo/model/document.rb', line 107

def valid_type?
	self.type == self.class.type
end