Sha256: 08c158e1c05e0122490254ad7739b7d0e6770447f423e2da25baf10aa4562307

Contents?: true

Size: 1.31 KB

Versions: 106

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

class Module
  # Declares an attribute reader backed by an internally-named instance variable.
  def attr_internal_reader(*attrs)
    attrs.each { |attr_name| attr_internal_define(attr_name, :reader) }
  end

  # Declares an attribute writer backed by an internally-named instance variable.
  def attr_internal_writer(*attrs)
    attrs.each { |attr_name| attr_internal_define(attr_name, :writer) }
  end

  # Declares an attribute reader and writer backed by an internally-named instance
  # variable.
  def attr_internal_accessor(*attrs)
    attr_internal_reader(*attrs)
    attr_internal_writer(*attrs)
  end
  alias_method :attr_internal, :attr_internal_accessor

  class << self; attr_accessor :attr_internal_naming_format end
  self.attr_internal_naming_format = "@_%s"

  private
    def attr_internal_ivar_name(attr)
      Module.attr_internal_naming_format % attr
    end

    def attr_internal_define(attr_name, type)
      internal_name = attr_internal_ivar_name(attr_name).delete_prefix("@")
      # use native attr_* methods as they are faster on some Ruby implementations
      public_send("attr_#{type}", internal_name)
      attr_name, internal_name = "#{attr_name}=", "#{internal_name}=" if type == :writer
      alias_method attr_name, internal_name
      remove_method internal_name
    end
end

Version data entries

106 entries across 100 versions & 15 rubygems

Version Path
activesupport-7.1.5 lib/active_support/core_ext/module/attr_internal.rb
activesupport-7.1.4.2 lib/active_support/core_ext/module/attr_internal.rb
activesupport-7.0.8.6 lib/active_support/core_ext/module/attr_internal.rb
activesupport-6.1.7.10 lib/active_support/core_ext/module/attr_internal.rb
activesupport-6.1.7.9 lib/active_support/core_ext/module/attr_internal.rb
activesupport-7.1.4.1 lib/active_support/core_ext/module/attr_internal.rb
activesupport-7.0.8.5 lib/active_support/core_ext/module/attr_internal.rb
activesupport-7.1.4 lib/active_support/core_ext/module/attr_internal.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/core_ext/module/attr_internal.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/module/attr_internal.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/module/attr_internal.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/module/attr_internal.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/module/attr_internal.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/module/attr_internal.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/module/attr_internal.rb
activesupport-7.1.3.4 lib/active_support/core_ext/module/attr_internal.rb
activesupport-7.0.8.4 lib/active_support/core_ext/module/attr_internal.rb
activesupport-6.1.7.8 lib/active_support/core_ext/module/attr_internal.rb
activesupport-7.1.3.2 lib/active_support/core_ext/module/attr_internal.rb
activesupport-7.1.3.1 lib/active_support/core_ext/module/attr_internal.rb