Class: JsonSchemaView::SchemaSet::ConstantSearchHelper::ConstantAutoLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/json_schema_view/schema_set/constant_search_helper.rb

Overview

Load corresponding constant for the path by using Rails’s autoloader.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(autoload_paths:) ⇒ ConstantAutoLoader

Returns a new instance of ConstantAutoLoader.

Parameters:

  • autoload_paths (Array<String>)


49
50
51
# File 'lib/json_schema_view/schema_set/constant_search_helper.rb', line 49

def initialize(autoload_paths:)
  @autoload_paths = autoload_paths.map { |path| Pathname.new(path) }
end

Instance Attribute Details

#autoload_pathsArray<Pathname> (readonly)

Returns:

  • (Array<Pathname>)


46
47
48
# File 'lib/json_schema_view/schema_set/constant_search_helper.rb', line 46

def autoload_paths
  @autoload_paths
end

Class Method Details

.from_rails_appConstantAutoLoader

Returns:



35
36
37
38
39
40
41
42
43
# File 'lib/json_schema_view/schema_set/constant_search_helper.rb', line 35

def self.from_rails_app
  autoload_paths = [
    *Rails.application.config.eager_load_paths,
    *Rails.application.config.autoload_once_paths,
    *Rails.application.config.autoload_paths
  ]

  new(autoload_paths: autoload_paths)
end

Instance Method Details

#load_constant_by_path(path) ⇒ Object

Parameters:

  • path (String, Pathname)

Returns:

  • (Object)


55
56
57
58
59
60
61
62
63
# File 'lib/json_schema_view/schema_set/constant_search_helper.rb', line 55

def load_constant_by_path(path)
  autoload_base_path = nearest_autoload_path(path)
  return nil unless autoload_base_path

  rel_path = child_path(path, autoload_base_path)
  return nil unless rel_path

  ActiveSupport::Inflector.safe_constantize(constant_name_from_path(rel_path))
end