require-magic 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,19 @@
1
+ 17-11-2009
2
+ ----------
3
+ Created unit tests to ensure all util functions work as expected. Fixed a number of bugs.
4
+
5
+ 14-11-2009
6
+ ----------
7
+ * Initial release with two helper functions:
8
+
9
+ - Require.folder(name, options)
10
+
11
+ name : the name (or path) of a top-level folder.
12
+ options : a hash containing specific options for applying the require (all optional)
13
+ :include - regular expression to match on .rb filenames to require
14
+ :exclude - regular expression to match on .rb filenames to NOT require
15
+ :recursive - whether to do a recursive traversal of directories
16
+ :folder_list - list of folders under main folder (name argument) to process (in list order)
17
+
18
+
19
+
data/README.rdoc CHANGED
@@ -1,8 +1,8 @@
1
1
  = require-magic
2
2
 
3
3
  Utility functions to facilitate importing (require) ruby files in complex Ruby project, such as gem with complex folder hierarchies.
4
-
5
- NOTE: Needs testing
4
+ See unit tests in /test directory to see how to use the tools for the best effect.
5
+ Using this toolset should really simplify your require statements and make your application more flexible to change.
6
6
 
7
7
  == Note on Patches/Pull Requests
8
8
 
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ begin
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "require-magic"
8
8
  gem.summary = %Q{utility to facilitate require of .rb files and folders}
9
- gem.description = %Q{utility to facilitate require of .rb files and folders}
9
+ gem.description = %Q{enhance your ruby app with require magic to more easily define how to include hierarchies of ruby files, and also apply inclusion/exclusion filters for what to include}
10
10
  gem.email = "kmandrup@gmail.com"
11
11
  gem.homepage = "http://github.com/kristianmandrup/require-magic"
12
12
  gem.authors = ["Kristian Mandrup"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.1
data/lib/require-magic.rb CHANGED
@@ -1,29 +1,113 @@
1
1
  module Require
2
- def self.folder(name, options)
3
- includes_rexp = options[:include]
4
- excludes_rexp = options[:exclude]
2
+ def self.rfolder(name, options = {})
3
+ options[:recursive] = true
4
+ folder(name, options)
5
+ end
6
+
7
+ def self.folder(name, options = {})
5
8
  recursive = options[:recursive]
6
9
  folder_list = options[:folders]
7
-
8
- file_pattern = recursive ? "#{name}/**/*.rb" : "#{name}/*.rb"
9
- Dir.glob(File.join(File.dirname(__FILE__), file_pattern)).each {|f|
10
- next if excludes && match(f, excludes_rexp)
11
- require f if !match || match(f, includes_rexp)
12
- }
13
- folders(folder_list, options) if folder_list
10
+ file_pattern = recursive ? "#{name}/**/*.rb" : "#{name}/*.rb"
11
+ base_path = File.dirname(__FILE__)
12
+ path = File.join(base_path, file_pattern)
13
+ required_files = []
14
+
15
+ puts_trace "folder:: name: #{name}", options
16
+
17
+ if options[:root_files] == :before
18
+ required_files << require_root_files(name, folder_list, path, options)
19
+ # options[:root] = false
20
+ required_files << require_folder_list(name, folder_list, options)
21
+ else
22
+ required_files << require_folder_list(name, folder_list, options)
23
+ required_files << require_root_files(name, folder_list, path, options)
24
+ # options[:root] = false
25
+ end
26
+ required_files.flatten
27
+ end
28
+
29
+ def self.rfolders(folder_list, options = {})
30
+ options[:recursive] = true
31
+ folders(folder_list, options)
14
32
  end
33
+
15
34
 
16
- def self.folders(folder_list, options)
17
- folder_list.each(|f| folder(f, options))
35
+ def self.folders(folder_list, options = {})
36
+ required_files = []
37
+ folder_list.each do |path|
38
+ options[:root] = path if is_root?(path, options)
39
+ required_files << folder(path, options)
40
+ end
41
+ required_files.flatten
18
42
  end
19
43
 
20
44
  protected
21
- def match(f, rexp)
45
+ def self.puts_trace(txt, options)
46
+ puts txt if tracing?(options)
47
+ end
48
+
49
+ def self.tracing?(options)
50
+ options[:tracing] == :on
51
+ end
52
+
53
+ def self.is_root?(path, options)
54
+ root = options[:root]
55
+ root == nil || (root && path.size < root.size)
56
+ end
57
+
58
+ def self.require_root_files(name, folder_list, path, options)
59
+ puts_trace "require_root_files:: name: #{name}, folders: #{folder_list.inspect}, path: #{path}", options
60
+ required_files = []
61
+ i_am_root = options[:root] == name
62
+ if options[:ignore_root_files] && i_am_root
63
+ options.delete :ignore_root_files
64
+ else
65
+ includes_rexp = options[:include]
66
+ excludes_rexp = options[:exclude]
67
+
68
+ Dir.glob(path).each {|f|
69
+ next if excludes_rexp && match(f, excludes_rexp)
70
+ if !includes_rexp || match(f, includes_rexp)
71
+ puts_trace "require: #{f}", options
72
+ required_files << do_require(f)
73
+ end
74
+ }
75
+ end
76
+
77
+ required_files.flatten
78
+ end
79
+
80
+ def self.require_folder_list(name, folder_list, options)
81
+ puts_trace "require_folder_list:: name: #{name}, folders: #{folder_list.inspect}", options
82
+ required_files = []
83
+ if folder_list
84
+ # options.delete :ignore_root_files
85
+ options.delete :folders
86
+
87
+ folder_list.each do |folder|
88
+ sub_folder = File.join(name, folder)
89
+ required_files << folders(sub_folder, options)
90
+ end
91
+ end
92
+ required_files.flatten
93
+ end
94
+
95
+ def self.do_require(name)
96
+ require name
97
+ name
98
+ end
99
+
100
+ def self.match(f, rexp)
101
+ if rexp.kind_of? String
102
+ rexp = /#{Regexp.escape(rexp)}/
103
+ end
104
+
22
105
  if rexp.kind_of? Array
23
106
  rexp.each{|e| return true if match(f, rexp)}
24
107
  false
25
- elsif rexp.kind_of? RegExp
26
- f =~ rexp
108
+ elsif rexp.kind_of? Regexp
109
+ match_res = (f =~ rexp)
110
+ !match_res.nil?
27
111
  end
28
112
  end
29
113
 
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{require-magic}
8
+ s.version = "0.2.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kristian Mandrup"]
12
+ s.date = %q{2009-11-19}
13
+ s.description = %q{enhance your ruby app with require magic to more easily define how to include hierarchies of ruby files, and also apply inclusion/exclusion filters for what to include}
14
+ s.email = %q{kmandrup@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "CHANGELOG",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/require-magic.rb",
28
+ "require-magic.gemspec",
29
+ "spec/require-magic_spec.rb",
30
+ "spec/spec.opts",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/kristianmandrup/require-magic}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.5}
37
+ s.summary = %q{utility to facilitate require of .rb files and folders}
38
+ s.test_files = [
39
+ "spec/require-magic_spec.rb",
40
+ "spec/spec_helper.rb",
41
+ "test/test_require_magic.rb",
42
+ "test/test_require_magic_adv.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
56
+ end
57
+ end
58
+
@@ -1 +1,50 @@
1
- # TODO
1
+ require 'require-magic'
2
+ require "test/unit"
3
+
4
+ class TestFilter < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @folders = ['data', 'data2']
8
+ end
9
+
10
+ def teardown
11
+ ## Nothing really
12
+ end
13
+
14
+ def test_require_folders
15
+ required_files = Require.folders(@folders)
16
+
17
+ found0 = !(required_files[0] =~ /data_a.rb/).nil?
18
+ found1 = !(required_files[1] =~ /data2\/blap_a.rb/).nil?
19
+
20
+ assert_equal 2, required_files.size, "Should require 2 files"
21
+ assert_equal true, found0, "Should require data_a.rb in /"
22
+ assert_equal true, found1, "Should require blap_a.rb in /data"
23
+ end
24
+
25
+ def test_require_folders_recursive
26
+ options = {:recursive => true}
27
+ required_files = Require.folders(@folders, options)
28
+ assert_equal 3, required_files.size, "Should require 3 files"
29
+ end
30
+
31
+ def test_require_folders_recursive
32
+ options = {:recursive => true, :include => 'blip_'}
33
+ required_files = Require.folders(@folders, options)
34
+ assert_equal 2, required_files.size, "Should require 2 files"
35
+ end
36
+
37
+ def test_require_folders_recursive
38
+ options = {:recursive => true, :exclude => 'blip_'}
39
+ required_files = Require.folders(@folders, options)
40
+ assert_equal 2, required_files.size, "Should require 2 file"
41
+ end
42
+
43
+ def test_require_folders_recursive
44
+ options = {:folders => ['blip']}
45
+ required_files = Require.folders(@folders, options)
46
+ assert_equal 4, required_files.size, "Should require 4 files"
47
+ end
48
+
49
+ end
50
+
@@ -0,0 +1,47 @@
1
+ require 'require-magic'
2
+ require "test/unit"
3
+
4
+ class TestFilter < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @folders = ['data', 'data2']
8
+ end
9
+
10
+ def teardown
11
+ ## Nothing really
12
+ end
13
+
14
+
15
+
16
+ def test_require_folders_recursive
17
+ options = {:folders => ['blip'], :ignore_root_files => true}
18
+ required_files = Require.folders(@folders, options)
19
+ assert_equal 3, required_files.size, "Should require 3 files"
20
+ end
21
+
22
+ def test_require_folders_root_folders_before
23
+ options = {:folders => ['blip'], :root_files => :before}
24
+ required_files = Require.folders(@folders, options)
25
+ assert_equal 4, required_files.size, "Should require 4 files"
26
+ end
27
+
28
+ def test_require_folders_root_folders_before
29
+ options = {:folders => ['blip'], :root_files => :after}
30
+ required_files = Require.folders(@folders, options)
31
+ assert_equal 4, required_files.size, "Should require 4 files"
32
+ end
33
+
34
+ def test_require_folders_root_folders_before_but_ignore
35
+ options = {:folders => ['blip'], :root_files => :after, :ignore_root_files => true}
36
+ required_files = Require.folders(@folders, options)
37
+ assert_equal 3, required_files.size, "Should require 3 files"
38
+ end
39
+
40
+ def test_require_folders_root_folders_after_but_ignore
41
+ options = {:folders => ['blip'], :root_files => :after, :ignore_root_files => true}
42
+ required_files = Require.folders(@folders, options)
43
+ assert_equal 3, required_files.size, "Should require 3 files"
44
+ end
45
+
46
+
47
+ end
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.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristian Mandrup
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-15 00:00:00 +01:00
12
+ date: 2009-11-19 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,7 +22,7 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: 1.2.9
24
24
  version:
25
- description: utility to facilitate require of .rb files and folders
25
+ description: enhance your ruby app with require magic to more easily define how to include hierarchies of ruby files, and also apply inclusion/exclusion filters for what to include
26
26
  email: kmandrup@gmail.com
27
27
  executables: []
28
28
 
@@ -34,11 +34,13 @@ extra_rdoc_files:
34
34
  files:
35
35
  - .document
36
36
  - .gitignore
37
+ - CHANGELOG
37
38
  - LICENSE
38
39
  - README.rdoc
39
40
  - Rakefile
40
41
  - VERSION
41
42
  - lib/require-magic.rb
43
+ - require-magic.gemspec
42
44
  - spec/require-magic_spec.rb
43
45
  - spec/spec.opts
44
46
  - spec/spec_helper.rb
@@ -74,3 +76,4 @@ test_files:
74
76
  - spec/require-magic_spec.rb
75
77
  - spec/spec_helper.rb
76
78
  - test/test_require_magic.rb
79
+ - test/test_require_magic_adv.rb