Module: Sequel::Plugins::Bitemporal::InstanceMethods
- Defined in:
- lib/sequel/plugins/bitemporal.rb
Instance Attribute Summary collapse
-
#pending_version ⇒ Object
readonly
Returns the value of attribute pending_version.
Instance Method Summary collapse
- #after_create ⇒ Object
- #after_save ⇒ Object
- #attributes ⇒ Object
- #attributes=(attributes) ⇒ Object
- #audited? ⇒ Boolean
- #before_create ⇒ Object
- #before_update ⇒ Object
- #before_validation ⇒ Object
- #deleted? ⇒ Boolean
- #destroy ⇒ Object
- #destroy_version(version, expand_previous_version) ⇒ Object
- #last_version ⇒ Object
- #pending_or_current_version ⇒ Object
- #reload ⇒ Object
- #restore(attrs = {}) ⇒ Object
- #update_attributes(attributes = {}) ⇒ Object
- #validate ⇒ Object
Instance Attribute Details
#pending_version ⇒ Object (readonly)
Returns the value of attribute pending_version.
248 249 250 |
# File 'lib/sequel/plugins/bitemporal.rb', line 248 def pending_version @pending_version end |
Instance Method Details
#after_create ⇒ Object
306 307 308 309 310 311 312 |
# File 'lib/sequel/plugins/bitemporal.rb', line 306 def after_create super if @create_version @create_version = nil return false unless save_pending_version end end |
#after_save ⇒ Object
322 323 324 325 |
# File 'lib/sequel/plugins/bitemporal.rb', line 322 def after_save super _refresh_set_values @values end |
#attributes ⇒ Object
270 271 272 273 274 275 276 277 278 |
# File 'lib/sequel/plugins/bitemporal.rb', line 270 def attributes if pending_version pending_version.values elsif current_version current_version.values else initial_version.values end end |
#attributes=(attributes) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/sequel/plugins/bitemporal.rb', line 280 def attributes=(attributes) @pending_version ||= begin current_attributes = {master_id: id} current_version.keys.each do |key| next if excluded_columns.include? key current_attributes[key] = current_version.send key end if current_version? model.version_class.new current_attributes end pending_version.set_all attributes end |
#audited? ⇒ Boolean
250 251 252 |
# File 'lib/sequel/plugins/bitemporal.rb', line 250 def audited? !!self.class.audit_class end |
#before_create ⇒ Object
301 302 303 304 |
# File 'lib/sequel/plugins/bitemporal.rb', line 301 def before_create @create_version = pending_version_holds_changes? super end |
#before_update ⇒ Object
314 315 316 317 318 319 320 |
# File 'lib/sequel/plugins/bitemporal.rb', line 314 def before_update if pending_version_holds_changes? expire_previous_versions return false unless save_pending_version end super end |
#before_validation ⇒ Object
254 255 256 257 |
# File 'lib/sequel/plugins/bitemporal.rb', line 254 def before_validation prepare_pending_version super end |
#deleted? ⇒ Boolean
360 361 362 |
# File 'lib/sequel/plugins/bitemporal.rb', line 360 def deleted? !new? && !current_version end |
#destroy ⇒ Object
327 328 329 330 |
# File 'lib/sequel/plugins/bitemporal.rb', line 327 def destroy point_in_time = ::Sequel::Plugins::Bitemporal.point_in_time versions_dataset.where(expired_at: nil).where("valid_to>valid_from").update expired_at: point_in_time end |
#destroy_version(version, expand_previous_version) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/sequel/plugins/bitemporal.rb', line 332 def destroy_version(version, ) now = ::Sequel::Plugins::Bitemporal.now point_in_time = ::Sequel::Plugins::Bitemporal.point_in_time return false if version.valid_to.to_datetime<=now model.db.transaction do success = true version_was_valid = now>=version.valid_from.to_datetime if previous = versions_dataset.where({ expired_at: nil, valid_to: version.valid_from, }).where("valid_to>valid_from").first if previous if version_was_valid success &&= save_fossil previous, created_at: point_in_time, valid_from: now, valid_to: version.valid_to else success &&= save_fossil previous, created_at: point_in_time, valid_to: version.valid_to success &&= previous.update expired_at: point_in_time end end end success &&= save_fossil version, created_at: point_in_time, valid_to: now if version_was_valid success &&= version.update expired_at: point_in_time raise Sequel::Rollback unless success success end end |
#last_version ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/sequel/plugins/bitemporal.rb', line 364 def last_version @last_version ||= begin return if new? t = ::Sequel::Plugins::Bitemporal.point_in_time n = ::Sequel::Plugins::Bitemporal.now if use_ranges = self.class.use_ranges range_conditions = self.class.existence_range_contains t end versions_dataset.where do if use_ranges range_conditions else (created_at <= t) & Sequel.|({expired_at=>nil}, expired_at > t) end & (valid_from <= n) end.order(Sequel.desc(:valid_to), Sequel.desc(:created_at)).first end end |
#pending_or_current_version ⇒ Object
266 267 268 |
# File 'lib/sequel/plugins/bitemporal.rb', line 266 def pending_or_current_version pending_version || current_version || initial_version end |
#reload ⇒ Object
398 399 400 401 402 403 404 |
# File 'lib/sequel/plugins/bitemporal.rb', line 398 def reload @last_version = nil @current_version_values = nil @pending_version = nil @initial_version = nil super end |
#restore(attrs = {}) ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/sequel/plugins/bitemporal.rb', line 383 def restore(attrs={}) return false unless deleted? last_version_attributes = if last_version last_version.columns.each_with_object({}) do |column, hash| unless excluded_columns.include? column hash[column] = last_version.send column end end else {} end update_attributes last_version_attributes.merge attrs @last_version = nil end |
#update_attributes(attributes = {}) ⇒ Object
292 293 294 295 296 297 298 299 |
# File 'lib/sequel/plugins/bitemporal.rb', line 292 def update_attributes(attributes={}) self.attributes = attributes if save raise_on_failure: false self else false end end |
#validate ⇒ Object
259 260 261 262 263 264 |
# File 'lib/sequel/plugins/bitemporal.rb', line 259 def validate super pending_version.errors.each do |key, key_errors| key_errors.each{|error| errors.add key, error} end if pending_version_holds_changes? && !pending_version.valid? end |