Module: Using

Extended by:
DefaultLoadSchemes
Defined in:
lib/using.rb

Defined Under Namespace

Modules: DefaultLoadSchemes

Constant Summary collapse

LOAD_SCHEMES =
[:require, :load, :autoload]

Instance Method Summary collapse

Methods included from DefaultLoadSchemes

default_load_scheme, default_load_scheme=, reset_default_load_scheme!

Instance Method Details

#load_schemeObject



27
28
29
# File 'lib/using.rb', line 27

def load_scheme
  @load_scheme ||= ::Using.default_load_scheme
end

#load_scheme=(load_scheme) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/using.rb', line 31

def load_scheme=(load_scheme)
  if LOAD_SCHEMES.include?(load_scheme)
    @load_scheme = load_scheme
  else
    raise LoadError, "#{load_scheme} is not a valid load method"
  end
end

#using(const_name, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/using.rb', line 47

def using(const_name, options={})
  path = File.expand_path(caller(1)[0].gsub(/\.rb.*$/, ""))
  file_name = options[:file] || const_name.to_s.pathize

  case load_scheme
    when :require   then require  File.join(path, file_name)
    when :load      then load     File.join(path, "#{file_name}.rb")
    when :autoload  then autoload const_name, File.join(path, file_name)
  end
end

#with_load_scheme(scheme) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/using.rb', line 39

def with_load_scheme(scheme)
  old_scheme = load_scheme
  self.load_scheme = scheme
  yield
ensure
  self.load_scheme = old_scheme
end