Class: StackDeck::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/stackdeck/context.rb,
lib/stackdeck/postgres.rb

Direct Known Subclasses

File, PgProc

Defined Under Namespace

Classes: File, PgProc

Constant Summary collapse

CONTEXT =
7

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, lineno) ⇒ Context

Returns a new instance of Context.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/stackdeck/context.rb', line 12

def initialize(lines, lineno)
  if lineno
    index = lineno - 1
    pre_range, post_range = context_ranges(lines, index)
    @before_lineno = pre_range.begin
    @before = lines[pre_range]
    @line = lines[index]
    @after = lines[post_range]
  else
    @line = lines.join("\n")
  end
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



5
6
7
# File 'lib/stackdeck/context.rb', line 5

def after
  @after
end

#beforeObject (readonly)

Returns the value of attribute before.



5
6
7
# File 'lib/stackdeck/context.rb', line 5

def before
  @before
end

#before_linenoObject (readonly)

Returns the value of attribute before_lineno.



5
6
7
# File 'lib/stackdeck/context.rb', line 5

def before_lineno
  @before_lineno
end

#lineObject (readonly)

Returns the value of attribute line.



5
6
7
# File 'lib/stackdeck/context.rb', line 5

def line
  @line
end

Instance Method Details

#context_ranges(lines, index) ⇒ Object



6
7
8
9
10
11
# File 'lib/stackdeck/context.rb', line 6

def context_ranges(lines, index)
  first_line = [index - CONTEXT, 0].max
  last_line = [index + CONTEXT, lines.size].min

  [first_line...index, (index + 1)..last_line]
end