Module: Faulty::ImmutableOptions

Overview

A struct that cannot be modified after initialization

Instance Method Summary collapse

Instance Method Details

#initialize(hash) {|self| ... } ⇒ Object

Parameters:

  • hash (Hash)

    A hash of attributes to initialize with

Yields:

  • (self)

    Yields itself to the block to set options before freezing


8
9
10
11
12
13
14
15
16
# File 'lib/faulty/immutable_options.rb', line 8

def initialize(hash)
  defaults.merge(hash).each { |key, value| self[key] = value }
  yield self if block_given?
  finalize
  required.each do |key|
    raise ArgumentError, "Missing required attribute #{key}" if self[key].nil?
  end
  freeze
end