Module: ActiveModel::Attributes::ClassMethods

Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, cast_type = nil, default: NO_DEFAULT_PROVIDED, **options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attributes.rb', line 19

def attribute(name, cast_type = nil, default: NO_DEFAULT_PROVIDED, **options)
  name = name.to_s

  cast_type = Type.lookup(cast_type, **options) if Symbol === cast_type
  cast_type ||= attribute_types[name]

  self.attribute_types = attribute_types.merge(name => cast_type)
  define_default_attribute(name, default, cast_type)
  define_attribute_method(name)
end

#attribute_namesObject

Returns an array of attribute names as strings

class Person
  include ActiveModel::Attributes

  attribute :name, :string
  attribute :age, :integer
end

Person.attribute_names
# => ["name", "age"]


41
42
43
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/attributes.rb', line 41

def attribute_names
  attribute_types.keys
end