Class: T::Props::Private::ApplyDefault

Inherits:
Object
  • Object
show all
Extended by:
Helpers, Sig
Defined in:
lib/types/props/private/apply_default.rb

Constant Summary collapse

NO_CLONE_TYPES =
T.let([TrueClass, FalseClass, NilClass, Symbol, Numeric, T::Enum].freeze, T::Array[Module])

Constants included from Helpers

Helpers::Private

Instance Attribute Summary collapse

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!

Constructor Details

#initialize(accessor_key, setter_proc) ⇒ ApplyDefault

Returns a new instance of ApplyDefault.



17
18
19
20
# File 'lib/types/props/private/apply_default.rb', line 17

def initialize(accessor_key, setter_proc)
  @accessor_key = T.let(accessor_key, Symbol)
  @setter_proc = T.let(setter_proc, SetterFactory::SetterProc)
end

Instance Attribute Details

#setter_procObject (readonly)

Returns the value of attribute setter_proc.



13
14
15
# File 'lib/types/props/private/apply_default.rb', line 13

def setter_proc
  @setter_proc
end

Class Method Details

.for(cls, rules) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/types/props/private/apply_default.rb', line 34

def self.for(cls, rules)
  accessor_key = rules.fetch(:accessor_key)
  setter = rules.fetch(:setter_proc)

  if rules.key?(:factory)
    ApplyDefaultFactory.new(cls, rules.fetch(:factory), accessor_key, setter)
  elsif rules.key?(:default)
    default = rules.fetch(:default)
    case default
    when *NO_CLONE_TYPES
      return ApplyPrimitiveDefault.new(default, accessor_key, setter)
    when String
      if default.frozen?
        return ApplyPrimitiveDefault.new(default, accessor_key, setter)
      end
    when Array
      if default.empty? && default.class == Array
        return ApplyEmptyArrayDefault.new(accessor_key, setter)
      end
    when Hash
      if default.empty? && default.default.nil? && T.unsafe(default).default_proc.nil? && default.class == Hash
        return ApplyEmptyHashDefault.new(accessor_key, setter)
      end
    end

    ApplyComplexDefault.new(default, accessor_key, setter)
  else
    nil
  end
end

Instance Method Details

#defaultObject



24
# File 'lib/types/props/private/apply_default.rb', line 24

def default; end

#set_default(instance) ⇒ Object



28
# File 'lib/types/props/private/apply_default.rb', line 28

def set_default(instance); end