Module: Faulty::ImmutableOptions

Overview

A struct that cannot be modified after initialization

Instance Method Summary collapse

Instance Method Details

#defaultsHash<Symbol, Object>

A hash of default values to set before yielding to the block

Returns:

  • (Hash<Symbol, Object>)


28
29
30
# File 'lib/faulty/immutable_options.rb', line 28

def defaults
  {}
end

#dup_with(hash, &block) ⇒ Object



12
13
14
# File 'lib/faulty/immutable_options.rb', line 12

def dup_with(hash, &block)
  dup.setup(hash, &block)
end

#finalizevoid

This method returns an undefined value.

Runs before freezing to finalize attribute initialization



42
43
# File 'lib/faulty/immutable_options.rb', line 42

def finalize
end

#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
# File 'lib/faulty/immutable_options.rb', line 8

def initialize(hash, &block)
  setup(defaults.merge(hash), &block)
end

#requiredArray<Symbol>

An array of required attributes

Returns:

  • (Array<Symbol>)


35
36
37
# File 'lib/faulty/immutable_options.rb', line 35

def required
  []
end

#setup(hash) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



16
17
18
19
20
21
22
23
# File 'lib/faulty/immutable_options.rb', line 16

def setup(hash)
  hash&.each { |key, value| self[key] = value }
  yield self if block_given?
  finalize
  guard_required!
  freeze
  self
end