Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/googletastic/ext/file.rb

Constant Summary collapse

NON_TEXT_FILE_EXTENSIONS =
[
  "jpg", "gif", "jpeg", "pdf", "swf", "swc", "psd", "ai", "ae", "png", "tiff", "mp3",
  "ttf", "otf", "bmp"
].collect! { |item| ".#{item}" }

Class Method Summary collapse

Class Method Details

.clean(file) ⇒ Object

removes file content



12
13
14
# File 'lib/googletastic/ext/file.rb', line 12

def self.clean(file)
  File.truncate(file, 0)
end

.empty?(file) ⇒ Boolean

easier to read method to see if file’s empty

Returns:

  • (Boolean)


30
31
32
# File 'lib/googletastic/ext/file.rb', line 30

def self.empty?(file)
  File.zero?(file)
end

.has_ext?(name) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/googletastic/ext/file.rb', line 60

def self.has_ext?(name)
  File::NON_TEXT_FILE_EXTENSIONS[name.downcase]
end

.insert_lines(target, inserts, pattern, offset = 0, duplicates = false, &block) ⇒ Object

Inserts lines into a file after a specified pattern’s last occurrence. This method first reads the file into memory as a string and checks to see if any of the lines you’re trying to add are already there. If they are, it removes them from the inserts. Then it goes through each line, finds the index of the last occurrence of that pattern, and splices your inserts into the lines array, and writes them back to the file.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/googletastic/ext/file.rb', line 173

def self.insert_lines(target, inserts, pattern, offset = 0, duplicates = false, &block)
  index = -1
  content = IO.read(target)
  accepted_inserts = []
  inserts.each_with_index do |insert, i|
    accepted_inserts << insert unless !duplicates and content.match(insert.strip)
  end
#    accepted_inserts[0] = "\n#{accepted_inserts[0]}" # next line
  File.open(target, 'r+') do |f|
    lines = f.readlines.each_with_index do |line, i|
      yield line if block_given?
      if pattern and line.match(pattern) then index = i end
    end
    index = index == -1 ? lines.length - 1 : index + offset
    lines = lines.splice(accepted_inserts, index)
    f.pos = 0
    f.print lines
    f.truncate(f.pos)
  end
end

.list_directories(target, includes = [".*"], excludes = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/googletastic/ext/file.rb', line 121

def self.list_directories(target, includes = [".*"], excludes = nil)
  dirs = []
  self.matches(target, false, includes, excludes) do |path|
    if FileTest.directory?(path)
      yield path if block_given?
      if !path.nil? and !path.eql?("") then dirs << path end
    end
  end
  dirs
end

.list_files(target, files_only = true, includes = [".*"], excludes = nil) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/googletastic/ext/file.rb', line 112

def self.list_files(target, files_only = true, includes = [".*"], excludes = nil)
  files = []
  self.matches(target, files_only, includes, excludes) do |path|
    yield path if block_given?
    if !path.nil? and !path.eql?("") then files << path end
  end
  files
end

.matches(target, files_only = true, includes = [".*"], excludes = nil) ⇒ Object

Matches the file name



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/googletastic/ext/file.rb', line 65

def self.matches(target, files_only = true, includes = [".*"], excludes = nil)
  includes ||= [".*"]
  # need to test this to make sure it always works
  includes = includes.collect { |e| e.is_a?(Regexp) ? e : Regexp.new(e) }
  excludes = excludes.collect { |e| e.is_a?(Regexp) ? e : Regexp.new(Regexp.escape(e)) } unless excludes.nil?
  Find.find(target) do |path|
    if excludes and excludes.any? {|e| path.match e }
      next
    elsif FileTest.directory?(path)
     yield path if !files_only
     next
    else #we have a file
      #name = "." + File.basename(path.downcase).split(".").last
      yield path if includes.any? {|e| path.match e }
    end  
  end
end

.move_all(target, includes = [".*"], excludes = nil) ⇒ Object

moves all files from random places to a single directory



108
109
110
# File 'lib/googletastic/ext/file.rb', line 108

def self.move_all(target, includes = [".*"], excludes = nil)
  
end

.num_files(target, files_only = true, includes = [".*"], excludes = nil) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/googletastic/ext/file.rb', line 99

def self.num_files(target, files_only = true, includes = [".*"], excludes = nil)
  n = 0
  self.matches(target, files_only, includes, excludes) do |path|
    n += 1
  end
  n
end

.num_lines(target, files_only = true, includes = [".*"], excludes = nil) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/googletastic/ext/file.rb', line 83

def self.num_lines(target, files_only = true, includes = [".*"], excludes = nil)
  n = 0
  self.matches(target, files_only, includes, excludes) do |path|
    n += File.readlines(path).size
  end
  n
end

.num_words(target, files_only = true, includes = [".*"], excludes = nil) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/googletastic/ext/file.rb', line 91

def self.num_words(target, files_only = true, includes = [".*"], excludes = nil)
  n = 0
  self.matches(target, files_only, includes, excludes) do |path|
    IO.read(path).scan(/\b\w+\b/) { n += 1 }
  end
  n
end

.remove_lines(target, patterns) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/googletastic/ext/file.rb', line 194

def self.remove_lines(target, patterns)
  new_lines = []
  if !patterns.nil? and patterns.length > 0
    patterns.map! {|p| Regexp.escape(p) }
    pattern = Regexp.new(patterns.join("|"))
    File.open(target, 'r+') do |f|
      f.readlines.each { |line| new_lines << line unless line.match(pattern) }
      f.pos = 0
      f.print new_lines
      f.truncate(f.pos)
    end
  end
  new_lines
end

.rename_all(target, files_only = false, includes = [".*"], excludes = [], &block) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/googletastic/ext/file.rb', line 157

def self.rename_all(target, files_only = false, includes = [".*"], excludes = [], &block)
  self.list_files(target, files_only, includes, excludes) do |path|
    dirname = File.dirname(path)
    filetype = File.basename(path).split(".").last
    filename = yield path, File.basename(path).split("/").last if block_given?
    new_path = dirname + "/" + filename
    File.rename(path, new_path)
  end
end

.replace(a, b, target, includes = [".*"], excludes = [], &block) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/googletastic/ext/file.rb', line 132

def self.replace(a, b, target, includes = [".*"], excludes = [], &block)
  a = Regexp.new(Regexp.escape(a))
  self.replace_all(target, includes, excludes) do |line|
    if block_given?
      yield line, a, b
    else            # modify lines
      line.gsub!(a, b) unless line.match(a).nil?
    end           # truncate to new length
  end
end

.replace_all(target, includes = [".*"], excludes = [], &block) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/googletastic/ext/file.rb', line 143

def self.replace_all(target, includes = [".*"], excludes = [], &block)
#  files = self.list_files(target, true, includes, excludes).sort do |a,b|
 #   Regexp.num_matches(a, "/") <=> Regexp.num_matches(b, "/")
#  end
  self.list_files(target, true, includes, excludes) do |path|
    File.open(path, 'r+') do |f|    # open file for update
      lines = f.readlines.collect! { |line| yield path, line if block_given? }
      f.pos = 0                     # back to start
      f.print lines                 # write out modified lines
      f.truncate(f.pos)             # truncate to new length
    end
  end
end

.scan(target, pattern, position = 0) ⇒ Object



209
210
211
212
213
214
215
216
217
218
# File 'lib/googletastic/ext/file.rb', line 209

def self.scan(target, pattern, position = 0)
  new_lines = []
  File.open(target, 'r+') do |f|
    lines = f.readlines.each do |line|
      match = line.match(pattern)
      new_lines << match.captures[position] unless match.nil?
    end
  end
  new_lines
end

.sort(files) ⇒ Object

sorts the names of files



21
22
23
24
25
26
27
# File 'lib/googletastic/ext/file.rb', line 21

def self.sort(files)
  files.sort do |x,y|
    xs = x.before_last("/")
    ys = y.before_last("/")
    xs <=> ys
  end
end

.template(target_path, template_path, bind) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/googletastic/ext/file.rb', line 43

def self.template(target_path, template_path, bind)
  FileUtils.touch(target_path) unless File.exists?(target_path)
  File.clean(target_path)
  File.open(target_path, 'r+') do |f|
    # parse the template file into this
    f.print ERB.new(IO.read(template_path), nil, '-').result(bind) 
  end
end

.touch(file) ⇒ Object



16
17
18
# File 'lib/googletastic/ext/file.rb', line 16

def self.touch(file)
  FileUtils.touch(file) unless File.exists?(file)
end

.uniq_dirs(paths) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/googletastic/ext/file.rb', line 34

def self.uniq_dirs(paths)
  dirs = []
  paths.each do |file|
    dirs << file.before_last("/") if file =~ /\//
  end
  dirs.uniq!
  dirs
end

.write(file, content) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/googletastic/ext/file.rb', line 52

def self.write(file, content)
  File.touch(file)
  File.clean(file)
  File.open(file, 'r+') do |f|
    f.print content
  end
end