Class: RFunk::Immutable

Inherits:
Object
  • Object
show all
Defined in:
lib/rfunk/attribute/immutable.rb

Instance Method Summary collapse

Instance Method Details

#create(options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rfunk/attribute/immutable.rb', line 3

def create(options)
  instance = options.fetch(:instance)
  variable_name = options.fetch(:variable_name)
  value = options.fetch(:value)

  instance.class.new.tap { |object|
    instance.instance_variables.select { |v| v != variable_name }.each { |v|
      previous_value = instance.instance_variable_get(v)
      object.instance_variable_set(v, previous_value)
    }

    object.instance_variable_set(variable_name, value)
    object.deep_freeze if should_deep_freeze?(value)
  }
end