2010-11-30 16:27:59 +08:00
|
|
|
module Zip
|
2013-06-03 15:56:24 +08:00
|
|
|
class EntrySet #:nodoc:all
|
2010-11-30 16:27:59 +08:00
|
|
|
include Enumerable
|
2013-06-03 02:33:03 +08:00
|
|
|
attr_accessor :entry_set, :entry_order
|
|
|
|
|
|
|
|
def initialize(an_enumerable = [])
|
2010-11-30 16:27:59 +08:00
|
|
|
super()
|
2013-06-03 02:33:03 +08:00
|
|
|
@entry_set = {}
|
|
|
|
an_enumerable.each { |o| push(o) }
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def include?(entry)
|
2013-06-03 02:33:03 +08:00
|
|
|
@entry_set.include?(to_key(entry))
|
2012-03-26 14:57:14 +08:00
|
|
|
end
|
|
|
|
|
2015-03-11 22:41:03 +08:00
|
|
|
def find_entry(entry)
|
2015-03-09 02:40:43 +08:00
|
|
|
@entry_set[to_key(entry)]
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def <<(entry)
|
2013-06-03 02:33:03 +08:00
|
|
|
@entry_set[to_key(entry)] = entry
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2013-06-03 02:33:03 +08:00
|
|
|
|
2015-03-21 18:14:21 +08:00
|
|
|
alias push <<
|
2010-11-30 16:27:59 +08:00
|
|
|
|
|
|
|
def size
|
2013-06-03 02:33:03 +08:00
|
|
|
@entry_set.size
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2013-06-03 02:33:03 +08:00
|
|
|
|
2015-03-21 18:14:21 +08:00
|
|
|
alias length size
|
2010-11-30 16:27:59 +08:00
|
|
|
|
|
|
|
def delete(entry)
|
2013-10-21 04:09:40 +08:00
|
|
|
if @entry_set.delete(to_key(entry))
|
2013-06-03 02:33:03 +08:00
|
|
|
entry
|
|
|
|
else
|
2012-10-20 03:24:02 +08:00
|
|
|
nil
|
2013-06-03 02:33:03 +08:00
|
|
|
end
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2013-06-03 02:33:03 +08:00
|
|
|
def each(&block)
|
2014-07-24 06:09:56 +08:00
|
|
|
@entry_set = sorted_entries.dup.each do |_, value|
|
2013-10-21 04:09:40 +08:00
|
|
|
block.call(value)
|
2012-10-20 03:24:02 +08:00
|
|
|
end
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def entries
|
2014-07-24 06:09:56 +08:00
|
|
|
sorted_entries.values
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# deep clone
|
|
|
|
def dup
|
2015-03-23 23:57:56 +08:00
|
|
|
EntrySet.new(@entry_set.values.map(&:dup))
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2012-02-01 03:46:42 +08:00
|
|
|
def ==(other)
|
2013-06-03 15:56:24 +08:00
|
|
|
return false unless other.kind_of?(EntrySet)
|
2013-10-21 04:09:40 +08:00
|
|
|
@entry_set.values == other.entry_set.values
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def parent(entry)
|
2013-06-03 02:33:03 +08:00
|
|
|
@entry_set[to_key(entry.parent_as_string)]
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2015-03-23 01:03:50 +08:00
|
|
|
def glob(pattern, flags = ::File::FNM_PATHNAME | ::File::FNM_DOTMATCH)
|
2012-05-19 05:57:19 +08:00
|
|
|
entries.map do |entry|
|
|
|
|
next nil unless ::File.fnmatch(pattern, entry.name.chomp('/'), flags)
|
|
|
|
yield(entry) if block_given?
|
|
|
|
entry
|
|
|
|
end.compact
|
2013-06-03 02:33:03 +08:00
|
|
|
end
|
2010-11-30 16:27:59 +08:00
|
|
|
|
|
|
|
protected
|
2015-03-21 16:19:43 +08:00
|
|
|
|
2014-07-24 06:09:56 +08:00
|
|
|
def sorted_entries
|
|
|
|
::Zip.sort_entries ? Hash[@entry_set.sort] : @entry_set
|
|
|
|
end
|
2012-03-26 14:57:14 +08:00
|
|
|
|
|
|
|
private
|
2015-03-21 16:19:43 +08:00
|
|
|
|
2012-03-26 14:57:14 +08:00
|
|
|
def to_key(entry)
|
2015-03-09 02:38:57 +08:00
|
|
|
k = entry.to_s.chomp('/')
|
|
|
|
k.downcase! if ::Zip.case_insensitive_match
|
|
|
|
k
|
2012-03-26 14:57:14 +08:00
|
|
|
end
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Copyright (C) 2002, 2003 Thomas Sondergaard
|
|
|
|
# rubyzip is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the ruby license.
|