Class: Terrestrial::Configurations::ConventionalConfiguration::TimestampObserver

Inherits:
Object
  • Object
show all
Defined in:
lib/terrestrial/configurations/conventional_configuration.rb

Instance Method Summary collapse

Constructor Details

#initialize(clock, dirty_map, created_at_field, created_at_setter, updated_at_field, updated_at_setter) ⇒ TimestampObserver

Returns a new instance of TimestampObserver.



409
410
411
412
413
414
415
416
# File 'lib/terrestrial/configurations/conventional_configuration.rb', line 409

def initialize(clock, dirty_map, created_at_field, created_at_setter, updated_at_field, updated_at_setter)
  @clock = clock
  @dirty_map = dirty_map
  @created_at_field = created_at_field
  @created_at_setter = created_at_setter
  @updated_at_field = updated_at_field
  @updated_at_setter = updated_at_setter
end

Instance Method Details

#post_save(mapping, object, record, new_record) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
# File 'lib/terrestrial/configurations/conventional_configuration.rb', line 433

def post_save(mapping, object, record, new_record)
  if created_at_field && record.fetch(created_at_field, false)
    time = record.fetch(created_at_field)
    created_at_setter.call(object, time)
  end

  if updated_at_field
    time = record.get(updated_at_field)
    updated_at_setter.call(object, time)
  end
end

#post_serialize(mapping, object, record) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
# File 'lib/terrestrial/configurations/conventional_configuration.rb', line 421

def post_serialize(mapping, object, record)
  time = clock.now

  if created_at_field && !record.get(created_at_field)
    record.set(created_at_field, time)
  end

  if updated_at_field && dirty_map.dirty?(record)
    record.set(updated_at_field, time)
  end
end