diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index bc3211d..68db59d 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -17,46 +17,6 @@ Layout/DotPosition: - 'spec/transformation_pipeline/transformations/aria_attributes_spec.rb' - 'spec/transformation_pipeline/transformations/data_attributes_spec.rb' -# This cop supports safe autocorrection (--autocorrect). -Layout/EmptyLineAfterGuardClause: - Exclude: - - 'lib/inline_svg.rb' - -# This cop supports safe autocorrection (--autocorrect). -Layout/EmptyLineAfterMagicComment: - Exclude: - - 'spec/cached_asset_file_spec.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: around, only_before -Layout/EmptyLinesAroundAccessModifier: - Exclude: - - 'lib/inline_svg/cached_asset_file.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: empty_lines, no_empty_lines -Layout/EmptyLinesAroundBlockBody: - Exclude: - - 'spec/helpers/inline_svg_spec.rb' - - 'spec/inline_svg_spec.rb' - - 'spec/io_resource_spec.rb' - - 'spec/transformation_pipeline/transformations_spec.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines, beginning_only, ending_only -Layout/EmptyLinesAroundClassBody: - Exclude: - - 'lib/inline_svg.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment. -Layout/ExtraSpacing: - Exclude: - - 'lib/inline_svg/webpack_asset_finder.rb' - # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: IndentationWidth. # SupportedStyles: special_inside_parentheses, consistent, align_brackets @@ -72,12 +32,6 @@ Layout/HashAlignment: Exclude: - 'spec/transformation_pipeline/transformations_spec.rb' -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowDoxygenCommentStyle, AllowGemfileRubyComment. -Layout/LeadingCommentSpace: - Exclude: - - 'Rakefile' - # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle, IndentationWidth. # SupportedStyles: aligned, indented, indented_relative_to_receiver @@ -87,62 +41,6 @@ Layout/MultilineMethodCallIndentation: - 'spec/transformation_pipeline/transformations/style_attribute_spec.rb' - 'spec/transformation_pipeline/transformations/view_box_spec.rb' -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: . -# SupportedStyles: space, no_space -Layout/SpaceAroundEqualsInParameterDefault: - EnforcedStyle: no_space - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowForAlignment, EnforcedStyleForExponentOperator, EnforcedStyleForRationalLiterals. -# SupportedStylesForExponentOperator: space, no_space -# SupportedStylesForRationalLiterals: space, no_space -Layout/SpaceAroundOperators: - Exclude: - - 'lib/inline_svg/webpack_asset_finder.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. -# SupportedStyles: space, no_space -# SupportedStylesForEmptyBraces: space, no_space -Layout/SpaceInsideBlockBraces: - Exclude: - - 'lib/inline_svg/transform_pipeline/transformations.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. -# SupportedStyles: space, no_space, compact -# SupportedStylesForEmptyBraces: space, no_space -Layout/SpaceInsideHashLiteralBraces: - Exclude: - - 'spec/helpers/inline_svg_spec.rb' - - 'spec/inline_svg_spec.rb' - - 'spec/transformation_pipeline/transformations/data_attributes_spec.rb' - - 'spec/transformation_pipeline/transformations_spec.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: space, compact, no_space -Layout/SpaceInsideParens: - Exclude: - - 'lib/inline_svg/transform_pipeline/transformations.rb' - - 'spec/helpers/inline_svg_spec.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBrackets. -# SupportedStyles: space, no_space -# SupportedStylesForEmptyBrackets: space, no_space -Layout/SpaceInsideReferenceBrackets: - Exclude: - - 'lib/inline_svg.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowInHeredoc. -Layout/TrailingWhitespace: - Exclude: - - 'spec/helpers/inline_svg_spec.rb' - - 'spec/io_resource_spec.rb' - # This cop supports safe autocorrection (--autocorrect). Lint/AmbiguousOperator: Exclude: @@ -271,24 +169,6 @@ RSpec/EmptyExampleGroup: Exclude: - 'spec/io_resource_spec.rb' -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowConsecutiveOneLiners. -RSpec/EmptyLineAfterExample: - Exclude: - - 'spec/helpers/inline_svg_spec.rb' - -# This cop supports safe autocorrection (--autocorrect). -RSpec/EmptyLineAfterExampleGroup: - Exclude: - - 'spec/helpers/inline_svg_spec.rb' - - 'spec/io_resource_spec.rb' - -# This cop supports safe autocorrection (--autocorrect). -RSpec/EmptyLineAfterFinalLet: - Exclude: - - 'spec/helpers/inline_svg_spec.rb' - - 'spec/io_resource_spec.rb' - # Configuration parameters: CountAsOne. RSpec/ExampleLength: Max: 32 diff --git a/Rakefile b/Rakefile index fad37b5..88c85cf 100644 --- a/Rakefile +++ b/Rakefile @@ -3,6 +3,6 @@ require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) do |t| t.pattern = Dir.glob("spec/**/*_spec.rb") - #t.rspec_opts = "--format documentation" + # t.rspec_opts = "--format documentation" end task :default => :spec diff --git a/lib/inline_svg.rb b/lib/inline_svg.rb index 45aa49e..8426fcc 100644 --- a/lib/inline_svg.rb +++ b/lib/inline_svg.rb @@ -65,7 +65,8 @@ def add_custom_transformation(options) if incompatible_transformation?(options.fetch(:transform)) raise InlineSvg::Configuration::Invalid.new("#{options.fetch(:transform)} should implement the .create_with_value and #transform methods") end - @custom_transformations.merge!(Hash[ *[options.fetch(:attribute, :no_attribute), options] ]) + + @custom_transformations.merge!(Hash[*[options.fetch(:attribute, :no_attribute), options]]) end def raise_on_file_not_found=(value) @@ -81,7 +82,6 @@ def raise_on_file_not_found? def incompatible_transformation?(klass) !klass.is_a?(Class) || !klass.respond_to?(:create_with_value) || !klass.instance_methods.include?(:transform) end - end @configuration = InlineSvg::Configuration.new diff --git a/lib/inline_svg/action_view/helpers.rb b/lib/inline_svg/action_view/helpers.rb index b3adbc0..957b6b0 100644 --- a/lib/inline_svg/action_view/helpers.rb +++ b/lib/inline_svg/action_view/helpers.rb @@ -4,25 +4,25 @@ module InlineSvg module ActionView module Helpers - def inline_svg_tag(filename, transform_params={}) + def inline_svg_tag(filename, transform_params = {}) with_asset_finder(InlineSvg.configuration.asset_finder) do render_inline_svg(filename, transform_params) end end - def inline_svg_pack_tag(filename, transform_params={}) + def inline_svg_pack_tag(filename, transform_params = {}) with_asset_finder(InlineSvg::WebpackAssetFinder) do render_inline_svg(filename, transform_params) end end - def inline_svg(filename, transform_params={}) + def inline_svg(filename, transform_params = {}) render_inline_svg(filename, transform_params) end private - def render_inline_svg(filename, transform_params={}) + def render_inline_svg(filename, transform_params = {}) begin svg_file = read_svg(filename) rescue InlineSvg::AssetFile::FileNotFound => error diff --git a/lib/inline_svg/cached_asset_file.rb b/lib/inline_svg/cached_asset_file.rb index 82d7c3a..b56b828 100644 --- a/lib/inline_svg/cached_asset_file.rb +++ b/lib/inline_svg/cached_asset_file.rb @@ -32,6 +32,7 @@ def named(asset_name) end private + # Internal: Finds the key for a given asset name (using a Regex). In the # event of an ambiguous asset_name matching multiple assets, this method # ranks the matches by their full file path, choosing the shortest (most diff --git a/lib/inline_svg/transform_pipeline/transformations.rb b/lib/inline_svg/transform_pipeline/transformations.rb index 2cec526..4f499b5 100644 --- a/lib/inline_svg/transform_pipeline/transformations.rb +++ b/lib/inline_svg/transform_pipeline/transformations.rb @@ -27,12 +27,12 @@ def self.magnify_priorities(transforms) transforms.inject({}) do |output, (name, definition)| priority = definition.fetch(:priority, built_in_transformations.size) - output[name] = definition.merge( { priority: magnify(priority) } ) + output[name] = definition.merge({ priority: magnify(priority) }) output end end - def self.magnify(priority=0) + def self.magnify(priority = 0) (priority + 1) * built_in_transformations.size end @@ -59,15 +59,15 @@ def self.params_with_defaults(params) end def self.without_empty_values(params) - params.reject {|key, value| value.nil?} + params.reject { |key, value| value.nil? } end def self.all_default_values custom_transformations .values - .select {|opt| opt[:default_value] != nil} - .map {|opt| [opt[:attribute], opt[:default_value]]} - .inject({}) {|options, attrs| options.merge!(attrs[0] => attrs[1])} + .select { |opt| opt[:default_value] != nil } + .map { |opt| [opt[:attribute], opt[:default_value]] } + .inject({}) { |options, attrs| options.merge!(attrs[0] => attrs[1]) } end def self.no_transform diff --git a/lib/inline_svg/webpack_asset_finder.rb b/lib/inline_svg/webpack_asset_finder.rb index b5e730e..81f4fd5 100644 --- a/lib/inline_svg/webpack_asset_finder.rb +++ b/lib/inline_svg/webpack_asset_finder.rb @@ -7,7 +7,7 @@ def self.find_asset(filename) def initialize(filename) @filename = filename manifest_lookup = asset_helper.manifest.lookup(@filename) - @asset_path = manifest_lookup.present? ? URI(manifest_lookup).path : "" + @asset_path = manifest_lookup.present? ? URI(manifest_lookup).path : "" end def pathname diff --git a/spec/cached_asset_file_spec.rb b/spec/cached_asset_file_spec.rb index aa74c84..4dfd74c 100644 --- a/spec/cached_asset_file_spec.rb +++ b/spec/cached_asset_file_spec.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require 'pathname' require_relative '../lib/inline_svg' diff --git a/spec/helpers/inline_svg_spec.rb b/spec/helpers/inline_svg_spec.rb index 190f7f3..1626bf7 100644 --- a/spec/helpers/inline_svg_spec.rb +++ b/spec/helpers/inline_svg_spec.rb @@ -10,11 +10,9 @@ def transform(doc) end describe InlineSvg::ActionView::Helpers do - - let(:helper) { ( Class.new { include InlineSvg::ActionView::Helpers } ).new } + let(:helper) { (Class.new { include InlineSvg::ActionView::Helpers }).new } shared_examples "inline_svg helper" do |helper_method:| - context "when passed the name of an SVG that does not exist" do after(:each) do InlineSvg.reset_configuration! @@ -94,7 +92,6 @@ def transform(doc) end context "when passed an existing SVG file" do - context "and no options" do it "returns a html safe version of the file's contents" do example_file = '' @@ -151,7 +148,7 @@ def transform(doc) context "with custom transformations" do before(:each) do InlineSvg.configure do |config| - config.add_custom_transformation({attribute: :custom, transform: WorkingCustomTransform}) + config.add_custom_transformation({ attribute: :custom, transform: WorkingCustomTransform }) end end @@ -170,7 +167,7 @@ def transform(doc) context "with custom transformations using a default value" do before(:each) do InlineSvg.configure do |config| - config.add_custom_transformation({attribute: :custom, transform: WorkingCustomTransform, default_value: 'default value'}) + config.add_custom_transformation({ attribute: :custom, transform: WorkingCustomTransform, default_value: 'default value' }) end end @@ -198,16 +195,18 @@ def transform(doc) end end end - end + context 'argument polimorphizm' do let(:argument) { double('argument') } + it 'accept IO' do expect(InlineSvg::IOResource).to receive(:===).with(argument).and_return(true) expect(InlineSvg::IOResource).to receive(:read).with(argument) expect(InlineSvg::AssetFile).to_not receive(:named) helper.send(helper_method, argument) end + it 'accept filename' do expect(InlineSvg::IOResource).to receive(:===).with(argument).and_return(false) expect(InlineSvg::IOResource).to_not receive(:read) @@ -215,10 +214,12 @@ def transform(doc) helper.send(helper_method, argument) end end + context 'when passed IO object argument' do let(:io_object) { double('io_object') } let(:file_path) { File.expand_path('../../files/example.svg', __FILE__) } let(:answer) { File.read(file_path) } + it 'return valid svg' do expect(InlineSvg::IOResource).to receive(:===).with(io_object).and_return(true) expect(InlineSvg::IOResource).to receive(:read).with(io_object).and_return("") @@ -232,15 +233,14 @@ def transform(doc) expect(output).to eq "" expect(output).to be_html_safe end - end - + context 'default output' do it "returns an SVG tag without any pre or post whitespace characters" do input_svg = '' - + allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg) - + expect(helper.send(helper_method, 'some-file')).to eq "" end end diff --git a/spec/inline_svg_spec.rb b/spec/inline_svg_spec.rb index 2d8a75d..1efb338 100644 --- a/spec/inline_svg_spec.rb +++ b/spec/inline_svg_spec.rb @@ -96,7 +96,7 @@ def self.named(filename); end config.add_custom_transformation(attribute: :my_transform, transform: MyCustomTransform) end - expect(InlineSvg.configuration.custom_transformations).to eq({my_transform: {attribute: :my_transform, transform: MyCustomTransform}}) + expect(InlineSvg.configuration.custom_transformations).to eq({ my_transform: { attribute: :my_transform, transform: MyCustomTransform } }) end it "rejects transformations that do not implement .create_with_value" do @@ -122,7 +122,6 @@ def self.named(filename); end end end.to raise_error(InlineSvg::Configuration::Invalid, /#{:not_a_class} should implement the .create_with_value and #transform methods/) end - end end end diff --git a/spec/io_resource_spec.rb b/spec/io_resource_spec.rb index 6b5cccf..8a98e52 100644 --- a/spec/io_resource_spec.rb +++ b/spec/io_resource_spec.rb @@ -23,6 +23,7 @@ expect(subject === File.new("#{Dir.tmpdir}/testfile", "w")).to be true end end + context 'return false' do it "for String object" do expect(subject === "string/filename").to be false @@ -31,11 +32,10 @@ end describe '#read' do - tests = proc do it "closed raise error" do rio.close - expect do + expect do subject.read(rio) end.to raise_error(IOError) end @@ -51,7 +51,7 @@ end it "write only raise error" do - expect do + expect do subject.read wio end.to raise_error(IOError) end @@ -61,6 +61,7 @@ let(:answer) { 'read' } let(:rio) { StringIO.new(answer, 'r') } let(:wio) { StringIO.new('write', 'w') } + instance_exec &tests end @@ -69,12 +70,11 @@ let(:answer) { File.read(file_path) } let(:rio) { File.new(file_path, 'r') } let(:wio) { File.new('/dev/null', 'w') } + instance_exec &tests it 'has non empty body' do expect(answer).to_not eq '' end end - - end end diff --git a/spec/transformation_pipeline/transformations/data_attributes_spec.rb b/spec/transformation_pipeline/transformations/data_attributes_spec.rb index ed1e6a5..89f5fd5 100644 --- a/spec/transformation_pipeline/transformations/data_attributes_spec.rb +++ b/spec/transformation_pipeline/transformations/data_attributes_spec.rb @@ -3,7 +3,7 @@ describe InlineSvg::TransformPipeline::Transformations::DataAttributes do it "adds a data attribute to a SVG document" do document = Nokogiri::XML::Document.parse('Some document') - transformation = InlineSvg::TransformPipeline::Transformations::DataAttributes.create_with_value({some: "value"}) + transformation = InlineSvg::TransformPipeline::Transformations::DataAttributes.create_with_value({ some: "value" }) expect(transformation.transform(document).to_html).to eq( "Some document\n" @@ -12,7 +12,7 @@ it "dasherizes the data attribute name" do document = Nokogiri::XML::Document.parse('Some document') - transformation = InlineSvg::TransformPipeline::Transformations::DataAttributes.create_with_value({some_name: "value"}) + transformation = InlineSvg::TransformPipeline::Transformations::DataAttributes.create_with_value({ some_name: "value" }) expect(transformation.transform(document).to_html).to eq( "Some document\n" @@ -21,7 +21,7 @@ it "dasherizes a data attribute name with multiple parts" do document = Nokogiri::XML::Document.parse('Some document') - transformation = InlineSvg::TransformPipeline::Transformations::DataAttributes.create_with_value({some_other_name: "value"}) + transformation = InlineSvg::TransformPipeline::Transformations::DataAttributes.create_with_value({ some_other_name: "value" }) expect(transformation.transform(document).to_html).to eq( "Some document\n" @@ -32,7 +32,7 @@ it "adds data attributes to the SVG for each supplied value" do document = Nokogiri::XML::Document.parse('Some document') transformation = InlineSvg::TransformPipeline::Transformations::DataAttributes. - create_with_value({some: "value", other: "thing"}) + create_with_value({ some: "value", other: "thing" }) expect(transformation.transform(document).to_html).to eq( "Some document\n" diff --git a/spec/transformation_pipeline/transformations_spec.rb b/spec/transformation_pipeline/transformations_spec.rb index 8c87d95..038bd46 100644 --- a/spec/transformation_pipeline/transformations_spec.rb +++ b/spec/transformation_pipeline/transformations_spec.rb @@ -93,8 +93,8 @@ class ASecondCustomTransform < ACustomTransform; end context "custom transformations" do before(:each) do InlineSvg.configure do |config| - config.add_custom_transformation({transform: ACustomTransform, attribute: :my_transform, priority: 2}) - config.add_custom_transformation({transform: ASecondCustomTransform, attribute: :my_other_transform, priority: 1}) + config.add_custom_transformation({ transform: ACustomTransform, attribute: :my_transform, priority: 2 }) + config.add_custom_transformation({ transform: ASecondCustomTransform, attribute: :my_other_transform, priority: 1 }) end end @@ -137,5 +137,4 @@ class ASecondCustomTransform < ACustomTransform; end ) end end - end