Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip extract_hyperlinks if not required #488

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/roo/excelx/relationships.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'roo/excelx/extractor'

module Roo
Expand All @@ -11,6 +13,12 @@ def to_a
@relationships ||= extract_relationships
end

def include_type?(type)
to_a.any? do |_, rel|
rel["Type"]&.include? type
end
end

private

def extract_relationships
Expand Down
2 changes: 1 addition & 1 deletion lib/roo/excelx/sheet_doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def cells(relationships)

def hyperlinks(relationships)
# If you're sure you're not going to need this hyperlinks you can discard it
@hyperlinks ||= if @options[:no_hyperlinks]
@hyperlinks ||= if @options[:no_hyperlinks] || !relationships.include_type?("hyperlink")
{}
else
extract_hyperlinks(relationships)
Expand Down
43 changes: 43 additions & 0 deletions spec/lib/roo/excelx/relationships_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require "spec_helper"

describe Roo::Excelx::Relationships do
subject(:relationships) { Roo::Excelx::Relationships.new Roo::Excelx.new(path).rels_files[0] }

describe "#include_type?" do
[
["with hyperlink type", "test/files/link.xlsx", true, false],
["with nil path", "test/files/Bibelbund.xlsx", false, false],
["with comments type", "test/files/comments-google.xlsx", false, true],
].each do |context_desc, file_path, hyperlink_value, comments_value|
context context_desc do
let(:path) { file_path }

it "should return #{hyperlink_value} for hyperlink" do
expect(subject.include_type?("hyperlink")).to be hyperlink_value
end

it "should return #{hyperlink_value} for link" do
expect(subject.include_type?("link")).to be hyperlink_value
end

it "should return false for hypelink" do
expect(subject.include_type?("hypelink")).to be false
end

it "should return false for coment" do
expect(subject.include_type?("coment")).to be false
end

it "should return #{comments_value} for comments" do
expect(subject.include_type?("comments")).to be comments_value
end

it "should return #{comments_value} for comment" do
expect(subject.include_type?("comment")).to be comments_value
end
end
end
end
end