module Shaf::ImmutableAttr

Constants

NON_DUPABLE_CLASSES

Public Class Methods

dup(obj) click to toggle source
# File lib/shaf/immutable_attr.rb, line 10
def self.dup(obj)
  return obj unless obj.respond_to? :dup
  return obj if NON_DUPABLE_CLASSES.include? obj.class
  obj.dup
end

Public Instance Methods

immutable_accessor(*methods) click to toggle source
# File lib/shaf/immutable_attr.rb, line 36
def immutable_accessor(*methods)
  immutable_reader(*methods)
  immutable_writer(*methods)
end
immutable_reader(*methods) click to toggle source
# File lib/shaf/immutable_attr.rb, line 16
def immutable_reader(*methods)
  methods.each do |method|
    define_method(method) do
      value = instance_variable_get(:"@#{method}")
      ImmutableAttr.dup(value)
    end
  end
end
immutable_writer(*methods) click to toggle source
# File lib/shaf/immutable_attr.rb, line 25
def immutable_writer(*methods)
  methods.each do |method|
    define_method(:"#{method}=") do |value|
      instance_variable_set(
        :"@#{method}",
        ImmutableAttr.dup(value)
      )
    end
  end
end