require-magic 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -7,15 +7,20 @@ Using this toolset should really simplify your require statements and make your
7
7
  == USAGE
8
8
  See unit tests for demonstrations of how to use it:
9
9
 
10
+ Set basepath to use for require
11
+ * required_files = Require.base_path = File.dirname(__FILE__)
12
+
10
13
  To require all files within the top level folder 'data' (non-recursively)
11
14
  * required_files = Require.folder('data')
12
15
 
16
+ Override base_path
17
+ * required_files = Require.folder('data', {:base_path => File.dirname(__FILE__) + '/../my/path})
18
+
13
19
  The required_files returned is a list of the paths of the files that were required
14
20
 
15
21
  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
16
22
  * required_files = Require.folder('data')
17
23
 
18
-
19
24
  To require all files within the top level folder 'data' recursively
20
25
  * required_files = Require.rfolder('data')
21
26
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
data/lib/require-magic.rb CHANGED
@@ -1,4 +1,12 @@
1
- module Require
1
+ require 'singleton'
2
+ class Require
3
+ include Singleton
4
+
5
+ class << self
6
+ attr_accessor :base_path
7
+ attr_accessor :tracing
8
+ attr_accessor :verbose
9
+ end
2
10
  def self.rfolder(name, options = {})
3
11
  options[:recursive] = true
4
12
  folder(name, options)
@@ -8,8 +16,14 @@ module Require
8
16
  recursive = options[:recursive]
9
17
  folder_list = options[:folders]
10
18
  file_pattern = recursive ? "#{name}/**/*.rb" : "#{name}/*.rb"
11
- base_path = File.dirname(__FILE__)
12
- path = File.join(base_path, file_pattern)
19
+
20
+ base_dir = File.dirname(__FILE__)
21
+
22
+ curr_base_path = options[:base_path] || base_path || base_dir
23
+
24
+ # puts "base_path: #{curr_base_path}, base_dir:#{base_dir}, :base_path #{base_path}"
25
+
26
+ path = File.join(curr_base_path, file_pattern)
13
27
  required_files = []
14
28
 
15
29
  puts_trace "folder:: name: #{name}", options
@@ -47,7 +61,7 @@ protected
47
61
  end
48
62
 
49
63
  def self.tracing?(options)
50
- options[:tracing] == :on
64
+ tracing == :on || options[:tracing] == :on
51
65
  end
52
66
 
53
67
  def self.is_root?(path, options)
@@ -94,7 +108,15 @@ protected
94
108
 
95
109
  def self.do_require(name)
96
110
  require name
97
- name
111
+ list_name name
112
+ end
113
+
114
+ def self.list_name(name)
115
+ if base_path && !(verbose == :on)
116
+ name.gsub(/#{Regexp.escape(base_path)}/, '')
117
+ else
118
+ name
119
+ end
98
120
  end
99
121
 
100
122
  def self.match(f, rexp)
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{require-magic}
8
- s.version = "0.2.2"
8
+ s.version = "0.2.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
@@ -38,8 +38,9 @@ Gem::Specification.new do |s|
38
38
  s.test_files = [
39
39
  "spec/require-magic_spec.rb",
40
40
  "spec/spec_helper.rb",
41
- "test/test_require_magic.rb",
42
- "test/test_require_magic_adv.rb",
41
+ "test/test_require_folder.rb",
42
+ "test/test_require_folders.rb",
43
+ "test/test_require_folders_adv.rb",
43
44
  "test/unit_test_require.rb"
44
45
  ]
45
46
 
@@ -0,0 +1,55 @@
1
+ require 'require-magic'
2
+ require "unit_test_require"
3
+
4
+ class TestRequireFolder < UnitTestRequire
5
+
6
+ def test_require_folder
7
+ required_files = Require.folder(@folder)
8
+
9
+ found0 = match? required_files[0], 'data_a.rb'
10
+
11
+ assert_equal 1, required_files.size, "Should require 1 file"
12
+ assert found0, "Should require data_a.rb in /"
13
+
14
+ end
15
+
16
+ def test_require_folder_recursive
17
+ required_files = Require.rfolder(@folder)
18
+ assert_equal 4, required_files.size, "Should require 4 files"
19
+ end
20
+
21
+ def test_require_folder_recursive_include
22
+ options = {:include => '_a'}
23
+ required_files = Require.folder(@folder, options)
24
+ assert_equal 1, required_files.size, "Should require 1 file"
25
+ end
26
+
27
+ def test_require_folder_recursive_include_base_path
28
+ options = {:include => '_a'}
29
+ Require.base_path = File.dirname(__FILE__) + "/../lib"
30
+ required_files = Require.folder(@folder, options)
31
+
32
+ assert_equal 1, required_files.size, "Should require 1 file"
33
+ assert_equal "/data/data_a.rb", required_files[0], "Path of required files should be from base_path when verbose not :on"
34
+ end
35
+
36
+
37
+ def test_require_folders_recursive_exclude_base_path
38
+ options = {:exclude => '_a'}
39
+ required_files = Require.folder(@folder, options)
40
+ assert_equal 0, required_files.size, "Should require 0 files"
41
+ end
42
+
43
+ def test_require_folder_recursive_default_base_path_override
44
+ options = {:folders => ['blip'], :base_path => File.dirname(__FILE__) + "/../lib"}
45
+ required_files = Require.folder(@folder, options)
46
+
47
+ assert_equal 3, required_files.size, "Should require 3 files"
48
+
49
+ found0 = match? required_files[0], 'data_a.rb'
50
+ assert found0, "Should require root files such as 'data_a.rb' first"
51
+ end
52
+
53
+
54
+ end
55
+
@@ -1,7 +1,7 @@
1
1
  require 'require-magic'
2
2
  require "unit_test_require"
3
3
 
4
- class TestFilter < UnitTestRequire
4
+ class TestRequireFolders < UnitTestRequire
5
5
 
6
6
  def test_require_folders
7
7
  required_files = Require.folders(@folders)
@@ -1,7 +1,7 @@
1
1
  require 'require-magic'
2
2
  require "unit_test_require"
3
3
 
4
- class TestRequireAdv < UnitTestRequire
4
+ class TestRequireFoldersAdv < UnitTestRequire
5
5
 
6
6
  def test_require_folders_recursive
7
7
  options = {:folders => ['blip'], :ignore_root_files => true}
@@ -4,6 +4,7 @@ class UnitTestRequire < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
6
  @folders = ['data', 'data2']
7
+ @folder = 'data'
7
8
  end
8
9
 
9
10
  def match?(expr, rexp)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: require-magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristian Mandrup
@@ -75,6 +75,7 @@ summary: utility to facilitate require of .rb files and folders
75
75
  test_files:
76
76
  - spec/require-magic_spec.rb
77
77
  - spec/spec_helper.rb
78
- - test/test_require_magic.rb
79
- - test/test_require_magic_adv.rb
78
+ - test/test_require_folder.rb
79
+ - test/test_require_folders.rb
80
+ - test/test_require_folders_adv.rb
80
81
  - test/unit_test_require.rb