Module: SweetLoader

Extended by:
ClassMethods
Included in:
Module, Scope
Defined in:
lib/sweetloader.rb,
lib/sweetloader/scope.rb,
lib/sweetloader/class_methods.rb

Defined Under Namespace

Modules: ClassMethods Classes: InvalidAutoloadMode, Scope

Instance Attribute Summary

Attributes included from ClassMethods

#default_mode

Instance Method Summary collapse

Methods included from ClassMethods

mode, mode=, namespaces, namespaces=, root, root=, translate, valid_mode?, valid_modes

Instance Method Details

#autoload_modules(*args) ⇒ Object Also known as: autoload_module, sweetload



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sweetloader.rb', line 20

def autoload_modules *args
  alm_options = args.extract_options!
  alm_options.merge!(autoload_options) if respond_to? :autoload_options

  root = alm_options[:root] || AutoLoader.root || ''
  path = root.strip.empty? ? self.name.to_s.underscore : [root, self.name.to_s.underscore].join('/')
  from = alm_options[:from] || path
  proc = alm_options[:mutate_path]
  from = proc.call(from) if proc
      
  the_module = send(:the_module) if respond_to? :the_module
  the_module ||= self
  
  mode = alm_options[:mode] if AutoLoader.valid_mode? alm_options[:mode]
  mode ||= AutoLoader.mode
  
  logic = alm_options[:proc] if alm_options[:proc].respond_to? :call
  logic ||= mode_logic mode
  
  # Here also could be adding of the file in top of load_paths like: $:.unshift File.dirname(__FILE__)
  # It is very useful for situations of having not load_paths built Rails or Gems way.
  args.each do |module_name|
    ruby_file = module_name.to_s.underscore
    module_name = module_name.to_s.camelize.to_sym
    require_file = SweetLoader.translate("#{from}/#{ruby_file}", alm_options)
    logic.call(the_module, module_name, require_file)
  end
end

#autoload_scope(options = {}, &block) ⇒ Object Also known as: sweetload_scope, sweet_scope



62
63
64
65
66
# File 'lib/sweetloader.rb', line 62

def autoload_scope options = {}, &block
  if block_given?
    block.arity == 1 ? yield(self) : SweetLoader::Scope.new(self, options).instance_eval(&block)
  end
end

#include_and_extend(the_module, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/sweetloader.rb', line 10

def include_and_extend(the_module, options={})
  options[:instance_methods] ||= :InstanceMethods
  options[:class_methods] ||= :ClassMethods
  # Mainly include but be flexible
  main_module = const_get(the_module.to_s.to_sym)
  include main_module # for an extend_and_include method, change this to extend main_module
  include main_module.const_get(options[:instance_methods]) if main_module.const_defined?(options[:instance_methods])
  extend main_module.const_get(options[:class_methods]) if main_module.const_defined?(options[:class_methods])
end

#mode_logic(mode) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/sweetloader.rb', line 51

def mode_logic mode
  case mode
  when :autoload
    Proc.new { |the_module, module_name, require_file| the_module.send :autoload, module_name, require_file }
  when :require
    Proc.new {|the_module, module_name, require_file| require require_file }
  else
    raise InvalidAutoloadMode, "Not a valid Autloader mode: #{AutoLoader.mode}, must be one of: #{AutoLoader.valid_mode}"
  end
end