Class: Exfuz::Body

Inherits:
Object
  • Object
show all
Defined in:
lib/exfuz/body.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(texts: [], top: 0, bottom: 10) ⇒ Body

Returns a new instance of Body.



7
8
9
10
11
12
13
14
15
16
# File 'lib/exfuz/body.rb', line 7

def initialize(texts: [], top: 0, bottom: 10)
  @top = top
  @bottom = bottom

  @prev_row_to_text = {}

  @size = bottom - top + 1
  @texts = init_texts(texts)
  init_page(@texts, @size)
end

Instance Attribute Details

#bottomObject

Returns the value of attribute bottom.



5
6
7
# File 'lib/exfuz/body.rb', line 5

def bottom
  @bottom
end

#topObject (readonly)

Returns the value of attribute top.



5
6
7
# File 'lib/exfuz/body.rb', line 5

def top
  @top
end

Instance Method Details

#change(texts) ⇒ Object



42
43
44
# File 'lib/exfuz/body.rb', line 42

def change(texts)
  change_some(texts)
end

#change_all(texts) ⇒ Object



53
54
55
56
57
# File 'lib/exfuz/body.rb', line 53

def change_all(texts)
  @texts = init_texts(texts)
  @prev_row_to_text = @row_to_text
  init_page(texts, @size)
end

#change_some(texts) ⇒ Object



46
47
48
49
50
51
# File 'lib/exfuz/body.rb', line 46

def change_some(texts)
  texts.each do |line_num, text|
    @prev_row_to_text[row(line_num)] = @row_to_text[row(line_num)] if @row_to_text.key?(row(line_num))
    @row_to_text[row(line_num)] = text
  end
end

#clear_prevObject



70
71
72
# File 'lib/exfuz/body.rb', line 70

def clear_prev
  @prev_row_to_text = {}
end

#lines(overwrite: false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/exfuz/body.rb', line 59

def lines(overwrite: false)
  return @row_to_text unless overwrite

  overwrite_lines = @prev_row_to_text.each_with_object({}) do |(row, prev_text), result|
    current_text = @row_to_text[row] || ''
    result[row] = overwrite(current: current_text, prev: prev_text)
  end

  @row_to_text.merge(overwrite_lines)
end

#move_nextObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/exfuz/body.rb', line 18

def move_next
  return if @current_page == @max_page

  @current_page += 1

  offset = (@current_page - 1) * @size
  next_texts = @texts.slice(offset, @size).each_with_index.each_with_object({}) do |(text, idx), result|
    result[idx] = text
  end
  change_some(next_texts)
end

#move_prevObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/exfuz/body.rb', line 30

def move_prev
  return if @current_page == 1

  @current_page -= 1

  offset = (@current_page - 1) * @size
  prev_texts = @texts.slice(offset, @size).each_with_index.each_with_object({}) do |(text, idx), result|
    result[idx] = text
  end
  change_some(prev_texts)
end