Sha256: 66cda82ac18750b18d2ef3cc0fa2c63adc26cef2f59fc2791add1e001e14e4d0

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 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:, default:)
      class_attribute_writer(*names, instance_writer:, 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

3 entries across 3 versions & 1 rubygems

Version Path
transmutation-0.4.5 lib/transmutation/class_attributes.rb
transmutation-0.4.4 lib/transmutation/class_attributes.rb
transmutation-0.4.1 lib/transmutation/class_attributes.rb