Class: File
- Inherits:
-
Object
- Object
- File
- Defined in:
- lib/mast/core_ext.rb
Overview
Metaclass extensions for core File class.
Class Method Summary collapse
-
.gzip?(file) ⇒ Boolean
Is a file a gzip file?.
-
.parent?(file1, file2) ⇒ Boolean
Is path1 a parent directory of path2.
-
.read_list(filepath, chomp_string = '') ⇒ Object
Reads in a file, removes blank lines and remarks (lines starting with ‘#’) and then returns an array of all the remaining lines.
-
.reduce(*list) ⇒ Object
Reduce a list of files so there is no overlapping directory entries.
-
.sharedpath(file1, file2) ⇒ Object
Return the path shared.
Class Method Details
.gzip?(file) ⇒ Boolean
Is a file a gzip file?
7 8 9 10 11 12 13 |
# File 'lib/mast/core_ext.rb', line 7 def self.gzip?(file) open(file,'rb') { |f| return false unless f.getc == 0x1f return false unless f.getc == 0x8b } true end |
.parent?(file1, file2) ⇒ Boolean
Is path1 a parent directory of path2.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mast/core_ext.rb', line 44 def self.parent?(file1, file2) return false if File.identical?(file1, file2) afile1 = file1.split(/(\/|\\)/) afile2 = file2.split(/(\/|\\)/) overlap = [] i = 0; e1, e2 = afile1[i], afile2[i] while e1 && e2 && e1 == e2 overlap << e1 i += 1; e1, e2 = afile1[i], afile2[i] end return (overlap == afile1) end |
.read_list(filepath, chomp_string = '') ⇒ Object
Reads in a file, removes blank lines and remarks (lines starting with ‘#’) and then returns an array of all the remaining lines.
CREDIT: Trans
20 21 22 23 24 25 26 27 28 |
# File 'lib/mast/core_ext.rb', line 20 def self.read_list(filepath, chomp_string='') farr = nil farr = read(filepath).split("\n") farr.collect! { |line| l = line.strip.chomp(chomp_string) (l.empty? or l[0,1] == '#') ? nil : l } farr.compact end |
.reduce(*list) ⇒ Object
Reduce a list of files so there is no overlapping directory entries. This is useful when recursively descending a directory listing, so as to avoid and repeat entries.
TODO: Maybe globbing should occur in here too?
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mast/core_ext.rb', line 64 def self.reduce(*list) # TODO: list = list.map{ |f| File.cleanpath(f) } newlist = list.dup list.each do |file1| list.each do |file2| if parent?(file1, file2) newlist.delete(file2) end end end newlist end |
.sharedpath(file1, file2) ⇒ Object
Return the path shared.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mast/core_ext.rb', line 31 def self.sharedpath(file1, file2) afile1 = file1.split(/\/\\/) afile2 = file2.split(/\/\\/) overlap = [] i = 0; e1, e2 = afile1[i], afile2[i] while e1 && e2 && e1 == e2 overlap << e1 i += 1; e1, e2 = afile1[i], afile2[i] end return overlap.empty? ? false : overlap end |