Class: Glark::FileSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Logue::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

Instance Method Summary collapse

Constructor Details

#initialize(fnames, args) ⇒ FileSet

Returns a new instance of FileSet.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/glark/util/io/fileset.rb', line 19

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

#filesObject (readonly)

Returns the value of attribute files.



15
16
17
# File 'lib/glark/util/io/fileset.rb', line 15

def files
  @files
end

Instance Method Details

#add_as_path(path) ⇒ Object

this is a path in the form /usr/bin:/home/me/projects



51
52
53
54
55
# File 'lib/glark/util/io/fileset.rb', line 51

def add_as_path path
  path.to_s.split(::File::PATH_SEPARATOR).each do |element|
    add_fd element
  end
end

#add_fd(fname) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/glark/util/io/fileset.rb', line 57

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



40
41
42
43
44
45
46
47
48
# File 'lib/glark/util/io/fileset.rb', line 40

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/glark/util/io/fileset.rb', line 78

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/glark/util/io/fileset.rb', line 145

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



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/glark/util/io/fileset.rb', line 109

def handle_directory pn, depth, &blk
  return if @dir_criteria.skipped? pn, depth

  subdepth = depth - 1

  pn.children.sort.each do |entry|
    if entry.file?
      type = FileType.type entry.to_s
      next if type == FileType::BINARY && @binary_files == 'skip'
    end
    handle_pathname entry, subdepth, &blk
  end
end

#handle_file(pn, &blk) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/glark/util/io/fileset.rb', line 123

def handle_file pn, &blk
  return if @file_criteria.skipped? pn
  return if @yielded_files.include? pn
  @yielded_files << 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



99
100
101
102
103
104
105
106
107
# File 'lib/glark/util/io/fileset.rb', line 99

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



141
142
143
# File 'lib/glark/util/io/fileset.rb', line 141

def handle_text pn, &blk
  blk.call [ :text, pn ]
end

#sizeObject



36
37
38
# File 'lib/glark/util/io/fileset.rb', line 36

def size
  @files.size
end

#stdin?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/glark/util/io/fileset.rb', line 74

def stdin?
  @files.size == 1 && @files.first == '-'
end