Class: Patman
- Inherits:
-
Object
- Object
- Patman
- Defined in:
- lib/patman.rb,
lib/version.rb
Overview
Patman (Patch Manipulator) is a library for text file patching. It can also be used to extract information from files.
Defined Under Namespace
Classes: PatmanError, PatmanFileError, PatmanSearchError
Constant Summary
- VERSION =
"0.0.3"
Instance Attribute Summary (collapse)
-
- (Object) marks
Returns the value of attribute marks.
Class Method Summary (collapse)
-
+ (Object) read(file)
Create editing session with file.
- + (Object) version
Instance Method Summary (collapse)
-
- (Object) [](range)
Reference Patman content by range.
-
- (Object) append(text = nil)
Append after current position.
-
- (Object) blockline
Jump to line after block.
-
- (Object) clear
Clear Patman content and reset current line.
-
- (Object) copy(file)
Copy Patman content to file.
-
- (Object) delete(count = 1)
Delete current line.
-
- (Object) do_all(&blk)
Execute given block for all lines, i.e.
-
- (Object) do_for(start, count, &blk)
Execute given block starting from start by count, and update position.
-
- (Object) do_range(start, stop, &blk)
Execute given block between start and stop positions, and update position.
-
- (Object) edit
Mark content modified (explicit).
-
- (Boolean) edited?
Return true if content is modified.
-
- (Object) excursion(&blk)
Execute block, retain current position, and return block value.
-
- (Object) filename
Return Patman file name.
-
- (Object) find(re_or_str, forward = true)
Find Regexp or literal string forwards or backwards.
-
- (Object) firstline
Jump to first line.
-
- (Object) get(count = 1)
Get current line or lines by count.
-
- (Object) get_for(start, count)
Get lines starting from start by count.
-
- (Object) get_range(start, stop)
Get lines between start and stop positions inclusive.
-
- (Patman) initialize(file)
constructor
Create Patman object.
-
- (Object) insert(text = nil)
Insert line or lines (Array) to current position.
-
- (Object) insertfile(file)
Insert file to current position.
-
- (Object) lastline
Jump to last line.
-
- (Object) length
Return line count of Patman content.
-
- (Object) line(arg = nil)
Return or set line.
-
- (Object) lines(arg = nil)
Get or set all Patman content.
-
- (Object) mark(tag = nil)
Mark (store) current position to default or to named mark.
-
- (Object) peek(count = 0)
View line content around current position (by count).
-
- (Object) peek_ln(count = 0)
View line content with line numbers around current position (by count).
-
- (Object) read
Read file in.
-
- (Object) ref(line = nil)
Get current line or any line.
-
- (Object) search(re_or_str, forward = true)
Search Regexp or literal string forwards or backwards.
-
- (Object) set(text)
Set current line.
-
- (Object) step(dir = 1)
Step forward or backward current position.
-
- (Object) sub(from, to)
Substitution in current line.
-
- (Object) unmark(tag = nil)
Unmark (restore) current position from default or from named mark.
-
- (Object) update(&blk)
Update current line content (i.e. get&set) with the return value of the given block.
-
- (Object) view(arg1 = nil, arg2 = nil)
View line content.
-
- (Object) view_ln(arg1 = nil, arg2 = nil)
View line content with line numbers.
-
- (Object) write(file = @file)
Write Patman content to disk.
Constructor Details
Instance Attribute Details
- (Object) marks
Returns the value of attribute marks
15 16 17 |
# File 'lib/patman.rb', line 15 def marks @marks end |
Class Method Details
+ (Object) read(file)
Create editing session with file.
19 20 21 22 23 |
# File 'lib/patman.rb', line 19 def Patman.read( file ) p = Patman.new( file ) p.read p end |
+ (Object) version
3 4 5 |
# File 'lib/version.rb', line 3 def Patman.version Patman::VERSION end |
Instance Method Details
- (Object) [](range)
Reference Patman content by range.
114 115 116 |
# File 'lib/patman.rb', line 114 def []( range ) @lines[ range ] end |
- (Object) append(text = nil)
Append after current position.
183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/patman.rb', line 183 def append( text = nil ) @edited = true if text.kind_of? Array text.each do |txt| append( txt ) end else step @lines.insert( @line, text ) end self end |
- (Object) blockline
Jump to line after block.
96 97 98 99 100 101 |
# File 'lib/patman.rb', line 96 def blockline if @blockline @line = @blockline end self end |
- (Object) clear
Clear Patman content and reset current line.
216 217 218 219 220 221 |
# File 'lib/patman.rb', line 216 def clear @edited = true @lines = [] @line = 0 self end |
- (Object) copy(file)
Copy Patman content to file.
62 63 64 65 |
# File 'lib/patman.rb', line 62 def copy( file ) write( file ) self end |
- (Object) delete(count = 1)
Delete current line.
197 198 199 200 201 202 203 |
# File 'lib/patman.rb', line 197 def delete( count = 1 ) @edited = true count.times do |i| @lines.delete_at( @line ) end self end |
- (Object) do_all(&blk)
Execute given block for all lines, i.e. all positions. Block parameter is Patman.
297 298 299 |
# File 'lib/patman.rb', line 297 def do_all( &blk ) do_for( 1, length-1, &blk ) end |
- (Object) do_for(start, count, &blk)
Execute given block starting from start by count, and update position.
309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/patman.rb', line 309 def do_for( start, count, &blk ) line = @line @line = start-1 count.times do yield self @line += 1 end @blockline = @line @line = line self end |
- (Object) do_range(start, stop, &blk)
Execute given block between start and stop positions, and update position.
303 304 305 |
# File 'lib/patman.rb', line 303 def do_range( start, stop, &blk ) do_for( start, (stop-start+1), &blk ) end |
- (Object) edit
Mark content modified (explicit).
252 253 254 |
# File 'lib/patman.rb', line 252 def edit @edited = true end |
- (Boolean) edited?
Return true if content is modified.
257 258 259 |
# File 'lib/patman.rb', line 257 def edited? @edited end |
- (Object) excursion(&blk)
Execute block, retain current position, and return block value.
262 263 264 265 266 267 |
# File 'lib/patman.rb', line 262 def excursion( &blk ) line = @line ret = instance_eval( &blk ) @line = line ret end |
- (Object) filename
Return Patman file name.
247 248 249 |
# File 'lib/patman.rb', line 247 def filename @file end |
- (Object) find(re_or_str, forward = true)
Find Regexp or literal string forwards or backwards. Return true on success.
225 226 227 228 229 230 231 232 |
# File 'lib/patman.rb', line 225 def find( re_or_str, forward = true ) begin @line = search_with_exception( re_or_str, forward ) true rescue false end end |
- (Object) firstline
Jump to first line.
84 85 86 87 |
# File 'lib/patman.rb', line 84 def firstline @line = 0 self end |
- (Object) get(count = 1)
Get current line or lines by count.
119 120 121 122 123 124 125 |
# File 'lib/patman.rb', line 119 def get( count = 1 ) if count == 1 @lines[ @line ] else @lines[ @line .. (@line+count-1) ] end end |
- (Object) get_for(start, count)
Get lines starting from start by count.
327 328 329 |
# File 'lib/patman.rb', line 327 def get_for( start, count ) @lines[ (start-1) ... (start-1+count) ] end |
- (Object) get_range(start, stop)
Get lines between start and stop positions inclusive.
322 323 324 |
# File 'lib/patman.rb', line 322 def get_range( start, stop ) @lines[ (start-1) .. (stop-1) ] end |
- (Object) insert(text = nil)
Insert line or lines (Array) to current position.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/patman.rb', line 167 def insert( text = nil ) @edited = true if text.kind_of? Array line = @line step -1 text.each do |txt| append( txt ) end @line = line else @lines.insert( @line, text ) end self end |
- (Object) insertfile(file)
Insert file to current position.
206 207 208 209 210 211 212 213 |
# File 'lib/patman.rb', line 206 def insertfile( file ) @edited = true step -1 read_clean( file ).each do |line| append line end self end |
- (Object) lastline
Jump to last line.
90 91 92 93 |
# File 'lib/patman.rb', line 90 def lastline @line = @lines.length-1 self end |
- (Object) length
Return line count of Patman content.
242 243 244 |
# File 'lib/patman.rb', line 242 def length @lines.length end |
- (Object) line(arg = nil)
Return or set line.
68 69 70 71 72 73 74 75 |
# File 'lib/patman.rb', line 68 def line( arg = nil ) if arg @line = (arg-1) self else @line+1 end end |
- (Object) lines(arg = nil)
Get or set all Patman content.
104 105 106 107 108 109 110 111 |
# File 'lib/patman.rb', line 104 def lines( arg = nil ) if arg @edited = true @lines = arg else @lines end end |
- (Object) mark(tag = nil)
Mark (store) current position to default or to named mark.
270 271 272 273 274 275 276 277 278 |
# File 'lib/patman.rb', line 270 def mark( tag = nil ) if tag @marks[ tag ] = @line+1 self else @mark = @line+1 self end end |
- (Object) peek(count = 0)
View line content around current position (by count).
332 333 334 335 |
# File 'lib/patman.rb', line 332 def peek( count = 0 ) view_range( @line-count, @line+count+1 ) nil end |
- (Object) peek_ln(count = 0)
View line content with line numbers around current position (by count).
339 340 341 342 |
# File 'lib/patman.rb', line 339 def peek_ln( count = 0 ) view_range( @line-count, @line+count+1, true ) nil end |
- (Object) read
Read file in.
37 38 39 40 41 42 43 |
# File 'lib/patman.rb', line 37 def read if File.exist?( @file ) @lines = read_clean( @file ) else raise PatmanFileError end end |
- (Object) ref(line = nil)
Get current line or any line.
128 129 130 131 132 133 134 |
# File 'lib/patman.rb', line 128 def ref( line = nil ) if line @lines[ line-1 ] else @lines[ @line ] end end |
- (Object) search(re_or_str, forward = true)
Search Regexp or literal string forwards or backwards. Fail with expection (PatmanSearchError) if not found.
236 237 238 239 |
# File 'lib/patman.rb', line 236 def search( re_or_str, forward = true ) @line = search_with_exception( re_or_str, forward ) self end |
- (Object) set(text)
Set current line.
137 138 139 140 141 |
# File 'lib/patman.rb', line 137 def set( text ) @edited = true @lines[ @line ] = text self end |
- (Object) step(dir = 1)
Step forward or backward current position.
78 79 80 81 |
# File 'lib/patman.rb', line 78 def step( dir = 1 ) @line = @line + dir self end |
- (Object) sub(from, to)
Substitution in current line.
144 145 146 147 148 |
# File 'lib/patman.rb', line 144 def sub( from, to ) @edited = true @lines[ @line ] = @lines[ @line ].sub( from, to ) self end |
- (Object) unmark(tag = nil)
Unmark (restore) current position from default or from named mark.
282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/patman.rb', line 282 def unmark( tag = nil ) if tag && @marks[ tag ] @line = @marks[ tag ]-1 self elsif @mark @line = @mark-1 @mark = nil self else self end end |
- (Object) update(&blk)
Update current line content (i.e. get&set) with the return value of the given block. Hence last stmt should include the new line content.
160 161 162 163 164 |
# File 'lib/patman.rb', line 160 def update( &blk ) @edited = true set( yield( get ) ) self end |
- (Object) view(arg1 = nil, arg2 = nil)
View line content.
-
no args: view all
-
one arg: view from current onwards by count
-
two args: view given range
349 350 351 352 353 354 355 356 357 358 |
# File 'lib/patman.rb', line 349 def view( arg1 = nil, arg2 = nil ) if !arg1 && !arg2 view_range( 0, length ) elsif arg1 && !arg2 view_range( @line, @line+arg1 ) elsif arg1 && arg2 view_range( arg1-1, arg1-1+arg2 ) end nil end |
- (Object) view_ln(arg1 = nil, arg2 = nil)
View line content with line numbers.
-
no args: view all
-
one arg: view from current onwards by count
-
two args: view given range
365 366 367 368 369 370 371 372 373 374 |
# File 'lib/patman.rb', line 365 def view_ln( arg1 = nil, arg2 = nil ) if !arg1 && !arg2 view_range( 0, length, true ) elsif arg1 && !arg2 view_range( @line, @line+arg1, true ) elsif arg1 && arg2 view_range( arg1-1, arg1-1+arg2, true ) end nil end |
- (Object) write(file = @file)
Write Patman content to disk.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/patman.rb', line 46 def write( file = @file ) return unless @edited fh = File.open( file, 'w' ) @lines.each do |line| if line fh.puts line else fh.puts "" end end fh.close @edited = false self end |