Class: FunWith::Gems::LibraryRequirer

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_with/gems/library_requirer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.require(*args, &block) ⇒ Object



5
6
7
# File 'lib/fun_with/gems/library_requirer.rb', line 5

def self.require( *args, &block )
  self.new.require( *args, &block )
end

Instance Method Details

#constant_name_to_filename(cname) ⇒ Object

We're guessing. It doesn't have to be perfect, but perfect 99% of the time would be nice



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fun_with/gems/library_requirer.rb', line 29

def constant_name_to_filename( cname )
  cname.split("::").map{ |chunk|
    buffer = []
    chunk.length.times do |i|
      char = chunk[i]
      next_char = chunk[i+1] || :eof
      
      cap_letter = char == char.upcase
      next_cap_letter = next_char == next_char.upcase
      
      if cap_letter && ! next_cap_letter && i > 0
        buffer << "_"
      end
      
      buffer << char
    end
    
              # chunk.gsub( /(.)([A-Z])/ ){ $1 + "_" + $2 }.downcase
    buffer.join("").downcase
              
  }.join("/") + ".rb"
    
end

#guess_caller_file(const) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/fun_with/gems/library_requirer.rb', line 19

def guess_caller_file( const )
  if const.respond_to?(:root)
    const_name = constant_name_to_filename( const.name )
    "lib/" + const_name
  else
    false
  end
end

#require(const, opts) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/fun_with/gems/library_requirer.rb', line 9

def require( const, opts )
  caller_file = opts[:caller] || guess_caller_file_name( const )
  files_to_require = const.root( "lib" ).glob(:all, :recursive => false) - [ caller_file ]
  files_to_require.flatten!
  
  for file in files_to_require
    file.requir
  end
end