Class: Tins::LinesFile

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/tins/lines_file.rb

Defined Under Namespace

Modules: LineExtension

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#filenameObject

Returns the value of attribute filename.



37
38
39
# File 'lib/tins/lines_file.rb', line 37

def filename
  @filename
end

#line_numberObject

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

Returns:

  • (Boolean)


69
70
71
# File 'lib/tins/lines_file.rb', line 69

def empty?
  @lines.empty?
end

#file_linenumberObject



91
92
93
# File 'lib/tins/lines_file.rb', line 91

def file_linenumber
  "#{filename}:#{line_number}"
end

#inspectObject



117
118
119
# File 'lib/tins/lines_file.rb', line 117

def inspect
  "#<#{self.class}: #{to_s.inspect}>"
end

#last_line_numberObject



65
66
67
# File 'lib/tins/lines_file.rb', line 65

def last_line_number
  @lines.size
end

#lineObject



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

#rewindObject



41
42
43
44
# File 'lib/tins/lines_file.rb', line 41

def rewind
  self.line_number = 1
  self
end

#to_sObject



113
114
115
# File 'lib/tins/lines_file.rb', line 113

def to_s
  "#{line_number} #{line.chomp}"
end