Class: File

Inherits:
Object show all
Defined in:
lib/core_extensions.rb

Class Method Summary collapse

Class Method Details

.append(path, text) ⇒ Object



106
107
108
# File 'lib/core_extensions.rb', line 106

def self.append(path, text)
  open(path, 'a') { |f| f.write text }
end

.append_line(path, text) ⇒ Object



110
111
112
113
114
115
# File 'lib/core_extensions.rb', line 110

def self.append_line(path, text)
  open(path, 'a') do |f|
    f.write(text)
    f.puts
  end
end

.exclusive_append(path) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/core_extensions.rb', line 153

def self.exclusive_append(path)
  open(path, 'a+') do |f|
    f.flock(File::LOCK_EX)
    f.rewind
    string = yield f.read
    f.write(string)
    f.flush
    f.truncate(f.pos)
  end
end

.exclusive_modify(path) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/core_extensions.rb', line 141

def self.exclusive_modify(path)
  open(path, 'a+') do |f|
    f.flock(File::LOCK_EX)
    f.rewind
    string = yield f.read
    f.rewind
    f.write(string)
    f.flush
    f.truncate(f.pos)
  end
end

.exclusive_read(path) ⇒ Object



124
125
126
127
128
129
# File 'lib/core_extensions.rb', line 124

def self.exclusive_read(path)
  open(path, 'r') do |f|
    f.flock(File::LOCK_EX)
    f.read
  end
end

.exclusive_write(path, string) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/core_extensions.rb', line 131

def self.exclusive_write(path, string)
  open(path, 'a+') do |f|
    f.flock(File::LOCK_EX)
    f.rewind
    f.write(string)
    f.flush
    f.truncate(f.pos)
  end
end

.shared_read(path) ⇒ Object



117
118
119
120
121
122
# File 'lib/core_extensions.rb', line 117

def self.shared_read(path)
  open(path, 'r') do |f|
    f.flock(File::LOCK_SH)
    f.read
  end
end