Sha256: 23dccc79eb5acb34ab5eecea5208380f4f32f53ddfe4f211c2a09ba8ebe7f3eb

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

require 'path_builder'
#
# This module makes the setting up of the load path for Ruby projects easy
# by the use of a simple configure() block. Also your statements to setup the load path
# look cleaner and clearer
#
# See the README.md file for detailed examples.
#
# Author: Nayyara Samuel (nayyara.samuel@opower.com)
#
module LoadPath

  # Primary method to setup your load path
  def self.configure(&block)
    root = calling_class_path(caller(0))
    # Evaluate the block on an instance of the load path setup
    # that understand the files required
    LoadPathSetup.new(root).instance_eval(&block)
  end

  # Construct the file location/path of the file that is using this module
  def self.calling_class_path(call_stack)
    me = __FILE__
    configure_call_index = call_stack.index { |call| call =~ /#{me}.*\:in.*configure/ }
    external_call = call_stack[configure_call_index + 1]
    begin
      external_file_name = (external_call.match /(.*):\d+:in/)[1]
      external_file_directory = File.expand_path(File.dirname(external_file_name))
    rescue
      raise "Cannot infer calling file's name from #{external_call}"
    end
    return external_file_directory
  end

  # Helper class to setup the load path
  class LoadPathSetup < PathHelper

    # Require all files matching the pattern in the directory
    def require_files(directory, file_pattern='*.rb')
      Dir.glob(File.join(directory, file_pattern)) do |file|
        require File.basename(file.to_s)
      end
    end

    # Add the given directory to the load path
    def add(directory)
      $: << directory
    end

    def path_builder(&block)
      builder = LoadPath::PathBuilder.new(self.file_path)
      builder.instance_eval(&block)
      builder.file_path
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
load_path-0.2.0 lib/load_path.rb