finder 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.ruby CHANGED
@@ -29,16 +29,23 @@ repositories:
29
29
  scm: git
30
30
  name: upstream
31
31
  resources:
32
- home: http://rubyworks.github.com/finder
33
- code: http://github.com/rubyworks/finder
34
- mail: http://groups.google.com/rubyworks-mailinglist
32
+ - uri: http://rubyworks.github.com/finder
33
+ label: Website
34
+ type: home
35
+ - uri: http://github.com/rubyworks/finder
36
+ label: Source Code
37
+ type: code
38
+ - uri: http://groups.google.com/rubyworks-mailinglist
39
+ label: Mailing List
40
+ type: mail
41
+ categories: []
35
42
  extra: {}
36
43
  load_path:
37
44
  - lib
38
45
  revision: 0
39
46
  name: finder
40
47
  title: Finder
41
- version: 0.2.1
48
+ version: 0.3.0
42
49
  summary: Robust library file locator
43
50
  created: '2009-11-24'
44
51
  description: Finder is a general purpose file finder for Ruby. Finder can search RubyGems,
@@ -46,4 +53,4 @@ description: Finder is a general purpose file finder for Ruby. Finder can search
46
53
  active or the most current library files. It is especially useful for implementing
47
54
  library-based plugin systems.
48
55
  organization: rubyworks
49
- date: '2012-03-14'
56
+ date: '2012-05-22'
data/HISTORY.md CHANGED
@@ -1,9 +1,25 @@
1
1
  # RELEASE HISTORY
2
2
 
3
+ ## 0.3.0 / 2012-05-22
4
+
5
+ This is a significant release in that the behavior of `Find.load_path` has
6
+ changed to return absolute paths by default. To get relative paths set
7
+ the `:relative` options to `true`. However, if you are doing that you
8
+ likely want to use the new `Find.feature` method which specifically searches
9
+ for requirable files and returns relative paths by default.
10
+
11
+ Changes:
12
+
13
+ * Find.load_path now returns absolute paths by default.
14
+ * Adds option `:relative=>true` to get relative paths.
15
+ * Adds new `Find.feature` for finding requirable files.
16
+ * New Base module provides shared methods to all systems.
17
+
18
+
3
19
  ## 0.2.1 / 2012-03-15
4
20
 
5
21
  Fix Gem finder so that is places spec in Array, and catches
6
- error is gem is not found.
22
+ error if gem is not found.
7
23
 
8
24
  Changes:
9
25
 
@@ -0,0 +1,31 @@
1
+ Finder
2
+
3
+ Copyright (c) 2012 Rubyworks. All rights reserved.
4
+
5
+ License (spdx) BSD-2-Clause
6
+
7
+ Redistribution and use in source and binary forms, with or without
8
+ modification, are permitted provided that the following conditions are met:
9
+
10
+ 1. Redistributions of source code must retain the above copyright notice,
11
+ this list of conditions and the following disclaimer.
12
+
13
+ 2. Redistributions in binary form must reproduce the above copyright
14
+ notice, this list of conditions and the following disclaimer in the
15
+ documentation and/or other materials provided with the distribution.
16
+
17
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
+ COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24
+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+
28
+ -----
29
+ * http://rubyworks.github.com/finder
30
+ * http://spdx.com/licenses/BSD-2_clause
31
+
data/README.md CHANGED
@@ -19,21 +19,27 @@ $LOAD_PATH manually.
19
19
 
20
20
  ## INSTRUCTION
21
21
 
22
- To find finders, simply provide a glob to the appropriate Finder function,
23
- and it will return all matches found within current and/or most recent versions
24
- of a library.
22
+ To find paths, simply provide a glob to the appropriate Finder function,
23
+ and it will return all matches found within current or most recent
24
+ versions of a library.
25
25
 
26
- For example, a common use case is for a plugin-in enabled application is to require all
27
- the finders found in library load paths:
26
+ For example, a common use case for plug-in enabled application is to
27
+ require all the files found in library load paths:
28
28
 
29
29
  require 'finder'
30
30
 
31
- Find.load_path('myapp/*.rb').each do |file|
31
+ Find.feature('myapp/*').each do |file|
32
32
  require(file)
33
33
  end
34
34
 
35
- Alternately you might load finders only as needed. For instance, if a command-line
36
- option calls for it.
35
+ This is basically equivalent to:
36
+
37
+ Find.load_path('myapp/*.rb', :relative=>true).each do |file|
38
+ require(file)
39
+ end
40
+
41
+ Alternately you might load files only as needed. For instance, if a
42
+ command-line option calls for it.
37
43
 
38
44
 
39
45
  ## COPYRIGHTS
@@ -0,0 +1,6 @@
1
+ # Finder
2
+
3
+ To use the Finder library first we need to require it.
4
+
5
+ require 'finder'
6
+
@@ -1,13 +1,9 @@
1
- # Plugin Demonstrandum
1
+ # Load Path Lookup
2
2
 
3
- To use the Finder library first we need to require it.
4
-
5
- require 'finder'
6
-
7
- Now use +Find.load+path+ to seach for a file pattern of our
3
+ Use +Find.load_path+ to search for a file pattern of our
8
4
  choosing within library load paths.
9
5
 
10
- files = Find.load_path('example.rb')
6
+ files = Find.load_path('example.rb', :relative=>true)
11
7
  file = files.first
12
8
 
13
9
  The +find+ method returns path name relative to the load path.
@@ -0,0 +1,21 @@
1
+ # Feature Lookup
2
+
3
+ We can use +Find.feature+ to seach for requireable files from within
4
+ library load paths. The `feature` method returns relatvie paths
5
+ by default and automatically handles extensions --just like `require`.
6
+
7
+ files = Find.feature('example')
8
+ file = files.first
9
+
10
+ The +find+ method returns path name relative to the load path.
11
+
12
+ file.assert == 'example.rb'
13
+
14
+ We can use the `realtive` or `aboslute` options to get the full path.
15
+
16
+ files = Find.feature('example', :absolute=>true)
17
+ file = files.first
18
+
19
+ File.expand_path(file).assert == file
20
+ file.assert.end_with?('example.rb')
21
+
@@ -1,3 +1,5 @@
1
+ require 'finder'
2
+
1
3
  # add the fixtures directory to the $LOAD_PATH
2
4
  $:.unshift(File.dirname(File.dirname(__FILE__)) + '/fixtures')
3
5
 
@@ -3,26 +3,36 @@ require 'finder/find'
3
3
  module Finder
4
4
 
5
5
  # Current version.
6
- VERSION = '0.1.1'
6
+ VERSION = '0.2.0'
7
7
 
8
8
  # Clean module that can be included elsewhere, to proved #path, #load_path
9
9
  # and #data_path methods without including the Gem, Roll, and Site constants.
10
+ #
10
11
  module Findable
11
12
  def path(match, options={})
12
13
  Find.path(match, options)
13
14
  end
14
15
 
16
+ def data_path(match, options={})
17
+ Find.data_path(match, options)
18
+ end
19
+
15
20
  def load_path(match, options={})
16
21
  Find.load_path(match, options)
17
22
  end
18
23
 
19
- def data_path(match, options={})
20
- Find.data_path(match, options)
24
+ def require_path(match, options={})
25
+ Find.require_path(match, options)
26
+ end
27
+
28
+ def feature(match, options={})
29
+ Find.feature(match, options)
21
30
  end
22
31
  end
23
32
 
24
33
  end
25
34
 
26
- # Convenience shortcut.
27
- Find = Finder::Find
35
+ module Find
36
+ extend Finder::Find
37
+ end
28
38
 
@@ -0,0 +1,69 @@
1
+ module Finder
2
+ module Find
3
+
4
+ # Base module provides helper methods to other
5
+ # finders.
6
+ #
7
+ module Base
8
+
9
+ #
10
+ # When included into a module, that module is atuomatically
11
+ # self extended.
12
+ #
13
+ def self.included(mod)
14
+ mod.extend(mod)
15
+ end
16
+
17
+ ##
18
+ ## Like #load_path but searches only for requirable files.
19
+ ##
20
+ ## NOTE: This may be somewhat limited at the moment until we
21
+ ## figure out how best to determine all possible extensions.
22
+ ##
23
+ #def require_path(match, options={})
24
+ # match = append_extensions(match, options)
25
+ # load_path(match, options)
26
+ #end
27
+
28
+ #
29
+ # Like #load_path but searches only for requirable feature files
30
+ # and returns relative paths by default.
31
+ #
32
+ def feature(match, options={})
33
+ options[:relative] = true unless options.key?(:relative) or options.key?(:absolute)
34
+ match = append_extensions(match, options)
35
+ load_path(match, options)
36
+ end
37
+
38
+ private
39
+
40
+ #
41
+ # Validate and normalize load options.
42
+ #
43
+ # @param [Hash] options
44
+ #
45
+ def valid_load_options(options)
46
+ if options.key?(:relative) && options.key?(:absolute)
47
+ raise ArgumentError, "must be either relative or absolute" unless options[:relative] ^ options[:absolute]
48
+ end
49
+
50
+ options[:relative] = false if options[:absolute]
51
+
52
+ options
53
+ end
54
+
55
+ #
56
+ # Append requirable extensions to match glob.
57
+ #
58
+ #
59
+ def append_extensions(match, options={})
60
+ unless Find::EXTENSIONS.include?(File.extname(match))
61
+ match = match + '{' + Find::EXTENSIONS.join(',') + '}'
62
+ end
63
+ match
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+ end
@@ -1,3 +1,5 @@
1
+ require 'rbconfig'
2
+ require 'finder/base'
1
3
  require 'finder/roll'
2
4
  require 'finder/gem'
3
5
  require 'finder/site'
@@ -9,6 +11,9 @@ module Finder
9
11
  module Find
10
12
  extend self
11
13
 
14
+ # TODO: expand on extensions
15
+ EXTENSIONS = %w{.rb .rbx .so}
16
+
12
17
  # Find matching paths, searching through Rolled libraries, Gem-installed libraries
13
18
  # and site locations in `$LOAD_PATH` and `RbConfig::CONFIG['datadir']`.
14
19
  #
@@ -28,12 +33,27 @@ module Finder
28
33
 
29
34
  # Shortcut for #path.
30
35
  #
31
- # Plugin['syckle/*']
36
+ # Find['lib/foo/*']
32
37
  #
33
38
  alias_method :[], :path
34
39
 
35
- # Searching through Rolls, RubyGems and Site locations for matching
36
- # load paths.
40
+ # Searching through all systems for matching data paths.
41
+ #
42
+ # @param [String] match
43
+ # File glob to match against.
44
+ #
45
+ # @example
46
+ # Find.data_path('bar/*')
47
+ #
48
+ def data_path(match, options={})
49
+ found = []
50
+ systems.each do |system|
51
+ found.concat system.data_path(match, options)
52
+ end
53
+ found.uniq
54
+ end
55
+
56
+ # Searching through all systems for matching load paths.
37
57
  #
38
58
  # @param [String] match
39
59
  # File glob to match against.
@@ -49,19 +69,34 @@ module Finder
49
69
  found.uniq
50
70
  end
51
71
 
52
- # Searching through Rolls, RubyGems and Site locations for matching
53
- # data paths.
72
+ ## Searching through all systems for matching load paths.
73
+ ##
74
+ ## @param [String] match
75
+ ## File glob to match against.
76
+ ##
77
+ ## @example
78
+ ## Find.require_path('bar/*')
79
+ ##
80
+ #def require_path(match, options={})
81
+ # found = []
82
+ # systems.each do |system|
83
+ # found.concat system.require_path(match, options)
84
+ # end
85
+ # found.uniq
86
+ #end
87
+
88
+ # Searching through all systems for matching requirable feature files.
54
89
  #
55
90
  # @param [String] match
56
91
  # File glob to match against.
57
92
  #
58
93
  # @example
59
- # Find.data_path('bar/*')
94
+ # Find.feature('ostruct')
60
95
  #
61
- def data_path(match, options={})
96
+ def feature(match, options={})
62
97
  found = []
63
98
  systems.each do |system|
64
- found.concat system.data_path(match, options)
99
+ found.concat system.feature(match, options)
65
100
  end
66
101
  found.uniq
67
102
  end
@@ -4,7 +4,7 @@ module Finder
4
4
  # RubyGems finder methods.
5
5
  #
6
6
  module Gem
7
- extend self
7
+ include Base
8
8
 
9
9
  #
10
10
  # Search gems.
@@ -57,6 +57,8 @@ module Finder
57
57
  # @return [Array<String>] List of paths.
58
58
  #
59
59
  def load_path(match, options={})
60
+ options = valid_load_options(options)
61
+
60
62
  specs = specifications(options)
61
63
 
62
64
  matches = []
@@ -67,7 +69,7 @@ module Finder
67
69
  list = Dir[glob] #.map{ |f| f.untaint }
68
70
  list = list.map{ |d| d.chomp('/') }
69
71
  # return relative paths unless absolute flag
70
- if not options[:absolute]
72
+ if options[:relative] #not options[:absolute]
71
73
  # the extra '' in File.join adds a '/' to the end of the path
72
74
  list = list.map{ |f| f.sub(File.join(spec.full_gem_path, path, ''), '') }
73
75
  end
@@ -108,17 +110,20 @@ module Finder
108
110
 
109
111
  private
110
112
 
113
+ #
111
114
  def specifications(options)
112
115
  name = options[:from] || options[:gem]
113
116
  if name
117
+ criteria = [options[:version]].compact
114
118
  begin
115
- specs = [::Gem::Specification.find_by_name(name.to_s)]
119
+ specs = [::Gem::Specification.find_by_name(name.to_s, *criteria)]
116
120
  rescue ::Gem::LoadError
117
- return []
121
+ specs = []
118
122
  end
119
123
  else
120
- ::Gem::Specification.current_specs
124
+ specs = ::Gem::Specification.current_specs
121
125
  end
126
+ return specs
122
127
  end
123
128
 
124
129
  end
@@ -1,10 +1,10 @@
1
1
  module Finder
2
2
  module Find
3
3
 
4
- # Library finder methods.
4
+ # Finder methods for `Library` system.
5
5
  #
6
6
  module Roll
7
- extend self
7
+ include Base
8
8
 
9
9
  #
10
10
  # Search for current or latest files within a library.
@@ -26,10 +26,18 @@ module Finder
26
26
  ledger = ::Library.ledger
27
27
  end
28
28
 
29
- matches = []
29
+ criteria = [options[:version]].compact
30
+ matches = []
30
31
 
31
32
  ledger.each do |name, lib|
32
- lib = lib.sort.first if Array===lib
33
+ if Array === lib
34
+ lib = lib.select do |l|
35
+ criteria.all?{ |c| l.version.satisfy?(c) }
36
+ end
37
+ lib = lib.sort.first
38
+ else
39
+ next unless criteria.all?{ |c| l.version.satisfy?(c) }
40
+ end
33
41
  find = File.join(lib.location, match)
34
42
  list = Dir.glob(find)
35
43
  list = list.map{ |d| d.chomp('/') }
@@ -62,24 +70,43 @@ module Finder
62
70
  #
63
71
  def load_path(match, options={})
64
72
  return [] unless defined?(::Library)
73
+ options = valid_load_options(options)
65
74
 
66
75
  if from = options[:from]
67
- ledger = {from.to_s => ::Library.ledger[from.to_s]}
76
+ libs = ::Library.ledger[from.to_s]
77
+ if libs
78
+ case libs
79
+ when ::Array
80
+ ledger = libs.empty? ? {} : {from.to_s => libs}
81
+ else
82
+ ledger = {from.to_s => libs}
83
+ end
84
+ else
85
+ ledger = {}
86
+ end
68
87
  else
69
88
  ledger = ::Library.ledger
70
89
  end
71
90
 
91
+ criteria = [options[:version]].compact
72
92
  matches = []
73
93
 
74
94
  ledger.each do |name, lib|
75
95
  list = []
76
- lib = lib.sort.first if Array===lib
96
+ if Array===lib
97
+ lib = lib.select do |l|
98
+ criteria.all?{ |c| l.version.satisfy?(c) }
99
+ end
100
+ lib = lib.sort.first
101
+ else
102
+ next unless criteria.all?{ |c| lib.version.satisfy?(c) }
103
+ end
77
104
  lib.loadpath.each do |path|
78
105
  find = File.join(lib.location, path, match)
79
106
  list = Dir.glob(find)
80
107
  list = list.map{ |d| d.chomp('/') }
81
- # return relative load path unless absolte flag
82
- if not options[:absolute]
108
+ # return relative load path unless absolute flag
109
+ if options[:relative]
83
110
  # the extra '' in File.join adds a '/' to the end of the path
84
111
  list = list.map{ |f| f.sub(File.join(lib.location, path, ''), '') }
85
112
  end
@@ -100,6 +127,9 @@ module Finder
100
127
  # ::Library.find_files(match)
101
128
  #end
102
129
 
130
+ #
131
+ # Search project's data paths.
132
+ #
103
133
  def data_path(match, options={})
104
134
  return [] unless defined?(::Library)
105
135
 
@@ -109,11 +139,19 @@ module Finder
109
139
  ledger = ::Library.ledger
110
140
  end
111
141
 
142
+ criteria = [options[:version]].compact
112
143
  matches = []
113
144
 
114
145
  ledger.each do |name, lib|
115
146
  list = []
116
- lib = lib.sort.first if Array===lib
147
+ if Array === lib
148
+ lib = lib.select do |l|
149
+ criteria.all?{ |c| l.version.satisfy?(c) }
150
+ end
151
+ lib = lib.sort.first
152
+ else
153
+ next unless criteria.all?{ |c| l.version.satisfy?(c) }
154
+ end
117
155
  find = File.join(lib.location, 'data', match)
118
156
  list = Dir.glob(find)
119
157
  list = list.map{ |d| d.chomp('/') }
@@ -1,13 +1,14 @@
1
- require 'rbconfig'
2
-
3
1
  module Finder
4
2
  module Find
5
3
 
6
4
  # System location finder methods.
7
5
  #
8
6
  module Site
9
- extend self
7
+ include Base
10
8
 
9
+ #
10
+ # System's data path.
11
+ #
11
12
  DATA_PATH = RbConfig::CONFIG['datadir']
12
13
 
13
14
  # TODO: Might this support `:from` option via
@@ -23,7 +24,7 @@ module Finder
23
24
  # Search options.
24
25
  #
25
26
  # @return [Array<String>] List of paths.
26
- #
27
+ #
27
28
  def path(match, options={})
28
29
  return [] if options[:from]
29
30
 
@@ -37,6 +38,7 @@ module Finder
37
38
  found
38
39
  end
39
40
 
41
+ #
40
42
  # Search load path for matching patterns.
41
43
  #
42
44
  # @param [String] match
@@ -44,7 +46,7 @@ module Finder
44
46
  #
45
47
  # @param [Hash] options
46
48
  # Search options.
47
- #
49
+ #
48
50
  # @option options [true,false] :absolute
49
51
  # Return absolute paths instead of relative to load path.
50
52
  #
@@ -53,12 +55,14 @@ module Finder
53
55
  def load_path(match, options={})
54
56
  return [] if options[:from]
55
57
 
58
+ options = valid_load_options(options)
59
+
56
60
  found = []
57
- $LOAD_PATH.uniq.map do |path|
61
+ $LOAD_PATH.uniq.each do |path|
58
62
  list = Dir.glob(File.join(File.expand_path(path), match))
59
63
  list = list.map{ |d| d.chomp('/') }
60
- # return relative load path unless absolute flag
61
- if not options[:absolute]
64
+ # return absolute path unless relative flag
65
+ if options[:relative]
62
66
  # the extra '' in File.join adds a '/' to the end of the path
63
67
  list = list.map{ |f| f.sub(File.join(path, ''), '') }
64
68
  end
@@ -67,6 +71,7 @@ module Finder
67
71
  found
68
72
  end
69
73
 
74
+ #
70
75
  # Search data path.
71
76
  #
72
77
  def data_path(match, options={})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-15 00:00:00.000000000 Z
12
+ date: 2012-05-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: detroit
16
- requirement: &30903600 !ruby/object:Gem::Requirement
16
+ requirement: &28070020 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *30903600
24
+ version_requirements: *28070020
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: qed
27
- requirement: &30903060 !ruby/object:Gem::Requirement
27
+ requirement: &28069460 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *30903060
35
+ version_requirements: *28069460
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: ae
38
- requirement: &30918840 !ruby/object:Gem::Requirement
38
+ requirement: &28068940 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *30918840
46
+ version_requirements: *28068940
47
47
  description: Finder is a general purpose file finder for Ruby. Finder can search RubyGems,
48
48
  Roll libraries and Ruby's standard $LOAD_PATH and system data directory for the
49
49
  active or the most current library files. It is especially useful for implementing
@@ -53,20 +53,25 @@ email:
53
53
  executables: []
54
54
  extensions: []
55
55
  extra_rdoc_files:
56
+ - LICENSE.txt
56
57
  - HISTORY.md
57
58
  - README.md
58
59
  files:
59
60
  - .ruby
60
61
  - .yardopts
62
+ - demo/00_intro.md
63
+ - demo/01_load_path.md
64
+ - demo/02_feature.md
61
65
  - demo/applique/ae.rb
62
66
  - demo/applique/helper.rb
63
67
  - demo/fixtures/example.rb
64
- - demo/load_path.md
68
+ - lib/finder/base.rb
65
69
  - lib/finder/find.rb
66
70
  - lib/finder/gem.rb
67
71
  - lib/finder/roll.rb
68
72
  - lib/finder/site.rb
69
73
  - lib/finder.rb
74
+ - LICENSE.txt
70
75
  - HISTORY.md
71
76
  - README.md
72
77
  homepage: http://rubyworks.github.com/finder