Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Rename methods and clean up use of 'collect' in favor of 'map.'
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarber623 committed Apr 17, 2017
1 parent ddb4a1f commit 82fc40b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 14 deletions.
8 changes: 3 additions & 5 deletions lib/svgeez/svg_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ def initialize(source, destination)
end

def build
%(<svg id="#{@destination.file_id}" version="1.1" xmlns="http://www.w3.org/2000/svg">#{source_files.join}</svg>)
%(<svg id="#{@destination.file_id}" version="1.1" xmlns="http://www.w3.org/2000/svg">#{element_contents}</svg>)
end

private

def source_files
@source.file_paths.collect do |file_path|
SymbolElement.new(file_path, @destination.file_id).build
end
def element_contents
@source.file_paths.map { |file_path| SymbolElement.new(file_path, @destination.file_id).build }.join
end
end
end
6 changes: 3 additions & 3 deletions lib/svgeez/symbol_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ def initialize(file_path, file_id)

def build
IO.read(@file_path).match(%r{^<svg\s*?(?<attributes>.*?)>(?<content>.*?)</svg>}m) do |matches|
%(<symbol #{build_attributes(matches[:attributes]).sort.join(' ')}>#{process_content(matches[:content])}</symbol>)
%(<symbol #{element_attributes(matches[:attributes]).sort.join(' ')}>#{element_contents(matches[:content])}</symbol>)
end
end

private

def build_attributes(attributes)
def element_attributes(attributes)
attrs = attributes.scan(/(?:viewBox|xmlns:.+?)=".*?"/m)

attrs << %(id="#{@file_id}-#{File.basename(@file_path, '.svg').gsub(/['"\s]/, '-')}")
end

def process_content(content)
def element_contents(content)
content.scan(/\sid="(.+?)"/).flatten.each do |value|
content.gsub!(/\s(id|xlink:href)="(#?#{value})"/m, %( \\1="\\2-#{SecureRandom.uuid}"))
end
Expand Down
4 changes: 1 addition & 3 deletions spec/lib/svgeez/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@

context 'when @source contains SVG files' do
let :file_paths do
%w(facebook github heart skull twitter).collect do |i|
"./spec/fixtures/icons/#{i}.svg"
end
%w(facebook github heart skull twitter).map { |i| "./spec/fixtures/icons/#{i}.svg" }
end

let(:file) { double(File) }
Expand Down
4 changes: 1 addition & 3 deletions spec/lib/svgeez/source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
let(:source) { Svgeez::Source.new }

let :file_paths do
%w(facebook github heart skull twitter).collect do |i|
File.expand_path("./_svgeez/#{i}.svg")
end
%w(facebook github heart skull twitter).map { |i| File.expand_path("./_svgeez/#{i}.svg") }
end

before do
Expand Down

0 comments on commit 82fc40b

Please sign in to comment.