module ROM::Model::Attributes::ClassMethods

Class extensions for an attributes class

@api public

Constants

DEFAULT_TIMESTAMPS

Default timestamp attribute names used by `timestamps` method

Public Instance Methods

[](input) click to toggle source

Process input and return attributes instance

@example

class UserAttributes
  include ROM::Model::Attributes

  attribute :name, String
end

UserAttributes[name: 'Jane']

@param [Hash,#to_hash] input The input params

@return [Attributes]

@api public

# File lib/rom/model/attributes.rb, line 78
def [](input)
  input.is_a?(self) ? input : new(input)
end
set_model_name(name) click to toggle source

Macro for defining ActiveModel::Name object on the attributes class

This is essential for rails helpers to work properly when generating form input names etc.

@example

class UserAttributes
  include ROM::Model::Attributes

  set_model_name 'User'
end

@return [undefined]

@api public

# File lib/rom/model/attributes.rb, line 97
        def set_model_name(name)
          class_eval <<-RUBY
            def self.model_name
              @model_name ||= ActiveModel::Name.new(self, nil, #{name.inspect})
            end
          RUBY
        end
timestamps(*attrs) click to toggle source

Shortcut for defining timestamp attributes like created_at etc.

@example

class NewPostAttributes
  include ROM::Model::Attributes

  # provide name(s) explicitly
  timestamps :published_at

  # defaults to :created_at, :updated_at without args
  timestamps
end

@api public

# File lib/rom/model/attributes.rb, line 119
def timestamps(*attrs)
  if attrs.empty?
    DEFAULT_TIMESTAMPS.each do |t|
      attribute t, DateTime, default: proc { DateTime.now }
    end
  else
    attrs.each do |attr|
      attribute attr, DateTime, default: proc { DateTime.now }
    end
  end
end