Sha256: d5e95b7c630c1d040ea27b92e14e0338f211c616a20ed378bbe29bfac9e2a190

Contents?: true

Size: 1.81 KB

Versions: 185

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

require 'active_support/inflector'
require 'eac_ruby_utils/listable'

module EacRubyUtils
  class << self
    def require_sub(file, options = {})
      ::EacRubyUtils::RequireSub.new(file, options).apply
    end
  end

  class RequireSub
    include ::EacRubyUtils::Listable
    lists.add_symbol :option, :base, :include_modules, :require_dependency

    attr_reader :file, :options

    def initialize(file, options = {})
      @file = file
      @options = self.class.lists.option.hash_keys_validate!(options)
    end

    def apply
      require_sub_files
      include_modules
    end

    private

    def active_support_require(path)
      return false unless options[OPTION_REQUIRE_DEPENDENCY]

      ::Kernel.require_dependency(path)
      true
    end

    def autoload_require(path)
      return false unless base?

      basename = ::File.basename(path, '.*')
      return false if basename.start_with?('_')

      base.autoload ::ActiveSupport::Inflector.camelize(basename), path
      true
    end

    def include_modules
      return unless options[OPTION_INCLUDE_MODULES]

      base.constants.each do |constant_name|
        constant = base.const_get(constant_name)
        next unless constant.is_a?(::Module) && !constant.is_a?(::Class)

        base.include(constant)
      end
    end

    def base
      options[OPTION_BASE] || raise('Option :base not setted')
    end

    def base?
      options[OPTION_BASE] ? true : false
    end

    def kernel_require(path)
      ::Kernel.require(path)
    end

    def require_sub_files
      Dir["#{File.dirname(file)}/#{::File.basename(file, '.*')}/*.rb"].sort.each do |path|
        require_sub_file(path)
      end
    end

    def require_sub_file(path)
      active_support_require(path) || autoload_require(path) || kernel_require(path)
    end
  end
end

Version data entries

185 entries across 185 versions & 3 rubygems

Version Path
eac_ruby_utils-0.84.0 lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.83.0 lib/eac_ruby_utils/require_sub.rb
avm-tools-0.113.6 sub/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.82.1 lib/eac_ruby_utils/require_sub.rb
avm-tools-0.113.5 sub/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
avm-tools-0.113.4 sub/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
avm-tools-0.113.3 sub/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.82.0 lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.81.0 lib/eac_ruby_utils/require_sub.rb
avm-tools-0.113.2 sub/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.80.1 lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.80.0 lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.79.0 lib/eac_ruby_utils/require_sub.rb
ehbrs-tools-0.28.3 vendor/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.78.0 lib/eac_ruby_utils/require_sub.rb
avm-tools-0.110.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.77.1 lib/eac_ruby_utils/require_sub.rb
avm-tools-0.109.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.77.0 lib/eac_ruby_utils/require_sub.rb
avm-tools-0.109.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb