Class: ROM::Plugins::Command::Timestamps

Inherits:
Module
  • Object
show all
Defined in:
lib/rom/plugins/command/timestamps.rb

Overview

A plugin for automatically adding timestamp values when executing a command

Set up attributes to timestamp when the command is called

Examples:

class CreateTask < ROM::Commands::Create[:sql]
  result :one
  use :timestamps, timestamps: %i(created_at, updated_at), datestamps: %i(:written)
end

create_user = rom.command(:user).create.curry(name: 'Jane')

result = create_user.call
result[:created_at]  #=> Time.now.utc

Defined Under Namespace

Modules: ClassInterface, InstanceMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timestamps: [], datestamps: []) ⇒ Timestamps

Returns a new instance of Timestamps.



25
26
27
28
# File 'lib/rom/plugins/command/timestamps.rb', line 25

def initialize(timestamps: [], datestamps: [])
  @timestamps = store_attributes(timestamps)
  @datestamps = store_attributes(datestamps)
end

Instance Attribute Details

#datestampsObject (readonly)



24
25
26
# File 'lib/rom/plugins/command/timestamps.rb', line 24

def datestamps
  @datestamps
end

#timestampsObject (readonly)



24
25
26
# File 'lib/rom/plugins/command/timestamps.rb', line 24

def timestamps
  @timestamps
end

Instance Method Details

#included(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
39
40
41
# File 'lib/rom/plugins/command/timestamps.rb', line 36

def included(klass)
  initialize_timestamp_attributes(klass)
  klass.include(InstanceMethods)
  klass.extend(ClassInterface)
  super
end

#initialize_timestamp_attributes(klass) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/rom/plugins/command/timestamps.rb', line 43

def initialize_timestamp_attributes(klass)
  klass.defines :timestamp_columns, :datestamp_columns
  klass.timestamp_columns Set.new
  klass.datestamp_columns Set.new
  klass.before :set_timestamps
  klass.timestamp_columns klass.timestamp_columns.merge(timestamps) if timestamps.any?
  klass.datestamp_columns klass.datestamp_columns.merge(datestamps) if datestamps.any?
end

#store_attributes(attr) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



31
32
33
# File 'lib/rom/plugins/command/timestamps.rb', line 31

def store_attributes(attr)
  attr.is_a?(Array) ? attr : Array[attr]
end