Class: Yoda::Store::Query::FindConstant

Inherits:
Base
  • Object
show all
Defined in:
lib/yoda/store/query/find_constant.rb

Instance Attribute Summary

Attributes inherited from Base

#registry

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Yoda::Store::Query::Base

Instance Method Details

#find(path) ⇒ Objects::Base?

Parameters:

Returns:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/yoda/store/query/find_constant.rb', line 7

def find(path)
  lexical_scope_paths = lexical_scopes_of(path)
  base_name, *constant_names = path_of(path).split
  # if path start with '::', base_name becomes ''.
  base_namespace_key = base_name == '' ? 'Object' : base_name
  base_namespace = select_base_namespace(base_namespace_key, lexical_scope_paths).first

  if constant_names.empty?
    # When the path does not contain separator (`::`)
    base_namespace
  else
    if base_namespace
      find_constant(constant_names.join('::'), base_namespace)
    else
      nil
    end
  end
end

#select_by_base_and_pattern(base:, pattern:) ⇒ Enumerator<Objects::Base>

Parameters:

  • base (Store::Object)
  • pattern (RegExp)

Returns:



29
30
31
# File 'lib/yoda/store/query/find_constant.rb', line 29

def select_by_base_and_pattern(base:, pattern:)
  select_constants_from_ancestors(base, pattern)
end

#select_with_prefix(path) ⇒ Array<Objects::Base>

Parameters:

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/yoda/store/query/find_constant.rb', line 35

def select_with_prefix(path)
  lexical_scope_paths = lexical_scopes_of(path)
  base_name, *constant_names, bottom_name = path_of(path).split

  if constant_names.empty? && !bottom_name
    # When the path does not contain separator (`::`)
    select_base_namespace(/\A#{Regexp.escape(base_name || '')}/, lexical_scope_paths).to_a.uniq
  else
    base_namespace = select_base_namespace(base_name, lexical_scope_paths).first
    scope = find_constant(constant_names.join('::'), base_namespace)
    return [] unless scope
    select_constants_from_ancestors(scope, /\A#{bottom_name}/).to_a
  end
end