Module: NRSER::Props::Mutable::Stash::InstanceMethods

Defined in:
lib/nrser/props/mutable/stash.rb

Overview

Instance methods to mix in with Hash.

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

Instance Method Summary collapse

Instance Method Details

#convert_key(key) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/nrser/props/mutable/stash.rb', line 145

def convert_key key
  case key
  when Symbol
    key
  when String
    sym = key.to_sym
    if self.[ sym ]
      sym
    else
      key
    end
  else
    key
  end
end

#dupObject



183
184
185
186
187
# File 'lib/nrser/props/mutable/stash.rb', line 183

def dup
  self.class.new( self ).tap do |new_stash|
    set_defaults new_stash
  end
end

#initialize_props(values = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nrser/props/mutable/stash.rb', line 76

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..
    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..prop_names
  
  values.each do |key, value|
    unless prop_names.include? convert_key( key )
      self[key] = value
    end
  end
end

#merge(other_hash = {}, &block) ⇒ Object

Need to patch ‘#merge` since InstanceMethods defines it, which overrides Stash#merge, so we just put it back.



193
194
195
# File 'lib/nrser/props/mutable/stash.rb', line 193

def merge other_hash = {}, &block
  dup.update other_hash, &block
end

#put(key, value) ⇒ VALUE

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

Parameters:

Returns:

  • (VALUE)

    The stored value.



171
172
173
174
175
176
177
178
179
180
# File 'lib/nrser/props/mutable/stash.rb', line 171

def put key, value
  key = convert_key key
  
  if (prop = self.class.[ 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) ⇒ Hash<String, *>

Override InstanceMethods#to_data to handle non-prop values in the Stash.

Parameters:

  • only_props: (Boolean) (defaults to: false)

    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).

  • **kwds

Returns:

  • (Hash<String, *>)

    Map of property names as strings to their “data” value, plus the special class identifier key and value, if requested.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/nrser/props/mutable/stash.rb', line 120

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