Class: NRSER::Props::Storage::Key

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

Overview

TODO:

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

Instance Method Summary collapse

Constructor Details

#initialize(immutable:, key_type:, get: :[], put: :[]=) ⇒ Key

Returns a new instance of Key.



33
34
35
36
37
38
# File 'lib/nrser/props/storage/key.rb', line 33

def initialize immutable:, key_type:, get: :[], put: :[]=
  @immutable = !!immutable
  @key_type = key_type
  @get_method_name = get
  @put_method_name = put
end

Instance Method Details

#get(instance, prop) ⇒ Object



58
59
60
# File 'lib/nrser/props/storage/key.rb', line 58

def get instance, prop
  instance.send @get_method_name, key_for( prop )
end

#immutable?Boolean

Instance Methods

Returns:

  • (Boolean)


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

def immutable?
  @immutable
end

#key_for(prop) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/nrser/props/storage/key.rb', line 48

def key_for prop
  case @key_type
  when :name
    prop.name
  when :index
    prop.index
  end
end

#put(instance, prop, value) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/nrser/props/storage/key.rb', line 63

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