Class: JsonSchemaView::SchemaSet::ConstantSearchHelper::ConstantAutoLoader
- Inherits:
-
Object
- Object
- JsonSchemaView::SchemaSet::ConstantSearchHelper::ConstantAutoLoader
- 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
- #autoload_paths ⇒ Array<Pathname> readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(autoload_paths:) ⇒ ConstantAutoLoader
constructor
A new instance of ConstantAutoLoader.
- #load_constant_by_path(path) ⇒ Object
Constructor Details
#initialize(autoload_paths:) ⇒ ConstantAutoLoader
Returns a new instance of ConstantAutoLoader.
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_paths ⇒ Array<Pathname> (readonly)
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_app ⇒ ConstantAutoLoader
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
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 |