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
avm-tools-0.116.2 sub/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.94.2 lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.94.1 lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.94.0 lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.93.0 lib/eac_ruby_utils/require_sub.rb
avm-tools-0.116.1 sub/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
avm-tools-0.116.0 sub/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.92.1 lib/eac_ruby_utils/require_sub.rb
avm-tools-0.115.0 sub/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.92.0 lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.91.0 lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.90.0 lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.89.0 lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.88.0 lib/eac_ruby_utils/require_sub.rb
avm-tools-0.114.2 sub/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
avm-tools-0.114.1 sub/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.87.0 lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.86.0 lib/eac_ruby_utils/require_sub.rb
avm-tools-0.114.0 sub/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb
eac_ruby_utils-0.85.0 lib/eac_ruby_utils/require_sub.rb