class method self.attribute

Ruby on Rails 7.0.10

Since v5.2.8.1

Available in: v5.2.8.1 v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.4 v8.0.5.1 v8.1.4 edge

Signature

self.attribute(*names)

Declares one or more attributes that will be given both class and instance accessor methods.

Parameters

names rest
Source
# File activesupport/lib/active_support/current_attributes.rb, line 100
      def attribute(*names)
        ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |owner|
          names.each do |name|
            owner.define_cached_method(name, namespace: :current_attributes) do |batch|
              batch <<
                "def #{name}" <<
                "attributes[:#{name}]" <<
                "end"
            end
            owner.define_cached_method("#{name}=", namespace: :current_attributes) do |batch|
              batch <<
                "def #{name}=(value)" <<
                "attributes[:#{name}] = value" <<
                "end"
            end
          end
        end

        ActiveSupport::CodeGenerator.batch(singleton_class, __FILE__, __LINE__) do |owner|
          names.each do |name|
            owner.define_cached_method(name, namespace: :current_attributes_delegation) do |batch|
              batch <<
                "def #{name}" <<
                "instance.#{name}" <<
                "end"
            end
            owner.define_cached_method("#{name}=", namespace: :current_attributes_delegation) do |batch|
              batch <<
                "def #{name}=(value)" <<
                "instance.#{name} = value" <<
                "end"
            end
          end
        end
      end

Defined in activesupport/lib/active_support/current_attributes.rb line 100 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::CurrentAttributes

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close