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.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/libcfenjin/fileeditcached.rb', line 16

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.



12
13
14
# File 'lib/libcfenjin/fileeditcached.rb', line 12

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)


146
147
148
149
150
# File 'lib/libcfenjin/fileeditcached.rb', line 146

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

Instance Method Details

#AppendIfNoSuchLine(line) ⇒ Object



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

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

#AutoCreateObject



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

def AutoCreate
	@createnew = true
end

#changed?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/libcfenjin/fileeditcached.rb', line 29

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

#EmptyEntireFilePleaseObject



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

def EmptyEntireFilePlease
	@lines = Array.new
end

#HashCommentLinesContaining(s) ⇒ Object



113
114
115
116
117
# File 'lib/libcfenjin/fileeditcached.rb', line 113

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



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/libcfenjin/fileeditcached.rb', line 59

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



51
52
53
# File 'lib/libcfenjin/fileeditcached.rb', line 51

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

#ReplaceAll(repl, with) ⇒ Object



71
72
73
74
75
# File 'lib/libcfenjin/fileeditcached.rb', line 71

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

#ReplaceAllAppend(repl, with) ⇒ Object



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

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.



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

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



41
42
43
44
45
46
47
48
49
# File 'lib/libcfenjin/fileeditcached.rb', line 41

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/libcfenjin/fileeditcached.rb', line 119

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