Class: SleepingKingStudios::Docs::RegistryQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/sleeping_king_studios/docs/registry_query.rb

Overview

Checks for the presence of requested data in the YARD registry.

Instance Method Summary collapse

Constructor Details

#initialize(registry:) ⇒ RegistryQuery

Returns a new instance of RegistryQuery.

Parameters:

  • registry (Enumerable)

    the YARD registry.



9
10
11
# File 'lib/sleeping_king_studios/docs/registry_query.rb', line 9

def initialize(registry:)
  @registry = registry
end

Instance Method Details

#class_method_exists?(method_name) ⇒ Boolean

Checks if the given class method is defined in the registry.

Parameters:

  • method_name (String)

    the name of the method and defining namespace, if any.

Returns:

  • (Boolean)

    true if the class method exists, otherwise false.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sleeping_king_studios/docs/registry_query.rb', line 19

def class_method_exists?(method_name)
  # Handle top-level class methods.
  if method_name.start_with?('.')
    return top_level_class_method_exists?(method_name)
  end

  # Handle legacy ::class_method format.
  unless method_name.include?('.')
    return legacy_class_method_exists?(method_name)
  end

  registry.any? do |obj|
    obj.type == :method && obj.scope == :class && obj.title == method_name
  end
end

#constant_exists?(constant_name) ⇒ Boolean

Checks if the given constant is defined in the registry.

Parameters:

  • constant_name (String)

    the name of the constant and defining namespace, if any.

Returns:

  • (Boolean)

    true if the constant exists, otherwise false.



41
42
43
44
45
# File 'lib/sleeping_king_studios/docs/registry_query.rb', line 41

def constant_exists?(constant_name)
  registry.any? do |obj|
    obj.type == :constant && obj.title == constant_name
  end
end

#definition_exists?(module_name) ⇒ Boolean

Checks if the given class or module is defined in the registry.

Parameters:

  • module_name (String)

    the name of the class or module and defining namespace, if any.

Returns:

  • (Boolean)

    true if the class or module exists, otherwise false.



53
54
55
56
57
# File 'lib/sleeping_king_studios/docs/registry_query.rb', line 53

def definition_exists?(module_name)
  registry.any? do |obj|
    (obj.type == :module || obj.type == :class) && obj.title == module_name # rubocop:disable Style/MultipleComparison
  end
end

#instance_method_exists?(method_name) ⇒ Boolean

Checks if the given instance method is defined in the registry.

Parameters:

  • method_name (String)

    the name of the method and defining namespace, if any.

Returns:

  • (Boolean)

    true if the instance method exists, otherwise false.



65
66
67
68
69
70
71
# File 'lib/sleeping_king_studios/docs/registry_query.rb', line 65

def instance_method_exists?(method_name)
  registry.any? do |obj|
    obj.type == :method &&
      obj.scope == :instance &&
      obj.title == method_name
  end
end