Module: Strict::Method

Defined in:
lib/strict/method.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(mod) ⇒ Object



5
6
7
8
9
# File 'lib/strict/method.rb', line 5

def self.extended(mod)
  return if mod.singleton_class?

  mod.singleton_class.extend(self)
end

Instance Method Details

#method_added(method_name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/strict/method.rb', line 53

def method_added(method_name)
  super

  sig = singleton_class.instance_variable_get(:@__strict_method_internal_last_sig_configuration)
  singleton_class.instance_variable_set(:@__strict_method_internal_last_sig_configuration, nil)
  return unless sig

  verifiable_method = Methods::VerifiableMethod.new(
    method: instance_method(method_name),
    parameters: sig.parameters,
    returns: sig.returns,
    instance: true
  )
  verifiable_method.verify_definition!
  strict_instance_methods[method_name] = verifiable_method
  prepend(Methods::Module.new(verifiable_method))
end

#sig(&block) ⇒ Object



11
12
13
14
# File 'lib/strict/method.rb', line 11

def sig(&block)
  instance = singleton_class? ? self : singleton_class
  instance.instance_variable_set(:@__strict_method_internal_last_sig_configuration, Methods::Dsl.run(&block))
end

#singleton_method_added(method_name) ⇒ Object

rubocop:disable Metrics/MethodLength



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/strict/method.rb', line 35

def singleton_method_added(method_name)
  super

  sig = singleton_class.instance_variable_get(:@__strict_method_internal_last_sig_configuration)
  singleton_class.instance_variable_set(:@__strict_method_internal_last_sig_configuration, nil)
  return unless sig

  verifiable_method = Methods::VerifiableMethod.new(
    method: singleton_class.instance_method(method_name),
    parameters: sig.parameters,
    returns: sig.returns,
    instance: false
  )
  verifiable_method.verify_definition!
  strict_class_methods[method_name] = verifiable_method
  singleton_class.prepend(Methods::Module.new(verifiable_method))
end

#strict_class_methodsObject



16
17
18
19
20
21
22
23
# File 'lib/strict/method.rb', line 16

def strict_class_methods
  instance = singleton_class? ? self : singleton_class
  if instance.instance_variable_defined?(:@__strict_method_internal_class_methods)
    instance.instance_variable_get(:@__strict_method_internal_class_methods)
  else
    instance.instance_variable_set(:@__strict_method_internal_class_methods, {})
  end
end

#strict_instance_methodsObject



25
26
27
28
29
30
31
32
# File 'lib/strict/method.rb', line 25

def strict_instance_methods
  instance = singleton_class? ? self : singleton_class
  if instance.instance_variable_defined?(:@__strict_method_internal_instance_methods)
    instance.instance_variable_get(:@__strict_method_internal_instance_methods)
  else
    instance.instance_variable_set(:@__strict_method_internal_instance_methods, {})
  end
end