Class: T::ImmutableStruct

Inherits:
InexactStruct show all
Extended by:
Sig
Defined in:
lib/types/struct.rb

Constant Summary

Constants included from Helpers

Helpers::Private

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sig

sig

Methods included from Helpers

#abstract!, #final!, #interface!, #mixes_in_class_methods, #requires_ancestor, #sealed!

Methods included from Props::Serializable

#deserialize, #serialize

Methods included from Props::PrettyPrintable

#inspect, #pretty_inspect, #pretty_print

Constructor Details

#initialize(hash = {}) ⇒ ImmutableStruct

Returns a new instance of ImmutableStruct.



36
37
38
39
40
# File 'lib/types/struct.rb', line 36

def initialize(hash={})
  super

  freeze
end

Class Method Details

.inherited(subclass) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/types/struct.rb', line 24

def self.inherited(subclass)
  super(subclass)

  original_method = subclass.singleton_class.instance_method(:inherited)
  T::Private::ClassUtils.replace_method(original_method, subclass.singleton_class, :inherited) do |s|
    super(s)
    raise "#{self.name} is a subclass of T::ImmutableStruct and cannot be subclassed"
  end
end

.prop(name, cls, **rules) ⇒ Object



44
45
46
47
48
# File 'lib/types/struct.rb', line 44

def self.prop(name, cls, **rules)
  return super if (cls.is_a?(Hash) && cls[:immutable]) || rules[:immutable]

  raise "Cannot use `prop` in #{self.name} because it is an immutable struct. Use `const` instead"
end

Instance Method Details

#with(changed_props) ⇒ Object



50
51
52
# File 'lib/types/struct.rb', line 50

def with(changed_props)
  raise "Cannot use `with` in #{self.class.name} because it is an immutable struct"
end