Sha256: 880972acd23dc377c9f860cfb5763ef03138da7c92f6b938dd5e26c023aab86b

Contents?: true

Size: 1.54 KB

Versions: 26

Compression:

Stored size: 1.54 KB

Contents

require 'active_support/inflections'

module JSONAPIonify
  module Autoload
    def self.eager_load!
      load_tracker = {}
      while unloaded.present?
        unloaded.each do |mod, consts|
          consts.each do |const|
            tracker = [mod.name, const].join('::')
            begin
              mod.const_get(const, false)
            rescue NameError => e
              load_tracker[tracker] = load_tracker[tracker].to_i + 1
              if !e.message.include?('uninitialized constant') || load_tracker[tracker] > 50
                raise e
              end
            end
          end
        end
      end
    end

    def self.unloaded
      modules = ObjectSpace.each_object.select do |o|
        o.is_a?(Module)
      end
      modules.each_with_object({}) do |mod, hash|
        autoloadable_constants = mod.constants.each_with_object([]) do |const, ary|
          if mod.autoload?(const) && mod.autoload?(const).include?(__dir__)
            ary << const
          end
        end
        if autoloadable_constants.present?
          hash[mod] = autoloadable_constants
        end
      end
    end

    def autoload_all(dir=nil)
      file     = caller[0].split(/\:\d/)[0]
      base_dir = File.expand_path File.dirname(file)
      dir      ||= name.split('::').last.underscore
      Dir.glob("#{base_dir}/#{dir}/*.rb").each do |file|
        basename   = File.basename file, File.extname(file)
        fullpath   = File.expand_path file
        const_name = basename.camelize.to_sym
        autoload const_name, fullpath
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
jsonapionify-0.12.10 lib/jsonapionify/autoload.rb
jsonapionify-0.12.9 lib/jsonapionify/autoload.rb
jsonapionify-0.12.8 lib/jsonapionify/autoload.rb
jsonapionify-0.12.7 lib/jsonapionify/autoload.rb
jsonapionify-0.12.5 lib/jsonapionify/autoload.rb
jsonapionify-0.12.4 lib/jsonapionify/autoload.rb
jsonapionify-0.12.3 lib/jsonapionify/autoload.rb
jsonapionify-0.12.2 lib/jsonapionify/autoload.rb
jsonapionify-0.12.1 lib/jsonapionify/autoload.rb
jsonapionify-0.12.0 lib/jsonapionify/autoload.rb
jsonapionify-0.11.11 lib/jsonapionify/autoload.rb
jsonapionify-0.11.10 lib/jsonapionify/autoload.rb
jsonapionify-0.11.9 lib/jsonapionify/autoload.rb
jsonapionify-0.11.8 lib/jsonapionify/autoload.rb
jsonapionify-0.11.7 lib/jsonapionify/autoload.rb
jsonapionify-0.11.6 lib/jsonapionify/autoload.rb
jsonapionify-0.11.5 lib/jsonapionify/autoload.rb
jsonapionify-0.11.0 lib/jsonapionify/autoload.rb
jsonapionify-0.10.2 lib/jsonapionify/autoload.rb
jsonapionify-0.10.1 lib/jsonapionify/autoload.rb