Class: SmallCage::AnonymousLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/smallcage/anonymous_loader.rb

Overview

load files into anonymous module

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAnonymousLoader

Returns a new instance of AnonymousLoader.



6
7
8
9
# File 'lib/smallcage/anonymous_loader.rb', line 6

def initialize
  @module = Module.new
  @module_names = []
end

Instance Attribute Details

#moduleObject (readonly)

Returns the value of attribute module.



4
5
6
# File 'lib/smallcage/anonymous_loader.rb', line 4

def module
  @module
end

#module_namesObject (readonly)

Returns the value of attribute module_names.



4
5
6
# File 'lib/smallcage/anonymous_loader.rb', line 4

def module_names
  @module_names
end

Class Method Details

.load(dir, rex) ⇒ Object



26
27
28
29
30
# File 'lib/smallcage/anonymous_loader.rb', line 26

def self.load(dir, rex)
  loader = new
  loader.load_match_files(dir, rex)
  { :module => loader.module, :names => loader.module_names }
end

Instance Method Details

#load_match_files(dir, rex) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/smallcage/anonymous_loader.rb', line 11

def load_match_files(dir, rex)
  return unless File.exist?(dir)

  Dir.entries(dir).sort.each do |h|
    next unless h =~ rex

    module_name = Regexp.last_match(1).camelize
    path = File.join(dir, h)
    src = File.read(path)

    @module.module_eval(src, path)
    @module_names << module_name
  end
end