Sha256: 8cbfb8a3c219eef4c4145e86b8034877aac1aa84d0cb4fdbbec136468a0cbbaa

Contents?: true

Size: 1.18 KB

Versions: 10

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Transmutation
  # @api private
  module ClassAttributes
    def class_attribute(
      *names,
      instance_accessor: true,
      instance_reader: instance_accessor,
      instance_writer: instance_accessor,
      default: nil
    )
      class_attribute_reader(*names, instance_reader: instance_reader, default: default)
      class_attribute_writer(*names, instance_writer: instance_writer, default: default)
    end

    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

    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
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
transmutation-0.3.6 lib/transmutation/class_attributes.rb
transmutation-0.3.5 lib/transmutation/class_attributes.rb
transmutation-0.3.4 lib/transmutation/class_attributes.rb
transmutation-0.3.3 lib/transmutation/class_attributes.rb
transmutation-0.3.1 lib/transmutation/class_attributes.rb
transmutation-0.3.0 lib/transmutation/class_attributes.rb
transmutation-0.2.3 lib/transmutation/class_attributes.rb
transmutation-0.2.2 lib/transmutation/class_attributes.rb
transmutation-0.2.1 lib/transmutation/class_attributes.rb
transmutation-0.2.0 lib/transmutation/class_attributes.rb