Class: Glark::FileSet
- Inherits:
-
Object
- Object
- Glark::FileSet
- Includes:
- Enumerable, Loggable
- Defined in:
- lib/glark/util/io/fileset.rb
Overview
Files and directories. And standard input, just for fun.
Constant Summary collapse
- DEPTH_RE =
Regexp.new '\.\.\.(\d*)$'
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
Instance Method Summary collapse
-
#add_as_path(path) ⇒ Object
this is a path in the form /usr/bin:/home/me/projects.
- #add_fd(fname) ⇒ Object
- #add_files(fnames) ⇒ Object
- #each(&blk) ⇒ Object
- #handle_binary(pn, &blk) ⇒ Object
- #handle_directory(pn, depth, &blk) ⇒ Object
- #handle_file(pn, &blk) ⇒ Object
- #handle_pathname(pn, depth, &blk) ⇒ Object
- #handle_text(pn, &blk) ⇒ Object
-
#initialize(fnames, args) ⇒ FileSet
constructor
A new instance of FileSet.
- #size ⇒ Object
- #stdin? ⇒ Boolean
Constructor Details
#initialize(fnames, args) ⇒ FileSet
Returns a new instance of FileSet.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/glark/util/io/fileset.rb', line 18 def initialize fnames, args @max_depth = Depth.new args[:max_depth] @binary_files = args[:binary_files] || 'skip' @dir_criteria = args[:dir_criteria] @file_criteria = args[:file_criteria] @split_as_path = args[:split_as_path] @dir_to_max_depth = Hash.new @files = Array.new if fnames.size == 0 @files << '-' else add_files fnames end end |
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
14 15 16 |
# File 'lib/glark/util/io/fileset.rb', line 14 def files @files end |
Instance Method Details
#add_as_path(path) ⇒ Object
this is a path in the form /usr/bin:/home/me/projects
50 51 52 53 54 |
# File 'lib/glark/util/io/fileset.rb', line 50 def add_as_path path path.to_s.split(::File::PATH_SEPARATOR).each do |element| add_fd element end end |
#add_fd(fname) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/glark/util/io/fileset.rb', line 56 def add_fd fname pn = nil if md = DEPTH_RE.match(fname) val = md[1].empty? ? nil : md[1].to_i fname.sub! DEPTH_RE, '' fname = '.' if fname.empty? pn = Pathname.new fname @dir_to_max_depth[pn] = Depth.new val else pn = Pathname.new fname end return if pn.file? && @file_criteria.skipped?(pn) @files << pn end |
#add_files(fnames) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/glark/util/io/fileset.rb', line 39 def add_files fnames fnames.each do |fname| if @split_as_path add_as_path fname else add_fd fname end end end |
#each(&blk) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/glark/util/io/fileset.rb', line 77 def each &blk # to keep from cycling through links: @yielded_files = Array.new (0 ... @files.size).each do |idx| pn = @files[idx] if stdin? blk.call [ :text, '-' ] next end unless pn.readable? write "directory not readable: #{pn}" next end dirmax = @dir_to_max_depth[pn] || @max_depth handle_pathname pn, dirmax, &blk end end |
#handle_binary(pn, &blk) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/glark/util/io/fileset.rb', line 144 def handle_binary pn, &blk type = case @binary_files when 'binary' :binary when 'skip', 'without-match' return when 'decompress', 'read' :read when 'list' :list else :text end blk.call [ type, pn ] end |
#handle_directory(pn, depth, &blk) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/glark/util/io/fileset.rb', line 108 def handle_directory pn, depth, &blk return if @dir_criteria.skipped? pn, depth subdepth = depth - 1 pn.children.sort.each do |entry| next if @yielded_files.include?(entry) if entry.file? type = FileType.type entry.to_s next if type == FileType::BINARY && @binary_files == 'skip' end @yielded_files << entry handle_pathname entry, subdepth, &blk end end |
#handle_file(pn, &blk) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/glark/util/io/fileset.rb', line 124 def handle_file pn, &blk return if @file_criteria.skipped? pn type = FileType.type pn.to_s case type when FileType::TEXT handle_text pn, &blk when FileType::BINARY handle_binary pn, &blk when FileType::NONE write "no such file: #{pn}" when FileType::UNKNOWN write "unknown file type: #{pn}" end end |
#handle_pathname(pn, depth, &blk) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/glark/util/io/fileset.rb', line 98 def handle_pathname pn, depth, &blk if pn.directory? handle_directory pn, depth, &blk elsif pn.file? handle_file pn, &blk else write "unknown file type: #{pn}" end end |
#handle_text(pn, &blk) ⇒ Object
140 141 142 |
# File 'lib/glark/util/io/fileset.rb', line 140 def handle_text pn, &blk blk.call [ :text, pn ] end |
#size ⇒ Object
35 36 37 |
# File 'lib/glark/util/io/fileset.rb', line 35 def size @files.size end |
#stdin? ⇒ Boolean
73 74 75 |
# File 'lib/glark/util/io/fileset.rb', line 73 def stdin? @files.size == 1 && @files.first == '-' end |