Class: NRSER::Props::Storage::InstanceVariables

Inherits:
Object
  • Object
show all
Defined in:
lib/nrser/props/storage/instance_variables.rb

Overview

TODO:

document NRSER::Props::Storage::Key module.

Instance Method Summary collapse

Constructor Details

#initialize(immutable:) ⇒ InstanceVariables

Returns a new instance of InstanceVariables.



33
34
35
# File 'lib/nrser/props/storage/instance_variables.rb', line 33

def initialize immutable:
  @immutable = !!immutable
end

Instance Method Details

#get(instance, prop) ⇒ Object



43
44
45
# File 'lib/nrser/props/storage/instance_variables.rb', line 43

def get instance, prop
  instance.instance_variable_get "@#{ prop.name }"
end

#immutable?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/nrser/props/storage/instance_variables.rb', line 38

def immutable?
  @immutable
end

#put(instance, prop, value) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nrser/props/storage/instance_variables.rb', line 48

def put instance, prop, value
  if immutable?
    raise RuntimeError.new binding.erb <<~END
      Properties of #{ instance.class.safe_name } are immutable.
      
      Tried to set prop #{ prop.name } to value
      
          <%= value.pretty_inspect %>
      
      in instance
      
          <%= instance.pretty_inspect %>
      
    END
  end
  
  instance.instance_variable_set "@#{ prop.name }", value
end