require-magic

Utility functions to facilitate importing (require) ruby files in complex Ruby project, such as gem with complex folder hierarchies. See unit tests in /test directory to see how to use the tools for the best effect. Using this toolset should really simplify your require statements and make your application more flexible to change.

USAGE

require ‘require_magic’

# alternatively to only include require_dsl require ‘require_dsl’

Folder.enter [path relative to current file location] do |folder|

# from new location, enter a subdir
folder.enter [subdir] do |path|        
  folder.all('**/*.rb').except(/sound\/*.rb/).require  
end

# from new location, enter a subdir
folder.enter [another subdir] do |path|           
  # use file blobs here
  folder.all('**/*.rb').require
end

# from new location, enter a subdir
folder.enter [yet another subdir] do |path|         
  # matching and except are to be used as include and exclude filters
  # they each take a list containing regular expressions and strings
  # string arguments are postfixed with .rb internally if not present  
  folder.all('blip/**/*.rb').matching(/_mixin.rb/, /.*\/power/).except(/sound/, /disco/).require
end

end

# use current path as folder Folder.enter do |folder|

folder.all('**/*.rb').require
folder.enter 'game' do |path|
  list = folder.all('**/*.rb')    
  # puts list.matching('sound', 'network').except(/sound/).show_require(:relative)
  list.matching('sound', 'network').except(/sound/).require
end

end

USAGE (Deprecated)

See unit tests for demonstrations of how to use it:

Set basepath to use for require

  • required_files = Require.base_path = File.dirname(__FILE__)

To require all files within the top level folder ‘data’ (non-recursively)

  • required_files = Require.folders(‘data’)

Override base_path

  • required_files = Require.folders(‘data’, => File.dirname(__FILE__) + ‘/../my/path)

The required_files returned is a list of the paths of the files that were required

To require all files within the top level folder ‘data’ (non-recursively) and apply tracing to see output for the process of requiring the files

  • required_files = Require.folder ‘data’

  • required_files = Require.folders ‘data’

To require all files within the top level folder ‘data’ recursively

  • required_files = Require.recursive(‘data’)

To require all files within the top level folders ‘data’ and ‘data2’ (non-recursively)

  • required_files = Require.recursive([‘data’, ‘data2’])

To require all files within the top level folders ‘data’ and ‘data2’ recursively

  • required_files = Require.recursive([‘data’, ‘data2’])

To require files within the top level folders ‘data’ and ‘data2’ and also files within the subdirectory ‘blip’ if it exists

  • required_files = Require.folders([‘data’, ‘data2’], => [‘blip])

To require files within ‘data/blip’ and ‘data2/blip’ only, NOT including the root files

  • required_files = Require.folders([‘data’, ‘data2’], => [‘blip], :ignore_root_files => true)

To require files within ‘data’ and ‘data2’ first and then AFTER any files within the subdirectory ‘blip’ (default order)

  • required_files = Require.folders([‘data’, ‘data2’], => [‘blip], :root_files => :before)

To require files within ‘data/blip’ and ‘data2/blip’ first and then AFTER any files within ‘data’ and ‘data2’ folders (the root files)

  • required_files = Require.folders([‘data’, ‘data2’], => [‘blip], :root_files => :after)

To require files within ‘data’ and ‘data2’ (the root files) first (BEFORE) and then any files within the subdirectory ‘blip’

  • required_files = Require.folders([‘data’, ‘data2’], => [‘blip], :root_files => :before)

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but

    bump version in a commit by itself I can ignore when I pull)
    
  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 Kristian Mandrup. See LICENSE for details.