Class: Onlylogs::File
- Inherits:
-
Object
- Object
- Onlylogs::File
- Defined in:
- app/models/onlylogs/file.rb
Instance Attribute Summary collapse
-
#last_line_number ⇒ Object
readonly
Returns the value of attribute last_line_number.
-
#last_position ⇒ Object
readonly
Returns the value of attribute last_position.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #exist? ⇒ Boolean
- #go_to_position(position) ⇒ Object
- #grep(filter, regexp_mode: false, start_position: 0, end_position: nil, &block) ⇒ Object
-
#initialize(path, last_position: 0) ⇒ File
constructor
A new instance of File.
- #size ⇒ Object
- #text_file? ⇒ Boolean
- #watch(&block) ⇒ Object
Constructor Details
#initialize(path, last_position: 0) ⇒ File
Returns a new instance of File.
7 8 9 10 11 12 |
# File 'app/models/onlylogs/file.rb', line 7 def initialize(path, last_position: 0) self.path = path self.last_position = last_position self.last_line_number = 0 validate! end |
Instance Attribute Details
#last_line_number ⇒ Object
Returns the value of attribute last_line_number.
5 6 7 |
# File 'app/models/onlylogs/file.rb', line 5 def last_line_number @last_line_number end |
#last_position ⇒ Object
Returns the value of attribute last_position.
5 6 7 |
# File 'app/models/onlylogs/file.rb', line 5 def last_position @last_position end |
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'app/models/onlylogs/file.rb', line 5 def path @path end |
Class Method Details
.text_file?(path) ⇒ Boolean
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'app/models/onlylogs/file.rb', line 46 def self.text_file?(path) return false unless ::File.exist?(path) return false if ::File.zero?(path) # Read first chunk and check for null bytes (binary indicator) ::File.open(path, "rb") do |file| chunk = file.read(8192) || "" # If it contains null bytes, it's likely binary return false if chunk.include?("\x00") end true end |
Instance Method Details
#exist? ⇒ Boolean
38 39 40 |
# File 'app/models/onlylogs/file.rb', line 38 def exist? ::File.exist?(path) end |
#go_to_position(position) ⇒ Object
14 15 16 17 18 19 |
# File 'app/models/onlylogs/file.rb', line 14 def go_to_position(position) return if position < 0 self.last_position = position self.last_line_number = 0 end |
#grep(filter, regexp_mode: false, start_position: 0, end_position: nil, &block) ⇒ Object
60 61 62 63 64 |
# File 'app/models/onlylogs/file.rb', line 60 def grep(filter, regexp_mode: false, start_position: 0, end_position: nil, &block) Grep.grep(filter, path, regexp_mode: regexp_mode, start_position: start_position, end_position: end_position) do |line_number, content| yield Onlylogs::LogLine.new(line_number, content) end end |
#size ⇒ Object
34 35 36 |
# File 'app/models/onlylogs/file.rb', line 34 def size ::File.size(path) end |
#text_file? ⇒ Boolean
42 43 44 |
# File 'app/models/onlylogs/file.rb', line 42 def text_file? self.class.text_file?(path) end |
#watch(&block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/models/onlylogs/file.rb', line 21 def watch(&block) # return enum_for(:watch) unless block loop do sleep 0.5 new_lines = read_new_lines next if new_lines.empty? yield new_lines end end |