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.



34
35
36
37
38
# File 'lib/types/struct.rb', line 34

def initialize(hash={})
  super

  freeze
end

Class Method Details

.inherited(subclass) ⇒ Object



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

def self.inherited(subclass)
  super(subclass)

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

.prop(name, cls, rules = {}) ⇒ Object



42
43
44
45
46
# File 'lib/types/struct.rb', line 42

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



48
49
50
# File 'lib/types/struct.rb', line 48

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