lib/grizzled/fileutil/includer.rb in grizzled-ruby-0.1.8 vs lib/grizzled/fileutil/includer.rb in grizzled-ruby-0.1.9
- old
+ new
@@ -172,15 +172,18 @@
# include_pattern:: String regex pattern to match include directives.
# Must have a single regex group for the file name
# or URL. Default: ^%include\s"([^"]+)"
# allow_glob:: For file names, allow and expand glob expressions.
# Doesn't apply to URLs.
+ # sort_glob: true to force a sort of the globbed expression
+ # (default), false not to sort.
def initialize(source, options={})
@max_nesting = options.fetch(:max_nesting, 100)
inc_pattern = options.fetch(:include_pattern, '^%include\s"([^"]+)"')
@include_re = /#{inc_pattern}/
@allow_glob = options.fetch(:allow_glob, false)
+ @sort_glob = options.fetch(:sort_glob, true)
includer_source = source_to_includer_source source
@source_uri = includer_source.uri
@temp = preprocess includer_source
@input = File.open @temp.path
forward_to @input
@@ -201,13 +204,13 @@
private
def source_to_includer_source(source)
if source.class == String
- open_source(URI::parse(source))
+ open_source(URI::parse(source), source)
elsif source.class == File
- open_source(URI::parse(source.path))
+ open_source(URI::parse(source.path), source.path)
elsif source.respond_to? :each_line
IncludeSource.new(source, nil)
else
raise IncludeException.new("Bad input of class #{source.class}")
end
@@ -259,20 +262,21 @@
parent_path = cur_uri.path
abs = File.absolute_path(
File.join(::File.dirname(cur_uri.path), source)
)
uri = FakeURI.new(abs)
+ source = abs
end
end
- open_source(uri)
+ open_source(uri, source)
end
# Open an input source, based on a parsed URI.
- def open_source(uri)
+ def open_source(uri, source)
case uri.scheme
- when nil then f = open_path(uri.path) # assume file/path
+ when nil then f = open_path(source) # assume file/path
when 'file' then f = open(uri.path) # open path directly
when 'http' then f = open(uri.to_s) # open-uri will handle it
when 'https' then f = open(uri.to_s) # open-uri will handle it
when 'ftp' then f = open(uri.to_s) # open-uri will handle it
@@ -282,10 +286,12 @@
IncludeSource.new(f, uri)
end
def open_path(path)
if @allow_glob
- FileIterator.new(Dir.glob(path))
+ globs = Dir.glob(path)
+ globs.sort! if @sort_glob
+ FileIterator.new(globs)
else
File.open(path)
end
end
end # class Includer