Module: T::Props::Utils

Defined in:
lib/types/props/utils.rb

Overview

typed: true

Class Method Summary collapse

Class Method Details

.deep_clone(what) ⇒ Object

deep_clone_object with freeze: false. Kept kwarg-free, with String (the most common serialized scalar) tested first: this is what generated serializers/deserializers emit for dynamic fallbacks, so it runs per element of every untyped container prop.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/types/props/utils.rb', line 15

def self.deep_clone(what)
  case what
  when String
    what.clone
  when true, false, Symbol, NilClass, Numeric
    what
  when Array
    what.map { |v| deep_clone(v) }
  when Hash
    h = what.class.new
    what.each_pair do |k, v|
      h[k] = deep_clone(v)
    end
    h
  when Regexp
    what.dup
  when T::Enum
    what
  else
    what.clone
  end
end

.deep_clone_freeze(what) ⇒ Object

deep_clone_object with freeze: true.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/types/props/utils.rb', line 39

def self.deep_clone_freeze(what)
  result = case what
  when true, false, Symbol, NilClass, Numeric
    what
  when Array
    what.map { |v| deep_clone_freeze(v) }
  when Hash
    h = what.class.new
    what.each_pair do |k, v|
      k.freeze
      h[k] = deep_clone_freeze(v)
    end
    h
  when Regexp
    what.dup
  when T::Enum
    what
  else
    what.clone
  end
  result.freeze
end

.deep_clone_object(what, freeze: false) ⇒ Object

Deep copy an object. The object must consist of Ruby primitive types and Hashes and Arrays.



7
8
9
# File 'lib/types/props/utils.rb', line 7

def self.deep_clone_object(what, freeze: false)
  freeze ? deep_clone_freeze(what) : deep_clone(what)
end

.merge_serialized_optional_rule(prop_rules) ⇒ Object



84
85
86
# File 'lib/types/props/utils.rb', line 84

def self.merge_serialized_optional_rule(prop_rules)
  {'_tnilable' => true}.merge(prop_rules.merge('_tnilable' => true))
end

.need_nil_read_check?(prop_rules) ⇒ Boolean

The prop_rules indicate whether we should check for reading a nil value for the prop/field. This is mostly for the compatibility check that we allow existing documents carry some nil prop/field.

Returns:



64
65
66
67
# File 'lib/types/props/utils.rb', line 64

def self.need_nil_read_check?(prop_rules)
  # . :on_load allows nil read, but we need to check for the read for future writes
  prop_rules[:optional] == :on_load || prop_rules[:raise_on_nil_write]
end

.need_nil_write_check?(prop_rules) ⇒ Boolean

The prop_rules indicate whether we should check for writing a nil value for the prop/field.

Returns:



70
71
72
# File 'lib/types/props/utils.rb', line 70

def self.need_nil_write_check?(prop_rules)
  need_nil_read_check?(prop_rules) || T::Props::Utils.required_prop?(prop_rules)
end

.optional_prop?(prop_rules) ⇒ Boolean

Returns:



79
80
81
82
# File 'lib/types/props/utils.rb', line 79

def self.optional_prop?(prop_rules)
  # Clients should never reference :_tnilable as the implementation can change.
  !!prop_rules[:_tnilable]
end

.required_prop?(prop_rules) ⇒ Boolean

Returns:



74
75
76
77
# File 'lib/types/props/utils.rb', line 74

def self.required_prop?(prop_rules)
  # Clients should never reference :_tnilable as the implementation can change.
  !prop_rules[:_tnilable]
end