Module: Shaf::ImmutableAttr

Included in:
Formable::Builder::FormWrapper, Formable::Field, Formable::Form
Defined in:
lib/shaf/immutable_attr.rb

Constant Summary collapse

NON_DUPABLE_CLASSES =
[
  NilClass,
  TrueClass,
  FalseClass,
  Symbol
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dup(obj) ⇒ Object



10
11
12
13
14
# 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

Instance Method Details

#immutable_accessor(*methods) ⇒ Object



36
37
38
39
# File 'lib/shaf/immutable_attr.rb', line 36

def immutable_accessor(*methods)
  immutable_reader(*methods)
  immutable_writer(*methods)
end

#immutable_reader(*methods) ⇒ Object



16
17
18
19
20
21
22
23
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# 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