class ROM::Plugins::Command::Timestamps

A plugin for automatically adding timestamp values when executing a command

Set up attributes to timestamp when the command is called

@example

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

@api public

Attributes

datestamps[R]
timestamps[R]

Public Class Methods

new(timestamps: [], datestamps: []) click to toggle source
# File lib/rom/plugins/command/timestamps.rb, line 27
def initialize(timestamps: [], datestamps: [])
  @timestamps = store_attributes(timestamps)
  @datestamps = store_attributes(datestamps)
end

Public Instance Methods

included(klass) click to toggle source

@api private

Calls superclass method
# File lib/rom/plugins/command/timestamps.rb, line 38
def included(klass)
  initialize_timestamp_attributes(klass)
  klass.include(InstanceMethods)
  klass.extend(ClassInterface)
  super
end
initialize_timestamp_attributes(klass) click to toggle source
# File lib/rom/plugins/command/timestamps.rb, line 45
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) click to toggle source

@api private

# File lib/rom/plugins/command/timestamps.rb, line 33
def store_attributes(attr)
  attr.is_a?(Array) ? attr : Array[attr]
end