Class: RubyJard::Decorators::SourceDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_jard/decorators/source_decorator.rb

Overview

Decorator to decorate a file of source code It loads a window of source code that centers the current line position.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, lineno, window) ⇒ SourceDecorator

Returns a new instance of SourceDecorator.



11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby_jard/decorators/source_decorator.rb', line 11

def initialize(file, lineno, window)
  @file = file
  @lineno = lineno
  @window = window
  @window_start = 0
  @window_end = 0
  @codes = []

  decorate
end

Instance Attribute Details

#codesObject (readonly)

Returns the value of attribute codes.



9
10
11
# File 'lib/ruby_jard/decorators/source_decorator.rb', line 9

def codes
  @codes
end

#window_endObject (readonly)

Returns the value of attribute window_end.



9
10
11
# File 'lib/ruby_jard/decorators/source_decorator.rb', line 9

def window_end
  @window_end
end

#window_startObject (readonly)

Returns the value of attribute window_start.



9
10
11
# File 'lib/ruby_jard/decorators/source_decorator.rb', line 9

def window_start
  @window_start
end

Instance Method Details

#decorateObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby_jard/decorators/source_decorator.rb', line 22

def decorate
  begin
    file = File.open(@file)
  rescue Errno::ENOENT
    return
  end

  @window_start = @lineno - @window / 2
  @window_start = 1 if @window_start <= 0
  @window_end = @window_start + @window

  until file.eof?
    loc = file.gets
    next if file.lineno < @window_start
    break if @window_end < file.lineno

    @codes << loc
  end

  file.close
end