Class: Cfruby::FileEditCached

Inherits:
Object
  • Object
show all
Includes:
Cfp_Cksum
Defined in:
lib/libcfenjin/fileeditcached.rb

Direct Known Subclasses

Cfp_EditFileCached

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cfp_Cksum

#cksum

Constructor Details

#initialize(fn) ⇒ FileEditCached

Returns a new instance of FileEditCached.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/libcfenjin/fileeditcached.rb', line 20

def initialize fn
	@filename = fn
	@lines = Array.new
	if File.exist? fn
		FileOps.flock(fn) { | f|
			f.each_line do | line |
				@lines.push line
			end
		}
	end
	@original_cksum = cksum @lines
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



16
17
18
# File 'lib/libcfenjin/fileeditcached.rb', line 16

def filename
  @filename
end

Class Method Details

.edit(fn) {|e| ... } ⇒ Object

This function allows you to use a block type:

EditFileCached.edit fn do | e |
  e.AppendIfNoSuchLine 'Yes!'
end

Yields:

  • (e)


150
151
152
153
154
# File 'lib/libcfenjin/fileeditcached.rb', line 150

def FileEditCached.edit fn
	e = FileEditCached.new fn
	yield e
	e.write
end

Instance Method Details

#AppendIfNoSuchLine(line) ⇒ Object



59
60
61
# File 'lib/libcfenjin/fileeditcached.rb', line 59

def AppendIfNoSuchLine line
	@lines.push forcenl(line) if NoSuchLine line,false
end

#AutoCreateObject



37
38
39
# File 'lib/libcfenjin/fileeditcached.rb', line 37

def AutoCreate
	@createnew = true
end

#changed?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/libcfenjin/fileeditcached.rb', line 33

def changed?
	cksum(@lines) != @original_cksum
end

#EmptyEntireFilePleaseObject



41
42
43
# File 'lib/libcfenjin/fileeditcached.rb', line 41

def EmptyEntireFilePlease
	@lines = Array.new
end

#HashCommentLinesContaining(s) ⇒ Object



117
118
119
120
121
# File 'lib/libcfenjin/fileeditcached.rb', line 117

def HashCommentLinesContaining s
	@lines.each_index do | i |
		@lines[i] = '#'+@lines[i] if @lines[i] =~ /#{s}/ and @lines[i] !~ /^#/
	end
end

#NoSuchLine(check, regex = true) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/libcfenjin/fileeditcached.rb', line 63

def NoSuchLine check, regex=true
	@lines.each do | line |
		if regex
			return false if line =~ /^#{check}$/
		else
			# p line.strip,check.strip
			return false if line.strip == check.strip
		end
	end
	true
end

#PrependIfNoSuchLine(line) ⇒ Object



55
56
57
# File 'lib/libcfenjin/fileeditcached.rb', line 55

def PrependIfNoSuchLine line
	@lines.unshift forcenl(line) if NoSuchLine line,false
end

#ReplaceAll(repl, with) ⇒ Object



75
76
77
78
79
# File 'lib/libcfenjin/fileeditcached.rb', line 75

def ReplaceAll repl,with
	@lines.each_index do | i |
		@lines[i] = @lines[i].gsub(/#{repl}/,with)
	end
end

#ReplaceAllAppend(repl, with) ⇒ Object



81
82
83
84
# File 'lib/libcfenjin/fileeditcached.rb', line 81

def ReplaceAllAppend repl,with
	ReplaceAll repl,with
	AppendIfNoSuchLine with 
end

#ReplaceSection(first, last, with, include_first_last = false) ⇒ Object

Edit a text file replacing a section with first line and last line passed as a regular expression.

Optionally you can include replacing the fist and last lines.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/libcfenjin/fileeditcached.rb', line 91

def ReplaceSection first,last,with,include_first_last=false
	@lines.each_index do | i |
		if @lines[i] =~ /^#{first}$/
			fi = i
fi -= 1 if include_first_last
			section = ''
			begin
				i = i+1
				if @lines[i] =~ /#{last}$/
					if section != with
						# $logger.log "EditFile replacing section #{first} with \"#{with}\""
			i += 1 if include_first_last
						before = @lines.slice(0..fi)
						after  = @lines.slice(i,@lines.size)
						# ---- insert new
						@lines = before + with.split("\n") + after
					end
					return
				end
				section += @lines[i]
			end while @lines[i]
		end
	end
	# $logger.warn "Section #{first} in #{@filename} does not exist!"
end

#Warning(msg = '') ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/libcfenjin/fileeditcached.rb', line 45

def Warning msg=''
	fullmsg = 
		"# WARNING: DO NOT CHANGE THIS FILE - it is maintained by #{ENV['USER']} #{msg} "+`date`
	if NoSuchLine '^# WARNING: DO NOT CHANGE THIS FILE'
		PrependIfNoSuchLine fullmsg
	else
		@lines[0] = fullmsg
	end
end

#write(dobackup = true) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/libcfenjin/fileeditcached.rb', line 123

def write dobackup=true
	if !File.exist? @filename and !@createnew
		# $logger.log "EditFile not creating (non existent file) #{@filename}"
		return
	end
	
	if changed?    
		if dobackup and File.exist? @filename
			FileOps.backup @filename
		end
		# $logger.log "EditFile writing updated #{@filename}"
		FileOps.flock(@filename,'w') { | f |
			@lines.each do | line |
				f.puts line
			end
		}
	else
		# $logger.log "EditFile #{@filename} unchanged"
	end
end