Class: HashMath::Record

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hash_math/record.rb

Overview

A Record serves as a prototype for a Hash. It will allow the output of hashes conforming to a strict (#make!) or non-strict (#make) shape.

Instance Method Summary collapse

Constructor Details

#initialize(keys = [], base_value = nil) ⇒ Record

Returns a new instance of Record.



18
19
20
21
22
# File 'lib/hash_math/record.rb', line 18

def initialize(keys = [], base_value = nil)
  @prototype = keys.map { |key| [key, base_value] }.to_h

  freeze
end

Instance Method Details

#make(hash = {}, bound = false) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/hash_math/record.rb', line 28

def make(hash = {}, bound = false)
  hash.each_with_object(shallow_copy_prototype) do |(key, value), memo|
    next unless assert_key_in_bounds(key, bound)

    memo[key] = value
  end
end

#make!(hash = {}) ⇒ Object



24
25
26
# File 'lib/hash_math/record.rb', line 24

def make!(hash = {})
  make(hash, true)
end