Class: HistoryReader

Inherits:
Object
  • Object
show all
Defined in:
lib/readers/history_reader.rb

Direct Known Subclasses

OhMyZshReader

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ HistoryReader

Returns a new instance of HistoryReader.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/readers/history_reader.rb', line 4

def initialize(filename)
  @filename = filename

  path = File.expand_path(filename)
  if File.exist?(path)
    file = File.open(path, 'r')
  else
    raise "File does not exist." + path.inspect
  end

  @file_contents = ""

  if String.method_defined?(:encode)
    @file_contents = file.read.encode!('UTF-8', 'UTF-8', :invalid => :replace)
  else
    ic = Iconv.new('UTF-8', 'UTF-8')
    file.each do |l|
      begin
        @file_contents += ic.iconv(l) + "\n"
      rescue
        puts "Encoding error: #{l}"
      end
    end
  end
end

Instance Attribute Details

#file_contentsObject

Returns the value of attribute file_contents.



2
3
4
# File 'lib/readers/history_reader.rb', line 2

def file_contents
  @file_contents
end

#filenameObject

Returns the value of attribute filename.



2
3
4
# File 'lib/readers/history_reader.rb', line 2

def filename
  @filename
end

Class Method Details

.filesObject



43
44
45
# File 'lib/readers/history_reader.rb', line 43

def self.files
  possible_files.map{|f| File.expand_path(f) }.select{|f| File.exist?(f) }
end

.timestamps?(filename) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/readers/history_reader.rb', line 34

def self.timestamps?(filename)
  reader = HistoryReader.new(filename)
  reader.timestamps?
end

Instance Method Details

#shell_commandsObject



39
40
41
# File 'lib/readers/history_reader.rb', line 39

def shell_commands
  raw_lines
end

#timestamps?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/readers/history_reader.rb', line 30

def timestamps?
  starts_with_timestamp?(raw_lines.first)
end