Sha256: 73c6abe8cf26b49c53b403eb6e41351f49198386560c353f130ff5e57a06fc0a

Contents?: true

Size: 1.37 KB

Versions: 46

Compression:

Stored size: 1.37 KB

Contents

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@/, '')
      class_eval do # class_eval is necessary on 1.9 or else the methods a made private
        # use native attr_* methods as they are faster on some Ruby implementations
        send("attr_#{type}", internal_name)
      end
      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

46 entries across 46 versions & 4 rubygems

Version Path
motion-support-1.2.1 motion/core_ext/module/attr_internal.rb
motion-support-1.1.1 motion/core_ext/module/attr_internal.rb
motion-support-1.2.0 motion/core_ext/module/attr_internal.rb
motion-support-1.1.0 motion/core_ext/module/attr_internal.rb
motion-support-1.0.0 motion/core_ext/module/attr_internal.rb
motion-support-0.3.0 motion/core_ext/module/attr_internal.rb
motion_blender-support-0.2.8 motion/core_ext/module/attr_internal.rb
motion_blender-support-0.2.7 motion/core_ext/module/attr_internal.rb
activesupport-4.0.13 lib/active_support/core_ext/module/attr_internal.rb
activesupport-4.0.13.rc1 lib/active_support/core_ext/module/attr_internal.rb
activesupport-4.0.11.1 lib/active_support/core_ext/module/attr_internal.rb
activesupport-4.0.12 lib/active_support/core_ext/module/attr_internal.rb
activesupport-4.0.11 lib/active_support/core_ext/module/attr_internal.rb
activesupport-4.0.10 lib/active_support/core_ext/module/attr_internal.rb
activesupport-4.0.10.rc2 lib/active_support/core_ext/module/attr_internal.rb
activesupport-4.0.10.rc1 lib/active_support/core_ext/module/attr_internal.rb
activesupport-4.0.9 lib/active_support/core_ext/module/attr_internal.rb
activesupport-4.0.8 lib/active_support/core_ext/module/attr_internal.rb
activesupport-4.0.7 lib/active_support/core_ext/module/attr_internal.rb
activesupport-4.0.6 lib/active_support/core_ext/module/attr_internal.rb