Class: Tins::LinesFile
- Includes:
- Enumerable
- Defined in:
- lib/tins/lines_file.rb
Defined Under Namespace
Modules: LineExtension
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#line_number ⇒ Object
Returns the value of attribute line_number.
Class Method Summary collapse
- .for_file(file, line_number = nil) ⇒ Object
- .for_filename(filename, line_number = nil) ⇒ Object
- .for_lines(lines, line_number = nil) ⇒ Object
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #file_linenumber ⇒ Object
-
#initialize(lines, line_number = nil) ⇒ LinesFile
constructor
A new instance of LinesFile.
- #inspect ⇒ Object
- #last_line_number ⇒ Object
- #line ⇒ Object
- #match_backward(regexp, previous_after_match = false) ⇒ Object
- #match_forward(regexp, next_after_match = false) ⇒ Object
- #next! ⇒ Object
- #previous! ⇒ Object
- #rewind ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(lines, line_number = nil) ⇒ LinesFile
Returns a new instance of LinesFile.
27 28 29 30 31 32 33 34 35 |
# File 'lib/tins/lines_file.rb', line 27 def initialize(lines, line_number = nil) @lines = lines @lines.each_with_index do |line, i| line.extend LineExtension line.instance_variable_set :@line_number, i + 1 line.instance_variable_set :@lines_file, self end instance_variable_set :@line_number, line_number || (@lines.empty? ? 0 : 1) end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
37 38 39 |
# File 'lib/tins/lines_file.rb', line 37 def filename @filename end |
#line_number ⇒ Object
Returns the value of attribute line_number.
39 40 41 |
# File 'lib/tins/lines_file.rb', line 39 def line_number @line_number end |
Class Method Details
.for_file(file, line_number = nil) ⇒ Object
17 18 19 20 21 |
# File 'lib/tins/lines_file.rb', line 17 def self.for_file(file, line_number = nil) obj = new(file.readlines, line_number) obj.filename = file.path obj end |
.for_filename(filename, line_number = nil) ⇒ Object
11 12 13 14 15 |
# File 'lib/tins/lines_file.rb', line 11 def self.for_filename(filename, line_number = nil) obj = new(File.readlines(filename), line_number) obj.filename = filename obj end |
.for_lines(lines, line_number = nil) ⇒ Object
23 24 25 |
# File 'lib/tins/lines_file.rb', line 23 def self.for_lines(lines, line_number = nil) new(lines, line_number) end |
Instance Method Details
#each(&block) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/tins/lines_file.rb', line 73 def each(&block) empty? and return self old_line_number = line_number 1.upto(last_line_number) do |number| self.line_number = number block.call(line) end self ensure self.line_number = old_line_number end |
#empty? ⇒ Boolean
69 70 71 |
# File 'lib/tins/lines_file.rb', line 69 def empty? @lines.empty? end |
#file_linenumber ⇒ Object
91 92 93 |
# File 'lib/tins/lines_file.rb', line 91 def file_linenumber "#{filename}:#{line_number}" end |
#inspect ⇒ Object
117 118 119 |
# File 'lib/tins/lines_file.rb', line 117 def inspect "#<#{self.class}: #{to_s.inspect}>" end |
#last_line_number ⇒ Object
65 66 67 |
# File 'lib/tins/lines_file.rb', line 65 def last_line_number @lines.size end |
#line ⇒ Object
86 87 88 89 |
# File 'lib/tins/lines_file.rb', line 86 def line index = line_number - 1 @lines[index] if index >= 0 end |
#match_backward(regexp, previous_after_match = false) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/tins/lines_file.rb', line 95 def match_backward(regexp, previous_after_match = false) begin if line =~ regexp previous_after_match and previous! return $~.captures end end while previous! end |
#match_forward(regexp, next_after_match = false) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/tins/lines_file.rb', line 104 def match_forward(regexp, next_after_match = false) begin if line =~ regexp next_after_match and next! return $~.captures end end while next! end |
#next! ⇒ Object
46 47 48 49 50 |
# File 'lib/tins/lines_file.rb', line 46 def next! old = line_number self.line_number += 1 line_number > old ? self : nil end |
#previous! ⇒ Object
52 53 54 55 56 |
# File 'lib/tins/lines_file.rb', line 52 def previous! old = line_number self.line_number -= 1 line_number < old ? self : nil end |
#rewind ⇒ Object
41 42 43 44 |
# File 'lib/tins/lines_file.rb', line 41 def rewind self.line_number = 1 self end |
#to_s ⇒ Object
113 114 115 |
# File 'lib/tins/lines_file.rb', line 113 def to_s "#{line_number} #{line.chomp}" end |