Skip to content

Commit

Permalink
Cleanup Excelx classes
Browse files Browse the repository at this point in the history
+ require Excelx's submodules instead of autoloading.
+  Use guard class in Excelx::Comments#extract_comments and
Excelx::Relationships.extract_relationships
+ Updated conditionals in Excelx::Cell.
  • Loading branch information
stevendaniels committed May 28, 2015
1 parent e9a8894 commit 023bc4d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 58 deletions.
77 changes: 38 additions & 39 deletions lib/roo/excelx.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
require 'nokogiri'
require 'zip/filesystem'
require 'roo/link'
require 'roo/utils'
require 'zip/filesystem'

module Roo
class Excelx < Roo::Base
autoload :Workbook, 'roo/excelx/workbook'
autoload :SharedStrings, 'roo/excelx/shared_strings'
autoload :Styles, 'roo/excelx/styles'
autoload :Cell, 'roo/excelx/cell'
autoload :Sheet, 'roo/excelx/sheet'

autoload :Relationships, 'roo/excelx/relationships'
autoload :Comments, 'roo/excelx/comments'
autoload :SheetDoc, 'roo/excelx/sheet_doc'

require 'roo/excelx/workbook'
require 'roo/excelx/shared_strings'
require 'roo/excelx/styles'
require 'roo/excelx/cell'
require 'roo/excelx/sheet'
require 'roo/excelx/relationships'
require 'roo/excelx/comments'
require 'roo/excelx/sheet_doc'

module Format
EXCEPTIONAL_FORMATS = {
'h:mm am/pm' => :date,
'h:mm:ss am/pm' => :date
}

STANDARD_FORMATS = {
0 => 'General',
1 => '0',
2 => '0.00',
3 => '#,##0',
4 => '#,##0.00',
9 => '0%',
10 => '0.00%',
11 => '0.00E+00',
12 => '# ?/?',
13 => '# ??/??',
14 => 'mm-dd-yy',
15 => 'd-mmm-yy',
16 => 'd-mmm',
17 => 'mmm-yy',
18 => 'h:mm AM/PM',
19 => 'h:mm:ss AM/PM',
20 => 'h:mm',
21 => 'h:mm:ss',
22 => 'm/d/yy h:mm',
37 => '#,##0 ;(#,##0)',
38 => '#,##0 ;[Red](#,##0)',
39 => '#,##0.00;(#,##0.00)',
40 => '#,##0.00;[Red](#,##0.00)',
45 => 'mm:ss',
46 => '[h]:mm:ss',
47 => 'mmss.0',
48 => '##0.0E+0',
49 => '@'
0 => 'General'.freeze,
1 => '0'.freeze,
2 => '0.00'.freeze,
3 => '#,##0'.freeze,
4 => '#,##0.00'.freeze,
9 => '0%'.freeze,
10 => '0.00%'.freeze,
11 => '0.00E+00'.freeze,
12 => '# ?/?'.freeze,
13 => '# ??/??'.freeze,
14 => 'mm-dd-yy'.freeze,
15 => 'd-mmm-yy'.freeze,
16 => 'd-mmm'.freeze,
17 => 'mmm-yy'.freeze,
18 => 'h:mm AM/PM'.freeze,
19 => 'h:mm:ss AM/PM'.freeze,
20 => 'h:mm'.freeze,
21 => 'h:mm:ss'.freeze,
22 => 'm/d/yy h:mm'.freeze,
37 => '#,##0 ;(#,##0)'.freeze,
38 => '#,##0 ;[Red](#,##0)'.freeze,
39 => '#,##0.00;(#,##0.00)'.freeze,
40 => '#,##0.00;[Red](#,##0.00)'.freeze,
45 => 'mm:ss'.freeze,
46 => '[h]:mm:ss'.freeze,
47 => 'mmss.0'.freeze,
48 => '##0.0E+0'.freeze,
49 => '@'.freeze
}

def to_type(format)
Expand Down
7 changes: 3 additions & 4 deletions lib/roo/excelx/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ def initialize(value, type, formula, excelx_type, excelx_value, style, hyperlink
end

def type
if @formula
case
when @formula
:formula
elsif @value.is_a?(Roo::Link)
when @value.is_a?(Roo::Link)
:link
else
@type
Expand All @@ -48,8 +49,6 @@ def type_cast_value(value)
create_datetime(@base_date + value.to_f.round(6))
when :time
value.to_f * 86_400
when :string
value
else
value
end
Expand Down
14 changes: 6 additions & 8 deletions lib/roo/excelx/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ def comments
private

def extract_comments
if doc_exists?
Hash[doc.xpath('//comments/commentList/comment').map do |comment|
value = (comment.at_xpath('./text/r/t') || comment.at_xpath('./text/t')).text
[::Roo::Utils.ref_to_key(comment.attributes['ref'].to_s), value]
end]
else
{}
end
return {} unless doc_exists?

Hash[doc.xpath('//comments/commentList/comment').map do |comment|
value = (comment.at_xpath('./text/r/t') || comment.at_xpath('./text/t')).text
[::Roo::Utils.ref_to_key(comment.attributes['ref'].to_s), value]
end]
end
end
end
Expand Down
12 changes: 5 additions & 7 deletions lib/roo/excelx/relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ def to_a
private

def extract_relationships
if doc_exists?
Hash[doc.xpath('/Relationships/Relationship').map do |rel|
[rel.attribute('Id').text, rel]
end]
else
[]
end
return [] unless doc_exists?

Hash[doc.xpath('/Relationships/Relationship').map do |rel|
[rel.attribute('Id').text, rel]
end]
end
end
end
Expand Down

0 comments on commit 023bc4d

Please sign in to comment.