Sha256: e53400b7386dfed7aaeb116819ff12bcbb43c41228ad4ed3e529a153f221a6ef

Contents?: true

Size: 988 Bytes

Versions: 2

Compression:

Stored size: 988 Bytes

Contents

# frozen_string_literal: true

module Yattho
  # :nodoc:
  module Forms
    # :nodoc:
    module Utils
      # Unfortunately this bug (https://github.com/ruby/ruby/pull/5646) prevents us from using
      # Ruby's native Module.const_source_location. Instead we have to fudge it by searching
      # for the file in the configured autoload paths. Doing so relies on Rails' autoloading
      # conventions, so it should work ok. Zeitwerk also has this information but lacks a
      # public API to map constants to source files.
      def const_source_location(class_name)
        # NOTE: underscore respects namespacing, i.e. will convert Foo::Bar to foo/bar.
        class_path = "#{class_name.underscore}.rb"

        ActiveSupport::Dependencies.autoload_paths.each do |autoload_path|
          absolute_path = File.join(autoload_path, class_path)
          return absolute_path if File.exist?(absolute_path)
        end

        nil
      end
    end

    Utils.extend(Utils)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yattho_view_components-0.1.1 lib/yattho/forms/utils.rb
yattho_view_components-0.0.1 lib/yattho/forms/utils.rb