Sha256: e9daf80fa9ee9589be9da039f93a2106724bdeee8220c02c4f031027e44d99e9

Contents?: true

Size: 1.52 KB

Versions: 10

Compression:

Stored size: 1.52 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

        # @api private
        def self.apply(schema, type: Types::Time, attributes: DEFAULT_TIMESTAMPS)
          attrs = attributes.map do |name|
            ROM::Schema.build_attribute_info(
              type.meta(source: schema.name),
              name: name
            )
          end

          schema.attributes.concat(
            schema.class.attributes(attrs, schema.attr_class)
          )
        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)
            options = plugin_options(:timestamps)
            options[:attributes] = names unless names.empty?

            self
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rom-core-5.4.0 lib/rom/plugins/schema/timestamps.rb
rom-core-5.3.2 lib/rom/plugins/schema/timestamps.rb
rom-core-5.3.1 lib/rom/plugins/schema/timestamps.rb
rom-core-5.3.0 lib/rom/plugins/schema/timestamps.rb
rom-core-5.2.6 lib/rom/plugins/schema/timestamps.rb
rom-core-5.2.5 lib/rom/plugins/schema/timestamps.rb
rom-core-5.2.4 lib/rom/plugins/schema/timestamps.rb
rom-core-5.2.3 lib/rom/plugins/schema/timestamps.rb
rom-core-5.2.2 lib/rom/plugins/schema/timestamps.rb
rom-core-5.2.1 lib/rom/plugins/schema/timestamps.rb