Sha256: 2288272ffcb82300634f05f94f8ac1976ce8f5589eba9c9635ac763429e2aad8

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module ROM
  module Plugins
    module Schema
      # A plugin for automatically adding timestamp fields
      # to the schema definition
      #
      # @example
      #   schema do
      #     use :timestamps
      #   end
      #
      #   # using non-default names
      #   schema do
      #     use :timestamps, attributes: %i(created_on updated_on)
      #   end
      #
      #   # using other types
      #   schema do
      #     use :timestamps, type: Types::Date
      #   end
      #
      # @api public
      module Timestamps
        DEFAULT_TIMESTAMPS = %i[created_at updated_at].freeze
        DEFAULT_TYPE = ROM::Types::Time

        # @api private
        def self.apply(schema, **options)
          attributes = options.fetch(:attributes, DEFAULT_TIMESTAMPS)
          attrs_type = options.fetch(:type, DEFAULT_TYPE)

          attributes.each do |name|
            schema.attribute(name, attrs_type)
          end

          schema
        end

        # @api private
        module DSL
          # Sets non-default timestamp attributes
          #
          # @example
          #   schema do
          #     use :timestamps
          #     timestamps :create_on, :updated_on
          #   end
          #
          # @api public
          def timestamps(*names)
            plugin(:timestamps, attributes: names).apply
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-6.0.0.alpha1 lib/rom/plugins/schema/timestamps.rb