Class: EacRubyUtils::RequireSub::Base

Inherits:
Object
  • Object
show all
Includes:
Listable
Defined in:
lib/eac_ruby_utils/require_sub/base.rb

Constant Summary collapse

INCLUDE_MODULES_MAP =
{
  nil => nil,
  false => nil,
  true => :include,
  include: :include,
  prepend: :prepend
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ Base

Returns a new instance of Base.



23
24
25
26
27
28
29
# File 'lib/eac_ruby_utils/require_sub/base.rb', line 23

def initialize(file, options = {})
  @file = file
  @options = self.class.lists.option.hash_keys_validate!(options)
  return unless options[OPTION_REQUIRE_MODE]

  self.class.lists.require_mode.value_validate!(options[OPTION_REQUIRE_MODE])
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



21
22
23
# File 'lib/eac_ruby_utils/require_sub/base.rb', line 21

def file
  @file
end

#optionsObject (readonly)

Returns the value of attribute options.



21
22
23
# File 'lib/eac_ruby_utils/require_sub/base.rb', line 21

def options
  @options
end

Instance Method Details

#active_support_require?Boolean

Returns:



32
33
34
# File 'lib/eac_ruby_utils/require_sub/base.rb', line 32

def active_support_require?
  options[OPTION_REQUIRE_DEPENDENCY] ? true : false
end

#applyObject



36
37
38
39
# File 'lib/eac_ruby_utils/require_sub/base.rb', line 36

def apply
  require_sub_files
  include_modules
end

#baseObject



41
42
43
# File 'lib/eac_ruby_utils/require_sub/base.rb', line 41

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

#base?Boolean

Returns:



45
46
47
# File 'lib/eac_ruby_utils/require_sub/base.rb', line 45

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

#include_modulesObject



49
50
51
# File 'lib/eac_ruby_utils/require_sub/base.rb', line 49

def include_modules
  sub_files.each(&:include_module)
end

#include_or_prepend_methodObject

Raises:

  • (::ArgumentError)


53
54
55
56
57
58
59
# File 'lib/eac_ruby_utils/require_sub/base.rb', line 53

def include_or_prepend_method
  return INCLUDE_MODULES_MAP.fetch(options[OPTION_INCLUDE_MODULES]) if
  INCLUDE_MODULES_MAP.key?(options[OPTION_INCLUDE_MODULES])

  raise ::ArgumentError, "Invalid value for 'options[OPTION_INCLUDE_MODULES]': " \
                         "\"#{options[OPTION_INCLUDE_MODULES]}\""
end

#require_modeSymbol

Returns:

  • (Symbol)


62
63
64
65
66
67
68
# File 'lib/eac_ruby_utils/require_sub/base.rb', line 62

def require_mode
  return options[OPTION_REQUIRE_MODE] if options[OPTION_REQUIRE_MODE]
  return REQUIRE_MODE_ACTIVE_SUPPORT if active_support_require?
  return REQUIRE_MODE_AUTOLOAD if base?

  REQUIRE_MODE_KERNEL
end

#require_sub_filesObject



70
71
72
# File 'lib/eac_ruby_utils/require_sub/base.rb', line 70

def require_sub_files
  sub_files.each(&:require_file)
end

#sub_filesObject



74
75
76
77
# File 'lib/eac_ruby_utils/require_sub/base.rb', line 74

def sub_files
  @sub_files ||= Dir["#{File.dirname(file)}/#{::File.basename(file, '.*')}/*.rb"].sort
                   .map { |path| ::EacRubyUtils::RequireSub::SubFile.new(self, path) }
end