lib/zip-container/dir.rb in zip-container-2.1.0 vs lib/zip-container/dir.rb in zip-container-2.2.0
- old
+ new
@@ -46,30 +46,30 @@
#
# There are code examples available with the source code of this library.
class Dir < Container
extend Forwardable
- def_delegators :@container, :close, :each, :path, :pos, :pos=, :read,
- :rewind, :seek, :tell
+ def_delegators :@container, :close, :each, :path, :pos, :pos=, :rewind,
+ :seek, :tell
private_class_method :new
# :stopdoc:
def initialize(location)
super(location)
end
# :startdoc:
# :call-seq:
- # create(pathname, mimetype) -> document
- # create(pathname, mimetype) {|document| ...}
+ # create(pathname, mimetype) -> container
+ # create(pathname, mimetype) {|container| ...}
#
# Create a new (or convert an existing) directory as a ZipContainer with
# the specified mimetype.
def self.create(pathname, mimetype, &block)
::Dir.mkdir(pathname) unless ::File.directory?(pathname)
- ::File.write(full_path(MIMETYPE_FILE), mimetype)
+ ::File.write(::File.join(pathname, MIMETYPE_FILE), mimetype)
# Now open the newly created container.
c = new(pathname)
if block_given?
@@ -81,10 +81,57 @@
end
c
end
+ # :call-seq:
+ # read
+ # read(path) -> file contents
+ #
+ # Provides compatibility between directory and zip containers. If called
+ # without any parameters it acts like
+ # {::Dir.read}[http://ruby-doc.org/core-1.9.3/Dir.html#method-i-read] but
+ # if called with a path then it acts like
+ # {Zip::File#read}[http://www.rubydoc.info/gems/rubyzip/1.1.6/Zip/File#read-instance_method].
+ #
+ # Please see the documentation of the relevant method for more details.
+ def read(name = nil)
+ return @container.read if name.nil?
+
+ ::File.read(full_path(name))
+ end
+
+ # :stopdoc:
+ # For internal use only!
+ # This method and the Entry and Entries classes provide compatibility
+ # between zip-style and dir-style entries
+ def entries
+ Entries.new(@container)
+ end
+
+ class Entries
+ include Enumerable
+
+ Entry = Struct.new(:name, :ftype)
+
+ def initialize(dir)
+ @entries = []
+
+ dir.each do |name|
+ type = ::File.directory?(name) ? :directory : :file
+ @entries << Entry.new(name, type)
+ end
+ end
+
+ def each(&block)
+ @entries.each do |entry|
+ yield entry
+ end
+ end
+ end
+ # :startdoc:
+
private
# Prepend the full path of the directory name to whatever is passed in
# here. This is for internal use to ensure we are always operating on
# files within our container directory.
@@ -152,17 +199,9 @@
# :call-seq:
# pos = integer -> integer
#
# Equal to
# {::Dir.pos=}[http://ruby-doc.org/core-1.9.3/Dir.html#method-i-pos-3D]
-
- ##
- # :method: read
- # :call-seq:
- # read -> string or nil
- #
- # Equal to
- # {::Dir.read}[http://ruby-doc.org/core-1.9.3/Dir.html#method-i-read]
##
# :method: rewind
# :call-seq:
# rewind -> dir