Sha256: 4bf00630109e47733f442b63a4aad49fe4252a4af4cabfef0775d5a1f27a2c3f
Contents?: true
Size: 1.18 KB
Versions: 2
Compression:
Stored size: 1.18 KB
Contents
# frozen_string_literal: true module Upgrow # A read-only Struct. An Immutable Struct is initialized with its member # values and subsequent state changes are not permitted. class ImmutableStruct < Struct class << self # Creates a new Immutable Struct class with the given members. # # @param args [Array<Symbol>] the list of members for the new class. # # @return [ImmutableStruct] the new Immutable Struct class able to # accommodate the given members. def new(*args, &block) if args.any? { |member| !member.is_a?(Symbol) } raise ArgumentError, 'all members must be symbols' end struct_class = super(*args, keyword_init: true, &block) struct_class.members.each do |member| struct_class.send(:undef_method, :"#{member}=") end struct_class end end undef []= # Initializes a new Immutable Struct with the given member values. # # @param args [Hash<Symbol, Object>] the list of values for each member of # the Immutable Struct. def initialize(**args) members.each { |key| args.fetch(key) } super(**args) freeze end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
upgrow-0.0.5 | lib/upgrow/immutable_struct.rb |
upgrow-0.0.4 | lib/upgrow/immutable_struct.rb |