class NRSER::Props::Storage::Key

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

Public Class Methods

new(immutable:, key_type:, get: :[], put: :[]=) click to toggle source
# 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

Public Instance Methods

get(instance, prop) click to toggle source
# File lib/nrser/props/storage/key.rb, line 58
def get instance, prop
  instance.send @get_method_name, key_for( prop )
end
immutable?() click to toggle source

Instance Methods

# File lib/nrser/props/storage/key.rb, line 43
def immutable?
  @immutable
end
key_for(prop) click to toggle source
# 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) click to toggle source
# 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