Class: RailsEmbedEditor::FileManager
- Inherits:
-
Object
- Object
- RailsEmbedEditor::FileManager
- Defined in:
- lib/rails_embed_editor/file_manager.rb
Instance Attribute Summary collapse
-
#first_line ⇒ Object
Returns the value of attribute first_line.
-
#last_line ⇒ Object
Returns the value of attribute last_line.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(filename, first_line = nil, last_line = nil) ⇒ FileManager
constructor
A new instance of FileManager.
- #read_lines ⇒ Object
- #read_text ⇒ Object
- #save_lines(lines) ⇒ Object
- #save_text(text) ⇒ Object
Constructor Details
#initialize(filename, first_line = nil, last_line = nil) ⇒ FileManager
Returns a new instance of FileManager.
5 6 7 8 9 |
# File 'lib/rails_embed_editor/file_manager.rb', line 5 def initialize(filename, first_line = nil, last_line=nil) @filename = filename @first_line = first_line @last_line = last_line end |
Instance Attribute Details
#first_line ⇒ Object
Returns the value of attribute first_line.
3 4 5 |
# File 'lib/rails_embed_editor/file_manager.rb', line 3 def first_line @first_line end |
#last_line ⇒ Object
Returns the value of attribute last_line.
3 4 5 |
# File 'lib/rails_embed_editor/file_manager.rb', line 3 def last_line @last_line end |
Class Method Details
.from_around(filename, line, radius = 2) ⇒ Object
19 20 21 22 23 |
# File 'lib/rails_embed_editor/file_manager.rb', line 19 def self.from_around(filename, line, radius=2) first_line = line - radius last_line = line + radius RailsEmbedEditor::FileManager.new(filename, first_line, last_line) end |
.from_options(filename, options) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/rails_embed_editor/file_manager.rb', line 11 def self.(filename, ) if [:first_line].nil? from_around(filename, [:line].to_i, [:radius].to_i) else RailsEmbedEditor::FileManager.new(filename, [:first_line].to_i, [:last_line].to_i) end end |
Instance Method Details
#read_lines ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/rails_embed_editor/file_manager.rb', line 25 def read_lines lines = [] File.open(@filename, 'r') do |f| lines = f.readlines[@first_line-1...@last_line] end lines end |
#read_text ⇒ Object
33 34 35 |
# File 'lib/rails_embed_editor/file_manager.rb', line 33 def read_text read_lines.join() end |
#save_lines(lines) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rails_embed_editor/file_manager.rb', line 41 def save_lines(lines) all_lines = [] File.open(@filename, 'r') do |f| all_lines = f.readlines end all_lines = all_lines[0...@first_line-1] + lines + all_lines[@last_line..-1] File.open(@filename, 'w') do |f| all_lines.each do |line| f.write(line) end end end |
#save_text(text) ⇒ Object
37 38 39 |
# File 'lib/rails_embed_editor/file_manager.rb', line 37 def save_text(text) save_lines(text.split(/\r?\n/).map { |x| "#{x}\n" }) end |