Skip to content

Commit c652789

Browse files
authored
Removes unused attr_reader and memoizes Book#sheets (#114)
1 parent b6eeda1 commit c652789

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

lib/creek/book.rb

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
module Creek
99
class Creek::Book
1010
attr_reader :files,
11-
:sheets,
1211
:shared_strings,
1312
:with_headers
1413

@@ -28,32 +27,34 @@ def initialize path, options = {}
2827
end
2928

3029
def sheets
31-
doc = @files.file.open "xl/workbook.xml"
32-
xml = Nokogiri::XML::Document.parse doc
33-
namespaces = xml.namespaces
30+
@sheets ||= begin
31+
doc = @files.file.open "xl/workbook.xml"
32+
xml = Nokogiri::XML::Document.parse doc
33+
namespaces = xml.namespaces
3434

35-
cssPrefix = ''
36-
namespaces.each do |namespace|
37-
if namespace[1] == 'http://schemas.openxmlformats.org/spreadsheetml/2006/main' && namespace[0] != 'xmlns' then
38-
cssPrefix = namespace[0].split(':')[1]+'|'
35+
cssPrefix = ''
36+
namespaces.each do |namespace|
37+
if namespace[1] == 'http://schemas.openxmlformats.org/spreadsheetml/2006/main' && namespace[0] != 'xmlns' then
38+
cssPrefix = namespace[0].split(':')[1]+'|'
39+
end
3940
end
40-
end
4141

42-
rels_doc = @files.file.open "xl/_rels/workbook.xml.rels"
43-
rels = Nokogiri::XML::Document.parse(rels_doc).css("Relationship")
44-
@sheets = xml.css(cssPrefix+'sheet').map do |sheet|
45-
sheetfile = rels.find { |el| sheet.attr("r:id") == el.attr("Id") }.attr("Target")
46-
sheet = Sheet.new(
47-
self,
48-
sheet.attr("name"),
49-
sheet.attr("sheetid"),
50-
sheet.attr("state"),
51-
sheet.attr("visible"),
52-
sheet.attr("r:id"),
53-
sheetfile
54-
)
55-
sheet.with_headers = with_headers
56-
sheet
42+
rels_doc = @files.file.open "xl/_rels/workbook.xml.rels"
43+
rels = Nokogiri::XML::Document.parse(rels_doc).css("Relationship")
44+
xml.css(cssPrefix+'sheet').map do |sheet|
45+
sheetfile = rels.find { |el| sheet.attr("r:id") == el.attr("Id") }.attr("Target")
46+
sheet = Sheet.new(
47+
self,
48+
sheet.attr("name"),
49+
sheet.attr("sheetid"),
50+
sheet.attr("state"),
51+
sheet.attr("visible"),
52+
sheet.attr("r:id"),
53+
sheetfile
54+
)
55+
sheet.with_headers = with_headers
56+
sheet
57+
end
5758
end
5859
end
5960

0 commit comments

Comments
 (0)