module Faulty::ImmutableOptions

A struct that cannot be modified after initialization

Public Class Methods

new(hash) { |self| ... } click to toggle source

@param hash [Hash] A hash of attributes to initialize with @yield [self] Yields itself to the block to set options before freezing

# 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

Private Instance Methods

defaults() click to toggle source

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

@return [Hash<Symbol, Object>]

# File lib/faulty/immutable_options.rb, line 23
def defaults
  {}
end
finalize() click to toggle source

Runs before freezing to finalize attribute initialization

@return [void]

# File lib/faulty/immutable_options.rb, line 37
def finalize
end
required() click to toggle source

An array of required attributes

@return [Array<Symbol>]

# File lib/faulty/immutable_options.rb, line 30
def required
  []
end