Skip to content

Атрибуты

Базовые

attribute

ruby
attribute :uuid, from: String, to: :id, as: String, format: :uuid
ruby
attribute :uuid, from: [String, NilClass], to: :id, as: [String, NilClass], format: :uuid, required: false

string

ruby
string! :uuid, to: :id
ruby
string? :uuid, to: :id

integer

ruby
integer! :rating, min: 1, max: 10
ruby
integer? :rating, min: 1, max: 10

float

ruby
float! :rating
ruby
float? :rating

boolean

ruby
boolean! :published
ruby
# не поддерживается

Опции

Для метода attribute доступны следующие опции:

  • from;
  • to;
  • as;
  • format;
  • min;
  • max.

Для хелперов эти опции также доступны, за исключением опции from.

О поддерживаемых значениях для format вы можете узнать здесь.

Хелперы

uuid

ruby
uuid! :id
ruby
string! :id, format: :uuid
ruby
uuid? :id
ruby
string? :id, format: :uuid

money

ruby
money! :box_office
ruby
integer! :box_office_cents
string! :box_office_currency
ruby
money? :box_office
ruby
integer? :box_office_cents
string? :box_office_currency

duration

ruby
duration! :episode_duration
ruby
attribute :episode_duration, from: String, as: ActiveSupport::Duration, format: { from: :duration }
ruby
duration? :episode_duration
ruby
attribute :episode_duration, from: [String, NilClass], as: [ActiveSupport::Duration, NilClass], format: { from: :duration }, required: false

date

ruby
date! :premiered_on
ruby
attribute :premiered_on, from: String, as: Date, format: { from: :date }
ruby
date? :premiered_on
ruby
attribute :premiered_on, from: [String, NilClass], as: [Date, NilClass], format: { from: :date }, required: false

time

ruby
time! :premiered_at
ruby
attribute :premiered_at, from: String, as: Time, format: { from: :time }
ruby
time? :premiered_at
ruby
attribute :premiered_at, from: [String, NilClass], as: [Time, NilClass], format: { from: :time }, required: false

datetime

ruby
datetime! :premiered_at
ruby
attribute :premiered_at, from: String, as: DateTime, format: { from: :datetime }
ruby
datetime? :premiered_at
ruby
attribute :premiered_at, from: [String, NilClass], as: [DateTime, NilClass], format: { from: :datetime }, required: false