Sha256: 1bcf29da1459356d8d38eb8d2775fd713e90d1ec2f059502c06c49070a9b9376

Contents?: true

Size: 1.3 KB

Versions: 100

Compression:

Stored size: 1.3 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).sub(/\A@/, "")
      # use native attr_* methods as they are faster on some Ruby implementations
      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

100 entries across 91 versions & 12 rubygems

Version Path
activesupport-6.0.2.2 lib/active_support/core_ext/module/attr_internal.rb
activesupport-5.2.4.2 lib/active_support/core_ext/module/attr_internal.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/core_ext/module/attr_internal.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/core_ext/module/attr_internal.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.4.1/lib/active_support/core_ext/module/attr_internal.rb
activesupport-6.0.2.1 lib/active_support/core_ext/module/attr_internal.rb
activesupport-5.2.4.1 lib/active_support/core_ext/module/attr_internal.rb
activesupport-6.0.2 lib/active_support/core_ext/module/attr_internal.rb
activesupport-6.0.2.rc2 lib/active_support/core_ext/module/attr_internal.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/core_ext/module/attr_internal.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/core_ext/module/attr_internal.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/activesupport-6.0.1/lib/active_support/core_ext/module/attr_internal.rb
activesupport-5.2.4 lib/active_support/core_ext/module/attr_internal.rb
activesupport-6.0.2.rc1 lib/active_support/core_ext/module/attr_internal.rb
activesupport-5.2.4.rc1 lib/active_support/core_ext/module/attr_internal.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/activesupport-6.0.1/lib/active_support/core_ext/module/attr_internal.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/core_ext/module/attr_internal.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/core_ext/module/attr_internal.rb
activesupport-6.0.1 lib/active_support/core_ext/module/attr_internal.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activesupport-6.0.0/lib/active_support/core_ext/module/attr_internal.rb