Module: PolyDelegate::ClassicAttributeAccess

Included in:
Delegated::ClassMethods
Defined in:
lib/poly_delegate/classic_access.rb

Overview

Provide overloads of attr_reader, attr_writer, and attr_accessor.

These overloads give access to the state of the instance using instance_variable_get and instance_variable_set.

instance_variable_get and instance_variable_set can be overloaded on a class or instance level to provide additional functionality when accessing attributes.

If you do choose to overload instance_variable_get and instance_variable_set, keep in mind that __instance_variable_get__ and __instance_variable_set__ will still maintain the original functionality of these methods.

Class Method Summary collapse

Class Method Details

.attr_accessor(method_name, ...) ⇒ void (private)

This method returns an undefined value.

Parameters:

  • method_name (Symbol, String)

    An accessible attribute

  • ... (Symbol, String)

    More accessible attributes



56
57
58
59
# File 'lib/poly_delegate/classic_access.rb', line 56

def attr_accessor(*args)
  attr_reader(*args)
  attr_writer(*args)
end

.attr_reader(method_name, ...) ⇒ void (private)

This method returns an undefined value.

Parameters:

  • method_name (Symbol, String)

    A readable attribute

  • ... (Symbol, String)

    More readable attributes



30
31
32
33
34
35
36
# File 'lib/poly_delegate/classic_access.rb', line 30

def attr_reader(*args)
  args.each do |method|
    define_method(method) do
      instance_variable_get(:"@#{method}")
    end
  end
end

.attr_writer(method_name, ...) ⇒ void (private)

This method returns an undefined value.

Parameters:

  • method_name (Symbol, String)

    A writable attribute

  • ... (Symbol, String)

    More writable attributes



43
44
45
46
47
48
49
# File 'lib/poly_delegate/classic_access.rb', line 43

def attr_writer(*args)
  args.each do |method|
    define_method("#{method}=") do |value|
      instance_variable_set(:"@#{method}", value)
    end
  end
end