require-magic 0.2.5 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,44 +5,83 @@ See unit tests in /test directory to see how to use the tools for the best effec
5
5
  Using this toolset should really simplify your require statements and make your application more flexible to change.
6
6
 
7
7
  == USAGE
8
+
9
+ require 'require_magic'
10
+
11
+ # alternatively to only include require_dsl
12
+ require 'require_dsl'
13
+
14
+ Require.magic [path relative to current file location] do |magic|
15
+ # from new location, enter a subdir
16
+ magic.enter [subdir] do |path|
17
+ magic.require_all('**/*.rb').except(/sound\/*.rb/).require
18
+ end
19
+
20
+ # from new location, enter a subdir
21
+ magic.enter [another subdir] do |path|
22
+ # use file blobs here
23
+ magic.require_all('**/*.rb').require
24
+ end
25
+
26
+ # from new location, enter a subdir
27
+ magic.enter [yet another subdir] do |path|
28
+ # matching and except are to be used as include and exclude filters
29
+ # they each take a list containing regular expressions and strings
30
+ # string arguments are postfixed with .rb internally if not present
31
+ magic.require_all('blip/**/*.rb').matching(/_mixin.rb/, /.*\/power/).except(/sound/, /disco/).require
32
+ end
33
+
34
+ end
35
+
36
+ Require.magic '../spec/fixtures' do |magic|
37
+ magic.enter 'game' do |path|
38
+ list = magic.require_all('**/*.rb')
39
+ # puts list.matching('sound', 'network').except(/sound/).show_require(:relative)
40
+ list.matching('sound', 'network').except(/sound/).require
41
+ end
42
+ end
43
+
44
+
45
+ == USAGE (Deprecated)
8
46
  See unit tests for demonstrations of how to use it:
9
47
 
10
48
  Set basepath to use for require
11
49
  * required_files = Require.base_path = File.dirname(__FILE__)
12
50
 
13
51
  To require all files within the top level folder 'data' (non-recursively)
14
- * required_files = Require.folder('data')
52
+ * required_files = Require.folders('data')
15
53
 
16
54
  Override base_path
17
- * required_files = Require.folder('data', {:base_path => File.dirname(__FILE__) + '/../my/path})
55
+ * required_files = Require.folders('data', {:base_path => File.dirname(__FILE__) + '/../my/path})
18
56
 
19
57
  The required_files returned is a list of the paths of the files that were required
20
58
 
21
59
  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
22
- * required_files = Require.folder('data')
60
+ * required_files = Require.folder 'data'
61
+ * required_files = Require.folders 'data'
23
62
 
24
63
  To require all files within the top level folder 'data' recursively
25
- * required_files = Require.rfolder('data')
64
+ * required_files = Require.recursive('data')
26
65
 
27
66
  To require all files within the top level folders 'data' and 'data2' (non-recursively)
28
- * required_files = Require.rfolders(['data', 'data2'])
67
+ * required_files = Require.recursive(['data', 'data2'])
29
68
 
30
69
  To require all files within the top level folders 'data' and 'data2' recursively
31
- * required_files = Require.rfolders(['data', 'data2'])
70
+ * required_files = Require.recursive(['data', 'data2'])
32
71
 
33
72
  To require files within the top level folders 'data' and 'data2' and also files within the subdirectory 'blip' if it exists
34
73
  * required_files = Require.folders(['data', 'data2'], {:folders => ['blip]})
35
74
 
36
- To require files within 'data/blip' and 'data2/blip' only
75
+ To require files within 'data/blip' and 'data2/blip' only, NOT including the root files
37
76
  * required_files = Require.folders(['data', 'data2'], {:folders => ['blip], :ignore_root_files => true})
38
77
 
39
- To require files within 'data' and 'data2' first and then any files within the subdirectory 'blip' (default order)
78
+ To require files within 'data' and 'data2' first and then AFTER any files within the subdirectory 'blip' (default order)
40
79
  * required_files = Require.folders(['data', 'data2'], {:folders => ['blip], :root_files => :before})
41
80
 
42
- To require files within 'data/blip' and 'data2/blip' first and then any files within 'data' and 'data2' folders
81
+ To require files within 'data/blip' and 'data2/blip' first and then AFTER any files within 'data' and 'data2' folders (the root files)
43
82
  * required_files = Require.folders(['data', 'data2'], {:folders => ['blip], :root_files => :after})
44
83
 
45
- To require files within 'data' and 'data2' first and then any files within the subdirectory 'blip'
84
+ To require files within 'data' and 'data2' (the root files) first (BEFORE) and then any files within the subdirectory 'blip'
46
85
  * required_files = Require.folders(['data', 'data2'], {:folders => ['blip], :root_files => :before})
47
86
 
48
87
 
@@ -59,4 +98,4 @@ To require files within 'data' and 'data2' first and then any files within the s
59
98
 
60
99
  == Copyright
61
100
 
62
- Copyright (c) 2009 Kristian Mandrup. See LICENSE for details.
101
+ Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
@@ -0,0 +1,102 @@
1
+ require 'rake'
2
+ require 'fileutils'
3
+ require 'util/util'
4
+
5
+ module Require
6
+
7
+ def self.magic(path, &block)
8
+ yield Magic.new path
9
+ end
10
+
11
+ module MagicList
12
+ attr_accessor :base_path
13
+ attr_accessor :rel_path
14
+
15
+ def require(file)
16
+ require file
17
+ end
18
+
19
+ def show_require(*options)
20
+ each do |f|
21
+ if options.include? :relative
22
+ file_path = File.join(rel_path, f)
23
+ path = Require::Dir.relative_path(base_path, file_path)
24
+ end
25
+ path = File.join(base_path, f) if !options.include? :relative
26
+ path
27
+ end
28
+ end
29
+
30
+
31
+ def delete_these(objs)
32
+ reject! do |obj|
33
+ a = false
34
+ objs.each do |del|
35
+ a = true if obj.include? del
36
+ end
37
+ a
38
+ end
39
+ end
40
+
41
+ def except(*reg_exps)
42
+ self.each do |file|
43
+ reg_exps.each {|re| self.delete(file) if file.match(re) }
44
+ end
45
+ self
46
+ end
47
+
48
+ def postfix_rb(str)
49
+ str << '.rb' if !str.include? '.rb'
50
+ end
51
+
52
+ def matching(*reg_exps)
53
+ keep_list = []
54
+ each do |file|
55
+ reg_exps.each do |re|
56
+ postfix_rb(re) if re.kind_of? String
57
+ keep_list << file if file.match(re)
58
+ end
59
+ end
60
+ reject_list = (self - keep_list).flatten
61
+ delete_these(reject_list)
62
+ self
63
+ end
64
+ end
65
+
66
+ class Magic
67
+ attr_accessor :current_path
68
+ attr_accessor :dir_stack
69
+
70
+ def initialize(dir)
71
+ @dir_stack = []
72
+ @current_path = FileUtils.pwd
73
+ @current_path = enter(dir)
74
+ end
75
+
76
+ def enter(dir)
77
+ FileUtils.cd dir
78
+ dir_stack.push path = FileUtils.pwd
79
+ @current_path = path
80
+ if block_given?
81
+ yield path
82
+ dir_stack.pop
83
+ current_path = dir_stack.last
84
+ end
85
+ path
86
+ end
87
+
88
+ def require_all(*globs)
89
+ list = FileList.new(globs)
90
+ list.extend(MagicList)
91
+ list.base_path = dir_stack.first
92
+ list.rel_path = current_path
93
+ list
94
+ end
95
+
96
+ end
97
+ end
98
+
99
+
100
+
101
+
102
+
@@ -1,27 +1,45 @@
1
- require 'rubygems'
2
- require 'singleton'
3
- class Require
4
- include Singleton
1
+ require 'require-dsl'
2
+
3
+ module Require
5
4
 
6
5
  class << self
7
6
  attr_accessor :base_path
8
7
  attr_accessor :tracing
9
8
  attr_accessor :verbose
10
9
  end
11
- def self.rfolder(name, options = {})
10
+
11
+ def self.recursive(*names, options, &block)
12
+ options = {} if !options
12
13
  options[:recursive] = true
13
- folder(name, options)
14
+ names.each{|name| folder(name, options) }
14
15
  end
15
16
 
17
+ def self.folders(*names, options)
18
+ options = {} if !options
19
+ required_files = []
20
+ names.each do |path|
21
+ options[:root] = path if is_root?(path, options)
22
+ required_files << folder(path, options)
23
+ end
24
+ required_files.flatten
25
+ end
26
+
27
+ def self.enter(name, options = {}, &block)
28
+ options[:recursive] = true
29
+ file folder(name, options)
30
+ yield File.dirname(file)
31
+ end
32
+
33
+
16
34
  def self.folder(name, options = {})
17
35
  recursive = options[:recursive]
18
36
  folder_list = options[:folders]
19
37
  file_pattern = recursive ? "#{name}/**/*.rb" : "#{name}/*.rb"
20
-
38
+
21
39
  base_dir = File.dirname(__FILE__)
22
-
40
+
23
41
  curr_base_path = options[:base_path] || base_path || base_dir
24
-
42
+
25
43
  # puts "base_path: #{curr_base_path}, base_dir:#{base_dir}, :base_path #{base_path}"
26
44
 
27
45
  path = File.join(curr_base_path, file_pattern)
@@ -39,22 +57,7 @@ class Require
39
57
  # options[:root] = false
40
58
  end
41
59
  required_files.flatten
42
- end
43
-
44
- def self.rfolders(folder_list, options = {})
45
- options[:recursive] = true
46
- folders(folder_list, options)
47
- end
48
-
49
-
50
- def self.folders(folder_list, options = {})
51
- required_files = []
52
- folder_list.each do |path|
53
- options[:root] = path if is_root?(path, options)
54
- required_files << folder(path, options)
55
- end
56
- required_files.flatten
57
- end
60
+ end
58
61
 
59
62
  protected
60
63
  def self.puts_trace(txt, options)
@@ -0,0 +1,13 @@
1
+ require 'pathname'
2
+
3
+ module Require
4
+ module Dir
5
+ def self.relative_path(base_path, path)
6
+ path.gsub! /#{Regexp.escape base_path}/
7
+ p1 = Pathname.new base_path
8
+ p2 = p1 + path
9
+ p4 = p2.relative_path_from(p1) # Pathname:lib/ruby/1.8
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1 @@
1
+ puts "Game was included"
@@ -0,0 +1 @@
1
+ puts "Graphics was included"
@@ -0,0 +1 @@
1
+ puts "Network was included"
@@ -0,0 +1 @@
1
+ puts "Sound was included"
@@ -0,0 +1,12 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "RequireMagic" do
4
+ it "works" do
5
+ Require.magic '../spec/fixtures' do |magic|
6
+ magic.enter 'game' do |path|
7
+ list = magic.require_all('**/*.rb')
8
+ puts list.matching( 'sound', 'network').except(/sound/).show_require(:relative).inspect.should include("network/network.rb")
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,9 +1,6 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ # require 'require-dsl'
3
4
  require 'require-magic'
4
- require 'spec'
5
- require 'spec/autorun'
6
-
7
- Spec::Runner.configure do |config|
8
-
9
- end
5
+ require 'rspec'
6
+ require 'rspec/autorun'
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.5
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristian Mandrup
@@ -11,18 +11,9 @@ cert_chain: []
11
11
 
12
12
  date: 2010-03-02 00:00:00 -04:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: rspec
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.2.9
24
- version:
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
14
+ dependencies: []
15
+
16
+ description: Lets you easily define how to include hierarchies of ruby files, and also apply inclusion/exclusion filters for what to include
26
17
  email: kmandrup@gmail.com
27
18
  executables: []
28
19
 
@@ -32,7 +23,9 @@ extra_rdoc_files:
32
23
  - LICENSE
33
24
  - README.rdoc
34
25
  files:
26
+ - lib/require-dsl.rb
35
27
  - lib/require-magic.rb
28
+ - lib/util/util.rb
36
29
  - LICENSE
37
30
  - README.rdoc
38
31
  has_rdoc: true
@@ -62,9 +55,13 @@ rubyforge_project:
62
55
  rubygems_version: 1.3.5
63
56
  signing_key:
64
57
  specification_version: 3
65
- summary: utility to facilitate require of .rb files and folders
58
+ summary: Facilitates requiring select ruby files in folders
66
59
  test_files:
67
- - spec/require-magic_spec.rb
60
+ - spec/fixtures/game/game.rb
61
+ - spec/fixtures/game/graphics/graphics.rb
62
+ - spec/fixtures/game/network/network.rb
63
+ - spec/fixtures/game/sound/sound.rb
64
+ - spec/require-dsl_spec.rb
68
65
  - spec/spec_helper.rb
69
66
  - test/test_require_folder.rb
70
67
  - test/test_require_folders.rb
@@ -1,7 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "RequireMagic" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
6
- end
7
- end