module NRSER::Props::Mutable::Stash::InstanceMethods

Instance methods to mix in with {NRSER::Props::Mutable::Hash}.

We want these to override {NRSER::Props::InstanceMethods}, so they need to be separate so we can include them after {NRSER::Props} in {NRSER::Props::Mutable::Hash.included}.

Public Instance Methods

convert_key(key) click to toggle source
# File lib/nrser/props/mutable/stash.rb, line 148
def convert_key key
  case key
  when Symbol
    key
  when String
    sym = key.to_sym
    if self.metadata[ sym ]
      sym
    else
      key
    end
  else
    key
  end
end
dup() click to toggle source
# File lib/nrser/props/mutable/stash.rb, line 186
def dup
  self.class.new( self ).tap do |new_stash|
    set_defaults new_stash
  end
end
initialize_props(values = {}) click to toggle source
# File lib/nrser/props/mutable/stash.rb, line 79
def initialize_props values = {}
  # Handles things like `[[:x, 1], [:y, 2]]`, since we know that's what is
  # meant in that case
  values = values.to_h unless values.respond_to?( :each_pair )
  
  self.class.metadata.
    each_primary_prop_value_from( values ) { |prop, value|
      _raw_put prop.name, value
    }
  
  # Check additional type invariants
  self.class.invariants.each do |type|
    type.check self
  end
  
  # Load in additional non-prop values, if any
  #
  # TODO  Optimize
  #
  
  prop_names = self.class.metadata.prop_names
  
  values.each do |key, value|
    unless prop_names.include? convert_key( key )
      self[key] = value
    end
  end
end
merge(other_hash = {}) click to toggle source

Need to patch `#merge` since {NRSER::Props::InstanceMethods} defines it, which overrides {NRSER::Stash#merge}, so we just put it back.

# File lib/nrser/props/mutable/stash.rb, line 196
def merge other_hash = {}, &block
  dup.update other_hash, &block
end
put(key, value) click to toggle source

Store a value at a key. If the key is a prop name, store it through the prop, which will check it's type.

@param [Symbol | String] key @param [VALUE] value

@return [VALUE]

The stored value.
# File lib/nrser/props/mutable/stash.rb, line 174
def put key, value
  key = convert_key key
  
  if (prop = self.class.metadata[ key ])
    prop.set self, value
  else
    # We know {#convert_value} is a no-op so can skip it
    _raw_put key, value
  end
end
to_data(only_props: false, **kwds) click to toggle source

Override {NRSER::Props::InstanceMethods#to_data} to handle non-prop values in the {NRSER::Stash}.

@param [Boolean] only_props

When `true` only prop values will be added to the data hash.

Otherwise, any non-prop keys and vales will be added as well
(default behavior).

@param [Hash] kwds

See {NRSER::Props::InstanceMethods#to_data}.

@return (see NRSER::Props::InstanceMethods#to_data)

Calls superclass method
# File lib/nrser/props/mutable/stash.rb, line 123
def to_data only_props: false, **kwds
  hash = super **kwds
  
  unless only_props
    each do |key, value|
      # Data uses **string** keys
      key = key.to_s
      
      # See if the key is missing
      unless hash.key?( key.to_s )
        # It is, so let's fill it in
        
        # If value knows how to be data, do that
        value = value.to_data if value.respond_to?( :to_data )
        
        # Set the key/value pair
        hash[key] = value
      end
    end
  end
  
  hash
end