Module: Dnsruby::RR::GenericConstantInspection

Defined in:
lib/dnsruby/resource/generic_constants.rb

Constant Summary collapse

NATIVE_DEFINED =
Module.instance_method(:const_defined?)
NATIVE_GET =
Module.instance_method(:const_get)
NATIVE_ANCESTORS =
Module.instance_method(:ancestors)
EMPTY =
Module.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.native_defined?(receiver, path, inherit, nested = false) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
# File 'lib/dnsruby/resource/generic_constants.rb', line 9

def self.native_defined?(receiver, path, inherit, nested = false)
  if nested
    first = path.split('::', -1).first
    NATIVE_DEFINED.bind_call(EMPTY, first, false) # Validate; never reinterpret :: as an absolute suffix.
    ancestors = inherit ? NATIVE_ANCESTORS.bind_call(receiver) : [receiver]
    ancestors = ancestors.take_while { |ancestor| !ancestor.equal?(Object) } unless receiver.equal?(Object)
    return false unless ancestors.any? { |ancestor| NATIVE_DEFINED.bind_call(ancestor, first, false) }
  end
  NATIVE_DEFINED.bind_call(receiver, path, inherit)
end

.nested_defined?(receiver, path, inherit) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/dnsruby/resource/generic_constants.rb', line 20

def self.nested_defined?(receiver, path, inherit)
  native_defined?(receiver, path, inherit, true) || virtual_defined?(receiver, path, inherit, true)
end

.virtual_defined?(receiver, path, inherit, nested = false) ⇒ Boolean

Returns:

  • (Boolean)


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
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dnsruby/resource/generic_constants.rb', line 24

def self.virtual_defined?(receiver, path, inherit, nested = false)
  parts = path.split('::', -1)
  parts.each_index do |index|
    prefix = parts[0..index].join('::')
    next if prefix.empty? && index.zero?
    next if native_defined?(receiver, prefix, inherit, nested)
    owner = if index.zero?
      receiver
    elsif index == 1 && parts.first.empty?
      Object
    else
      NATIVE_GET.bind_call(receiver, parts[0...index].join('::'), inherit)
    end
    generic = Dnsruby::RR::Generic
    return false unless owner.equal?(generic) || (inherit && owner.is_a?(Class) && owner < generic)
    local = parts[index]
    found = GenericCache.named(local)
    numeric = /\AType(\d+)_Class(\d+)\z/.match(local)
    return false unless found || numeric
    return true if index == parts.length - 1
    suffix = parts[(index + 1)..].join('::')
    if found
      return nested_defined?(found, suffix, inherit)
    end
    type, klass = numeric.captures.map(&:to_i)
    registered = Dnsruby::ClassHash[[type, klass]] || GenericCache.peek(type, klass)
    if registered
      raise TypeError, "#{path} does not refer to class/module" unless registered.is_a?(Module)
      return nested_defined?(registered, suffix, inherit)
    end
    NATIVE_DEFINED.bind_call(EMPTY, suffix, false) # Preserve delimiter validation before a projected value.
    # These are the only constants every newly reconstructed Class owns.
    if ['TypeValue', 'ClassValue'].include?(parts[index + 1])
      return true if index + 2 == parts.length
      raise TypeError, "#{path} does not refer to class/module"
    end
    ancestor = Dnsruby::RR::ClassInsensitiveTypes[type] || generic
    return nested_defined?(inherit ? ancestor : EMPTY, suffix, inherit)
  end
  false
end

Instance Method Details

#const_defined?(name, inherit = true) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
# File 'lib/dnsruby/resource/generic_constants.rb', line 66

def const_defined?(name, inherit = true)
  unless name.is_a?(String) || name.is_a?(Symbol)
    original = name
    name = String.try_convert(name)
    raise TypeError, "no implicit conversion of #{original.class} into String" unless name
  end
  return true if super(name, inherit)
  path = name.is_a?(Symbol) ? name.to_s : String.new(name)
  GenericConstantInspection.virtual_defined?(self, path, inherit)
end