Class: Files

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/Files.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Files

Returns a new instance of Files.



151
152
153
154
155
# File 'lib/Files.rb', line 151

def initialize(*args)
  options = args.extract_options!
  @args, @path, @pattern = args, options[:path], options[:pattern]
  load_metaprogrammed_methods
end

Instance Attribute Details

#filesObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/Files.rb', line 157

def files
  @files ||= (
    if !@args.empty?
      Files.with_attributes(@args.flatten)
    elsif path && pattern
      Files.find(path: path, pattern: pattern, return_files_object: false)
    elsif path
      Files.find(path: path, return_files_object: false)
    elsif pattern
      Files.find(pattern: pattern, return_files_object: false)
    else
      []
    end
  )
end

#pathObject

Returns the value of attribute path.



148
149
150
# File 'lib/Files.rb', line 148

def path
  @path
end

#patternObject

Returns the value of attribute pattern.



149
150
151
# File 'lib/Files.rb', line 149

def pattern
  @pattern
end

Class Method Details

.find(options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/Files.rb', line 66

def find(options = {})
  path, pattern, return_files_object = options[:path], options[:pattern], options[:return_files_object]
  case
  when path && pattern
    in_path___with_pattern___(path, pattern, return_files_object)
  when path && !pattern
    in_path___(path, return_files_object)
  when !path && pattern
    with_pattern___(pattern, return_files_object)
  when !path && !pattern
    here(return_files_object)
  end
end

.gsub!(filenames, replacement_pattern, replacement_text, selection_pattern = nil) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/Files.rb', line 127

def gsub!(filenames, replacement_pattern, replacement_text, selection_pattern = nil)
  filenames.each do |filename|
    File.open(filename, 'r+') do |f|
      f.gsub!(replacement_pattern, replacement_text, selection_pattern)
    end
  end
end

.here(return_files_object = true) ⇒ Object



104
105
106
# File 'lib/Files.rb', line 104

def here(return_files_object = true)
  in_path___matching___('./', '*', return_files_object)
end

.in_path___(path, return_files_object = true) ⇒ Object



94
95
96
# File 'lib/Files.rb', line 94

def in_path___(path, return_files_object = true)
  in_path___matching___(path, '*', return_files_object)
end

.in_path___with_pattern___(path, pattern, return_files_object = true) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/Files.rb', line 81

def in_path___with_pattern___(path, pattern, return_files_object = true)
  files = with_attributes(Dir["#{path}/#{pattern}"])
  if return_files_object
    files_object = Files.new
    files_object.files = files
    files_object
  else
    files
  end
end

.move(filenames, replacement_pattern, replacement_text) ⇒ Object



135
136
137
138
139
140
# File 'lib/Files.rb', line 135

def move(filenames, replacement_pattern, replacement_text)
  filenames.each do |filename|
    new_filename = filename.gsub(/#{replacement_pattern}/, replacement_text)
    FileUtils.mv(filename, new_filename)
  end
end

.with_attributes(paths) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/Files.rb', line 108

def with_attributes(paths)
  paths.collect do |path|
    begin
      f = {}
      f[:path] = path
      f[:size] = File.stat(path).size
      f[:ftype] = File.stat(path).ftype
      f[:mode] = File.stat(path).mode
      f[:gid] = File.stat(path).gid
      f[:uid] = File.stat(path).uid
      f[:ctime] = File.stat(path).ctime
      f[:mtime] = File.stat(path).mtime
      f[:atime] = File.stat(path).atime
      OpenStruct.new(f)
    rescue
    end
  end
end

.with_pattern___(pattern, return_files_object = true) ⇒ Object



99
100
101
# File 'lib/Files.rb', line 99

def with_pattern___(pattern, return_files_object = true)
  in_path___matching___('./', pattern, return_files_object)
end

Instance Method Details

#absolute_dirnamesObject



189
190
191
# File 'lib/Files.rb', line 189

def absolute_dirnames
  files.collect{|f| File.dirname(File.absolute_path(f.path))}
end

#absolute_pathsObject



181
182
183
# File 'lib/Files.rb', line 181

def absolute_paths
  files.collect{|f| File.expand_path(f.path)}
end

#dirnamesObject



185
186
187
# File 'lib/Files.rb', line 185

def dirnames
  files.collect{|f| File.dirname(f.path)}
end

#eachObject



173
174
175
# File 'lib/Files.rb', line 173

def each
  files.each{|f| yield f}
end

#gsub!(replacement_pattern, replacement_text) ⇒ Object



193
194
195
# File 'lib/Files.rb', line 193

def gsub!(replacement_pattern, replacement_text)
  Files.gsub!(paths, replacement_pattern, replacement_text)
end

#move(replacement_pattern, replacement_text) ⇒ Object



197
198
199
# File 'lib/Files.rb', line 197

def move(replacement_pattern, replacement_text)
  Files.move(paths, replacement_pattern, replacement_text)
end

#pathsObject



177
178
179
# File 'lib/Files.rb', line 177

def paths
  files.collect{|f| f.path}
end