Module: Transmutation::ClassAttributes Private

Included in:
Transmutation, Serializer
Defined in:
lib/transmutation/class_attributes.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

API:

  • private

Instance Method Summary collapse

Instance Method Details

#class_attribute(*names, instance_accessor: true, instance_reader: instance_accessor, instance_writer: instance_accessor, default: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



6
7
8
9
10
11
12
13
14
15
# File 'lib/transmutation/class_attributes.rb', line 6

def class_attribute(
  *names,
  instance_accessor: true,
  instance_reader: instance_accessor,
  instance_writer: instance_accessor,
  default: nil
)
  class_attribute_reader(*names, instance_reader:, default:)
  class_attribute_writer(*names, instance_writer:, default:)
end

#class_attribute_reader(*names, instance_reader: true, default: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/transmutation/class_attributes.rb', line 17

def class_attribute_reader(*names, instance_reader: true, default: nil)
  names.each do |name|
    self.class.define_method(name) do
      instance_variable_get("@#{name}")
    end

    define_method(name) { self.class.send(name) } if instance_reader

    instance_variable_set("@#{name}", default)
  end
end

#class_attribute_writer(*names, instance_writer: true, default: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/transmutation/class_attributes.rb', line 29

def class_attribute_writer(*names, instance_writer: true, default: nil)
  names.each do |name|
    self.class.define_method("#{name}=") do |value|
      instance_variable_set("@#{name}", value)
    end

    define_method("#{name}=") { |value| self.class.send("#{name}=", value) } if instance_writer

    instance_variable_set("@#{name}", default)
  end
end