Class: FunWith::Files::Requirements::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_with/files/requirements/manager.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Manager

Returns a new instance of Manager.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fun_with/files/requirements/manager.rb', line 9

def initialize( path )
  case path
  when Array
    @required_files = path
  when FilePath
    if path.directory?
      @required_files = path.glob( :recursive => true, :ext => "rb" )
    else
      @required_files = [path]
    end
  end
  
  @required_files.map!{|f| f.expand.gsub( /\.rb$/, '' ) }
  @successfully_required = []
  @missing_constants = {}
end

Class Method Details

.require_files(path) ⇒ Object



5
6
7
# File 'lib/fun_with/files/requirements/manager.rb', line 5

def self.require_files( path )
  self.new( path ).require_files
end

Instance Method Details

#check_for_needed_constantsObject



71
72
73
74
75
76
77
78
# File 'lib/fun_with/files/requirements/manager.rb', line 71

def check_for_needed_constants
  for konst, files in @missing_constants
    if Object.const_defined?( konst )
      @required_files = files + @required_files    
      @missing_constants.delete( konst )
    end
  end
end

#require_filesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fun_with/files/requirements/manager.rb', line 26

def require_files
  while @required_files.length > 0
    file = @required_files.shift
    
    if try_requiring_file( file )
      check_for_needed_constants
    end
  end
  
  # Ran into a situation where it was failing because the missing constant was incorrectly being guessed
  # to be M1::M2::M3::M4 instead of M1::M2::M4.  Had the file been required, it would have gone through.
  # So I'm adding a last-chance round, using the ugly old approach of simply trying to require everything
  # over and over again until it's clear no progress ins being made.
  unless @missing_constants.fwf_blank?
    unless require_files_messily( @missing_constants.values.flatten )
      raise NameError.new( "The following constants could not be defined: #{@missing_constants.inspect}")
    end
  end
end

#require_files_messily(files) ⇒ Object

returns true if all the files given got required



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fun_with/files/requirements/manager.rb', line 81

def require_files_messily( files )
  while true
    files_remaining = files.length
    return true if files_remaining == 0
    
    files.length.times do
      begin
        file = files.shift
        require file
        @successfully_required << file
      rescue NameError => e
        uninitialized_constant_error( e ) do
          files.push( file )
        end
      end
    end
    
    return false if files.length == files_remaining
  end
end

#try_requiring_file(file) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fun_with/files/requirements/manager.rb', line 55

def try_requiring_file( file )
  begin
    require file
    @successfully_required << file
    true
  rescue NameError => e
    uninitialized_constant_error( e ) do
      konst = e.message.split.last
      
      @missing_constants[konst] ||= []
      @missing_constants[konst] << file
      false
    end
  end
end

#uninitialized_constant_error(e, &block) ⇒ Object

If it’s not the sort of error we’re looking for, re-raise the error



47
48
49
50
51
52
53
# File 'lib/fun_with/files/requirements/manager.rb', line 47

def uninitialized_constant_error( e, &block )
  if e.message =~ /^uninitialized constant/
    yield
  else
    raise e
  end
end