Class: OOXL::RowCache

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ooxl/xl_objects/row_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sheet_xml, shared_strings, options = {}) ⇒ RowCache

Returns a new instance of RowCache.



15
16
17
18
19
20
21
# File 'lib/ooxl/xl_objects/row_cache.rb', line 15

def initialize(sheet_xml, shared_strings, options = {})
  @shared_strings = shared_strings
  @sheet_xml = sheet_xml
  @options = options
  @row_cache = []
  @row_id_map = {}
end

Instance Attribute Details

#row_cacheObject (readonly)

built on-demand – use rows instead



8
9
10
# File 'lib/ooxl/xl_objects/row_cache.rb', line 8

def row_cache
  @row_cache
end

#row_id_mapObject (readonly)

built on-demand – use fetch_row_by_id instead



11
12
13
# File 'lib/ooxl/xl_objects/row_cache.rb', line 11

def row_id_map
  @row_id_map
end

#stylesObject

Returns the value of attribute styles.



5
6
7
# File 'lib/ooxl/xl_objects/row_cache.rb', line 5

def styles
  @styles
end

Instance Method Details

#[](id) ⇒ Object Also known as: row



23
24
25
# File 'lib/ooxl/xl_objects/row_cache.rb', line 23

def [](id)
  fetch_row_by_id(id)
end

#each(&block) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/ooxl/xl_objects/row_cache.rb', line 29

def each(&block)
  if @options[:padded_rows]
    padded_rows(&block)
  else
    rows(&block)
  end
end

#max_row_indexObject



64
65
66
67
68
69
70
71
72
# File 'lib/ooxl/xl_objects/row_cache.rb', line 64

def max_row_index
  return 0 if row_nodes.empty?

  if all_rows_loaded?
    row_cache.last.id.to_i
  else
    Row.extract_id(row_nodes.last).to_i
  end
end

#row_range(start_index, end_index) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ooxl/xl_objects/row_cache.rb', line 52

def row_range(start_index, end_index)
  return enum_for(:row_range, start_index, end_index) unless block_given?

  rows do |row|
    row_id = row.id.to_i
    next if row_id < start_index
    break if row_id > end_index

    yield row
  end
end

#rows(&block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ooxl/xl_objects/row_cache.rb', line 37

def rows(&block)
  # track yield count to know if caller broke out of loop
  rows_yielded = 0
  row_cache.each do |r|
    yield r if block_given?
    rows_yielded += 1
  end

  if !all_rows_loaded? && rows_yielded == row_cache.count
    parse_more_rows(&block)
  end

  row_cache
end