Module: SmartInit

Included in:
Object
Defined in:
lib/drumherum/smart_init.rb

Overview

smart_init finds the directory named ‘lib’ in your project and adds

  • the (main) directory above

  • the lib-directory itself

to Rubys $LOAD_PATH. So your require statements load the actual version from your (local) project directory, not the (public) gem version.

Also, smart_init sets some release infos for Hoe.

Usage (wherever you are in the directory hierarchy of your project):

if $0 == __FILE__ 
  require 'drumherum'
  smart_init 
end
require 'my-gem-project'   # actual local version, not the gem version

Included in Object collapse

Instance Method Details

#smart_init(__file__ = nil) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/drumherum/smart_init.rb', line 158

def smart_init(__file__ = nil)
  __file__ = caller[0] unless __file__
  dir_caller =File.expand_path(File.dirname(__file__))
  
  #puts "smart_init " + dir_caller    
  
  patharray = dir_caller.split('/')
  patharray = dir_caller.split("\\")       if patharray.size == 1  
  projectname = 'ERROR'

  (patharray.size-2).times do |i|
  
    # found name/lib/name?
    if (patharray[-2] == 'lib'  &&  patharray[-1] == patharray[-3])
      projectname = patharray[-1]
      patharray.pop(2)
      break
      
    # found lib one level down?
    elsif File.directory?( File.join(patharray, 'lib') ) 
      projectname = patharray[-1]
      break
      
    else
      patharray.pop
      
    end
  
  end # do
  

  # see "projectname = #{projectname}"
  # see patharray    
  # see
  Drumherum::directory_main = patharray.dup
  
  # /projectname/lib/projectname not included, because you won't be able to require 'set'
  # anymore if you have a file with the same name in your lib
  # newpath = File.join(patharray,'lib', projectname)  
  # unless $:.include?(newpath)
    # $:.unshift(newpath)  
  # end      
     
  # /projectname/lib 
  newpath = File.join(patharray,'lib')
  unless $:.include?(newpath)
    $:.unshift(newpath)  
  end       
     
  # /projectname
  newpath = File.join(patharray)
  unless $:.include?(newpath)
    $:.unshift(newpath)  
  end    
  
  # see $LOAD_PATH
  
end