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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 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.[ sym ]
      sym
    else
      key
    end
  else
    key
  end
end

#dupObject



186
187
188
189
190
# 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 = {}) ⇒ Object



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
104
105
106
# 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..
    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.



196
197
198
# File 'lib/nrser/props/mutable/stash.rb', line 196

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.



174
175
176
177
178
179
180
181
182
183
# File 'lib/nrser/props/mutable/stash.rb', line 174

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 (Hash)

Returns:

  • (Hash<String, *>)

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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