Skip to content

Commit 699ff68

Browse files
committed
Use magic string literal comment over String#freeze
Use Time#to_datetime over Datetime.civil
1 parent b7a58e2 commit 699ff68

12 files changed

+94
-78
lines changed

lib/roo.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'roo/version'
24
require 'roo/constants'
35
require 'roo/errors'
@@ -10,7 +12,7 @@ module Roo
1012
autoload :Excelx, 'roo/excelx'
1113
autoload :CSV, 'roo/csv'
1214

13-
TEMP_PREFIX = 'roo_'.freeze
15+
TEMP_PREFIX = 'roo_'
1416

1517
CLASS_FOR_EXTENSION = {
1618
ods: Roo::OpenOffice,

lib/roo/base.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class Roo::Base
1919
include Roo::Formatters::XML
2020
include Roo::Formatters::YAML
2121

22-
MAX_ROW_COL = 999_999.freeze
23-
MIN_ROW_COL = 0.freeze
22+
MAX_ROW_COL = 999_999
23+
MIN_ROW_COL = 0
2424

2525
attr_reader :headers
2626

lib/roo/constants.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# frozen_string_literal: true
2+
13
module Roo
2-
ROO_EXCEL_NOTICE = "Excel support has been extracted to roo-xls due to its dependency on the GPL'd spreadsheet gem. Install roo-xls to use Roo::Excel.".freeze
3-
ROO_EXCELML_NOTICE = "Excel SpreadsheetML support has been extracted to roo-xls. Install roo-xls to use Roo::Excel2003XML.".freeze
4-
ROO_GOOGLE_NOTICE = "Google support has been extracted to roo-google. Install roo-google to use Roo::Google.".freeze
4+
ROO_EXCEL_NOTICE = "Excel support has been extracted to roo-xls due to its dependency on the GPL'd spreadsheet gem. Install roo-xls to use Roo::Excel."
5+
ROO_EXCELML_NOTICE = "Excel SpreadsheetML support has been extracted to roo-xls. Install roo-xls to use Roo::Excel2003XML."
6+
ROO_GOOGLE_NOTICE = "Google support has been extracted to roo-google. Install roo-google to use Roo::Google."
57
end

lib/roo/excelx/cell/boolean.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
module Roo
23
class Excelx
34
class Cell
@@ -11,7 +12,7 @@ def initialize(value, formula, style, link, coordinate)
1112
end
1213

1314
def formatted_value
14-
value ? 'TRUE'.freeze : 'FALSE'.freeze
15+
value ? 'TRUE' : 'FALSE'
1516
end
1617

1718
private

lib/roo/excelx/cell/datetime.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'date'
24

35
module Roo
@@ -80,7 +82,7 @@ def parse_date_or_time_format(part)
8082

8183
TIME_FORMATS = {
8284
'hh' => '%H', # Hour (24): 01
83-
'h' => '%-k'.freeze, # Hour (24): 1
85+
'h' => '%-k', # Hour (24): 1
8486
# 'hh'.freeze => '%I'.freeze, # Hour (12): 08
8587
# 'h'.freeze => '%-l'.freeze, # Hour (12): 8
8688
'mm' => '%M', # Minute: 01
@@ -96,9 +98,7 @@ def parse_date_or_time_format(part)
9698

9799
def create_datetime(base_timestamp, value)
98100
timestamp = (base_timestamp + (value.to_f.round(6) * SECONDS_IN_DAY)).round(0)
99-
t = ::Time.at(timestamp).utc
100-
101-
::DateTime.civil(t.year, t.month, t.day, t.hour, t.min, t.sec)
101+
::Time.at(timestamp).utc.to_datetime
102102
end
103103
end
104104
end

lib/roo/excelx/cell/number.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
module Roo
34
class Excelx
45
class Cell

lib/roo/excelx/extractor.rb

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1+
# frozen_string_literal: true
2+
13
module Roo
24
class Excelx
35
class Extractor
6+
7+
COMMON_STRINGS = {
8+
t: "t",
9+
r: "r",
10+
s: "s",
11+
ref: "ref",
12+
html_tag_open: "<html>",
13+
html_tag_closed: "</html>"
14+
}
15+
416
def initialize(path, options = {})
517
@path = path
618
@options = options

lib/roo/excelx/format.rb

+29-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
module Roo
34
class Excelx
45
module Format
@@ -9,34 +10,34 @@ module Format
910
}
1011

1112
STANDARD_FORMATS = {
12-
0 => 'General'.freeze,
13-
1 => '0'.freeze,
14-
2 => '0.00'.freeze,
15-
3 => '#,##0'.freeze,
16-
4 => '#,##0.00'.freeze,
17-
9 => '0%'.freeze,
18-
10 => '0.00%'.freeze,
19-
11 => '0.00E+00'.freeze,
20-
12 => '# ?/?'.freeze,
21-
13 => '# ??/??'.freeze,
22-
14 => 'mm-dd-yy'.freeze,
23-
15 => 'd-mmm-yy'.freeze,
24-
16 => 'd-mmm'.freeze,
25-
17 => 'mmm-yy'.freeze,
26-
18 => 'h:mm AM/PM'.freeze,
27-
19 => 'h:mm:ss AM/PM'.freeze,
28-
20 => 'h:mm'.freeze,
29-
21 => 'h:mm:ss'.freeze,
30-
22 => 'm/d/yy h:mm'.freeze,
31-
37 => '#,##0 ;(#,##0)'.freeze,
32-
38 => '#,##0 ;[Red](#,##0)'.freeze,
33-
39 => '#,##0.00;(#,##0.00)'.freeze,
34-
40 => '#,##0.00;[Red](#,##0.00)'.freeze,
35-
45 => 'mm:ss'.freeze,
36-
46 => '[h]:mm:ss'.freeze,
37-
47 => 'mmss.0'.freeze,
38-
48 => '##0.0E+0'.freeze,
39-
49 => '@'.freeze
13+
0 => 'General',
14+
1 => '0',
15+
2 => '0.00',
16+
3 => '#,##0',
17+
4 => '#,##0.00',
18+
9 => '0%',
19+
10 => '0.00%',
20+
11 => '0.00E+00',
21+
12 => '# ?/?',
22+
13 => '# ??/??',
23+
14 => 'mm-dd-yy',
24+
15 => 'd-mmm-yy',
25+
16 => 'd-mmm',
26+
17 => 'mmm-yy',
27+
18 => 'h:mm AM/PM',
28+
19 => 'h:mm:ss AM/PM',
29+
20 => 'h:mm',
30+
21 => 'h:mm:ss',
31+
22 => 'm/d/yy h:mm',
32+
37 => '#,##0 ;(#,##0)',
33+
38 => '#,##0 ;[Red](#,##0)',
34+
39 => '#,##0.00;(#,##0.00)',
35+
40 => '#,##0.00;[Red](#,##0.00)',
36+
45 => 'mm:ss',
37+
46 => '[h]:mm:ss',
38+
47 => 'mmss.0',
39+
48 => '##0.0E+0',
40+
49 => '@'
4041
}
4142

4243
def to_type(format)

lib/roo/excelx/shared_strings.rb

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1+
# frozen_string_literal: true
2+
13
require 'roo/excelx/extractor'
24

35
module Roo
46
class Excelx
57
class SharedStrings < Excelx::Extractor
6-
7-
COMMON_STRINGS = {
8-
t: "t",
9-
r: "r",
10-
html_tag_open: "<html>",
11-
html_tag_closed: "</html>"
12-
}
13-
148
def [](index)
159
to_a[index]
1610
end
@@ -46,14 +40,14 @@ def extract_shared_strings
4640
document = fix_invalid_shared_strings(doc)
4741
# read the shared strings xml document
4842
document.xpath('/sst/si').map do |si|
49-
shared_string = ''
43+
shared_string = String.new
5044
si.children.each do |elem|
5145
case elem.name
52-
when 'r'.freeze
46+
when 'r'
5347
elem.children.each do |r_elem|
5448
shared_string << r_elem.content if r_elem.name == 't'
5549
end
56-
when 't'.freeze
50+
when 't'
5751
shared_string = elem.content
5852
end
5953
end
@@ -66,16 +60,16 @@ def extract_html
6660
fix_invalid_shared_strings(doc)
6761
# read the shared strings xml document
6862
doc.xpath('/sst/si').map do |si|
69-
html_string = '<html>'
63+
html_string = '<html>'.dup
7064
si.children.each do |elem|
7165
case elem.name
72-
when 'r'.freeze
66+
when 'r'
7367
html_string << extract_html_r(elem)
74-
when 't'.freeze
68+
when 't'
7569
html_string << elem.content
7670
end # case elem.name
7771
end # si.children.each do |elem|
78-
html_string << '</html>'.freeze
72+
html_string << '</html>'
7973
end # doc.xpath('/sst/si').map do |si|
8074
end # def extract_html
8175

@@ -96,7 +90,7 @@ def extract_html
9690
#
9791
# Expected Output ::: "<html><sub|sup><b><i><u>TEXT</u></i></b></sub|/sup></html>"
9892
def extract_html_r(r_elem)
99-
str = ''
93+
str = String.new
10094
xml_elems = {
10195
sub: false,
10296
sup: false,
@@ -141,7 +135,7 @@ def extract_html_r(r_elem)
141135

142136
# This will return an html string
143137
def create_html(text, formatting)
144-
tmp_str = ''
138+
tmp_str = String.new
145139
formatting.each do |elem, val|
146140
tmp_str << "<#{elem}>" if val
147141
end

lib/roo/excelx/sheet_doc.rb

+19-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Roo
55
class Excelx
66
class SheetDoc < Excelx::Extractor
77
extend Forwardable
8-
delegate [:workbook, :shared_strings] => :@shared
8+
delegate [:workbook] => :@shared
99

1010
def initialize(path, relationships, shared, options = {})
1111
super(path)
@@ -41,7 +41,7 @@ def each_cell(row_xml)
4141
row_xml.children.each do |cell_element|
4242
# If you're sure you're not going to need this hyperlinks you can discard it
4343
hyperlinks = unless @options[:no_hyperlinks]
44-
key = ::Roo::Utils.ref_to_key(cell_element['r'.freeze])
44+
key = ::Roo::Utils.ref_to_key(cell_element[COMMON_STRINGS[:r]])
4545
hyperlinks(@relationships)[key]
4646
end
4747

@@ -53,13 +53,13 @@ def each_cell(row_xml)
5353

5454
def cell_value_type(type, format)
5555
case type
56-
when 's'.freeze
56+
when 's'
5757
:shared
58-
when 'b'.freeze
58+
when 'b'
5959
:boolean
60-
when 'str'.freeze
60+
when 'str'
6161
:string
62-
when 'inlineStr'.freeze
62+
when 'inlineStr'
6363
:inlinestr
6464
else
6565
Excelx::Format.to_type(format)
@@ -82,32 +82,32 @@ def cell_value_type(type, format)
8282
#
8383
# Returns a type of <Excelx::Cell>.
8484
def cell_from_xml(cell_xml, hyperlink)
85-
coordinate = ::Roo::Utils.extract_coordinate(cell_xml['r'.freeze])
85+
coordinate = ::Roo::Utils.extract_coordinate(cell_xml[COMMON_STRINGS[:r]])
8686
cell_xml_children = cell_xml.children
8787
return Excelx::Cell::Empty.new(coordinate) if cell_xml_children.empty?
8888

8989
# NOTE: This is error prone, to_i will silently turn a nil into a 0.
9090
# This works by coincidence because Format[0] is General.
91-
style = cell_xml['s'.freeze].to_i
91+
style = cell_xml[COMMON_STRINGS[:s]].to_i
9292
formula = nil
9393

9494
cell_xml_children.each do |cell|
9595
case cell.name
96-
when 'is'.freeze
97-
content = ""
96+
when 'is'
97+
content = String.new
9898
cell.children.each do |inline_str|
99-
if inline_str.name == 't'.freeze
99+
if inline_str.name == 't'
100100
content << inline_str.content
101101
end
102102
end
103103
unless content.empty?
104104
return Excelx::Cell.cell_class(:string).new(content, formula, style, hyperlink, coordinate)
105105
end
106-
when 'f'.freeze
106+
when 'f'
107107
formula = cell.content
108-
when 'v'.freeze
108+
when 'v'
109109
format = style_format(style)
110-
value_type = cell_value_type(cell_xml['t'.freeze], format)
110+
value_type = cell_value_type(cell_xml[COMMON_STRINGS[:t]], format)
111111

112112
return create_cell_from_value(value_type, cell, formula, format, style, hyperlink, coordinate)
113113
end
@@ -169,8 +169,8 @@ def extract_hyperlinks(relationships)
169169
return {} unless (hyperlinks = doc.xpath('/worksheet/hyperlinks/hyperlink'))
170170

171171
Hash[hyperlinks.map do |hyperlink|
172-
if hyperlink.attribute('id'.freeze) && (relationship = relationships[hyperlink.attribute('id'.freeze).text])
173-
[::Roo::Utils.ref_to_key(hyperlink.attributes['ref'.freeze].to_s), relationship.attribute('Target'.freeze).text]
172+
if hyperlink.attribute('id') && (relationship = relationships[hyperlink.attribute('id').text])
173+
[::Roo::Utils.ref_to_key(hyperlink.attributes[COMMON_STRINGS[:ref]].to_s), relationship.attribute('Target').text]
174174
end
175175
end.compact]
176176
end
@@ -179,7 +179,7 @@ def expand_merged_ranges(cells)
179179
# Extract merged ranges from xml
180180
merges = {}
181181
doc.xpath('/worksheet/mergeCells/mergeCell').each do |mergecell_xml|
182-
tl, br = mergecell_xml['ref'.freeze].split(/:/).map { |ref| ::Roo::Utils.ref_to_key(ref) }
182+
tl, br = mergecell_xml[COMMON_STRINGS[:ref]].split(/:/).map { |ref| ::Roo::Utils.ref_to_key(ref) }
183183
for row in tl[0]..br[0] do
184184
for col in tl[1]..br[1] do
185185
next if row == tl[0] && col == tl[1]
@@ -196,7 +196,7 @@ def expand_merged_ranges(cells)
196196
def extract_cells(relationships)
197197
extracted_cells = {}
198198
doc.xpath('/worksheet/sheetData/row/c').each do |cell_xml|
199-
key = ::Roo::Utils.ref_to_key(cell_xml['r'.freeze])
199+
key = ::Roo::Utils.ref_to_key(cell_xml[COMMON_STRINGS[:r]])
200200
extracted_cells[key] = cell_from_xml(cell_xml, hyperlinks(relationships)[key])
201201
end
202202

@@ -207,7 +207,7 @@ def extract_cells(relationships)
207207

208208
def extract_dimensions
209209
Roo::Utils.each_element(@path, 'dimension') do |dimension|
210-
return dimension.attributes['ref'.freeze].value
210+
return dimension.attributes[COMMON_STRINGS[:ref]].value
211211
end
212212
end
213213

lib/roo/open_office.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'date'
24
require 'nokogiri'
35
require 'cgi'
@@ -11,9 +13,9 @@ module Roo
1113
class OpenOffice < Roo::Base
1214
extend Roo::Tempdir
1315

14-
ERROR_MISSING_CONTENT_XML = 'file missing required content.xml'.freeze
15-
XPATH_FIND_TABLE_STYLES = "//*[local-name()='automatic-styles']".freeze
16-
XPATH_LOCAL_NAME_TABLE = "//*[local-name()='table']".freeze
16+
ERROR_MISSING_CONTENT_XML = 'file missing required content.xml'
17+
XPATH_FIND_TABLE_STYLES = "//*[local-name()='automatic-styles']"
18+
XPATH_LOCAL_NAME_TABLE = "//*[local-name()='table']"
1719

1820
# initialization and opening of a spreadsheet file
1921
# values for packed: :zip

lib/roo/utils.rb

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def each_element(path, elements)
9191
end
9292

9393
private
94+
9495
def char_index(byte)
9596
if byte >= 65 && byte <= 90
9697
byte - 64

0 commit comments

Comments
 (0)