-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
struct.rb
51 lines (41 loc) 路 1.45 KB
/
struct.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true
# typed: true
class T::InexactStruct
include T::Props
include T::Props::Serializable
include T::Props::Constructor
end
class T::Struct < T::InexactStruct
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::Struct and cannot be subclassed"
end
end
end
class T::ImmutableStruct < T::InexactStruct
extend T::Sig
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
# Matches the one in WeakConstructor, but freezes the object
sig {params(hash: T::Hash[Symbol, T.untyped]).void.checked(:never)}
def initialize(hash={})
super
freeze
end
# Matches the signature in Props, but raises since this is an immutable struct and only const is allowed
sig {params(name: Symbol, cls: T.untyped, rules: T.untyped).void}
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
def with(changed_props)
raise "Cannot use `with` in #{self.class.name} because it is an immutable struct"
end
end