Sha256: d90a70caac44d1c3ad83cd5082e136ad4c42126843dc77bdab480d604334ae24

Contents?: true

Size: 762 Bytes

Versions: 4

Compression:

Stored size: 762 Bytes

Contents

class Module
  # Works like <tt>attr_accessor</tt> but generates private getter/setter
  # methods for class internal use only. Useful for enforcing Smalltalk-style
  # internal encapsulation.
  #
  # For a more complete implementation of a similar feature see {ivar_encapsulation}[https://github.com/citizen428/ivar_encapsulation].
  def private_accessor(*names)
    names.each do |name|
      instance_var_name = "@#{name}"

      define_method(name) do
        instance_variable_get(instance_var_name)
      end

      define_method("#{name}=") do |value|
        instance_variable_set(instance_var_name, value)
      end

      send(:private, name)
      send(:private, "#{name}=")
    end
    nil # like attr_accessor
  end

  private :private_accessor
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shenanigans-1.0.17 lib/shenanigans/module/private_accessor.rb
shenanigans-1.0.16 lib/shenanigans/module/private_accessor.rb
shenanigans-1.0.15 lib/shenanigans/module/private_accessor.rb
shenanigans-1.0.14 lib/shenanigans/module/private_accessor.rb