From 82fc40bafcb2fc375615d6c984e2291f09c3c04c Mon Sep 17 00:00:00 2001 From: Jason Garber Date: Mon, 17 Apr 2017 13:05:40 -0400 Subject: [PATCH] Rename methods and clean up use of 'collect' in favor of 'map.' --- lib/svgeez/svg_element.rb | 8 +++----- lib/svgeez/symbol_element.rb | 6 +++--- spec/lib/svgeez/builder_spec.rb | 4 +--- spec/lib/svgeez/source_spec.rb | 4 +--- 4 files changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/svgeez/svg_element.rb b/lib/svgeez/svg_element.rb index d1ae438..d824853 100644 --- a/lib/svgeez/svg_element.rb +++ b/lib/svgeez/svg_element.rb @@ -6,15 +6,13 @@ def initialize(source, destination) end def build - %(#{source_files.join}) + %(#{element_contents}) 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 diff --git a/lib/svgeez/symbol_element.rb b/lib/svgeez/symbol_element.rb index a01d1f6..7a13973 100644 --- a/lib/svgeez/symbol_element.rb +++ b/lib/svgeez/symbol_element.rb @@ -7,19 +7,19 @@ def initialize(file_path, file_id) def build IO.read(@file_path).match(%r{^.*?)>(?.*?)}m) do |matches| - %(#{process_content(matches[:content])}) + %(#{element_contents(matches[:content])}) 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 diff --git a/spec/lib/svgeez/builder_spec.rb b/spec/lib/svgeez/builder_spec.rb index 3f05049..0253377 100644 --- a/spec/lib/svgeez/builder_spec.rb +++ b/spec/lib/svgeez/builder_spec.rb @@ -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) } diff --git a/spec/lib/svgeez/source_spec.rb b/spec/lib/svgeez/source_spec.rb index f81543f..a03ecbd 100644 --- a/spec/lib/svgeez/source_spec.rb +++ b/spec/lib/svgeez/source_spec.rb @@ -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