Skip to content

Commit

Permalink
Use %i[] for symbol arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
unixmonkey committed Oct 12, 2020
1 parent c2e050c commit 8cd58cb
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 99 deletions.
7 changes: 0 additions & 7 deletions .rubocop_todo.yml
Expand Up @@ -106,13 +106,6 @@ Style/RegexpLiteral:
- 'lib/wicked_pdf/wicked_pdf_helper/assets.rb'
- 'test/unit/wicked_pdf_test.rb'

# Offense count: 29
# Cop supports --auto-correct.
# Configuration parameters: MinSize.
# SupportedStyles: percent, brackets
Style/SymbolArray:
EnforcedStyle: brackets

# Offense count: 111
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -5,7 +5,7 @@ require 'rails/version'
require 'bundler/gem_tasks'

desc 'Default: run unit tests.'
task :default => [:setup_and_run_tests, :rubocop]
task :default => %i[setup_and_run_tests rubocop]

desc 'Test the wicked_pdf plugin.'
Rake::TestTask.new(:test) do |t|
Expand Down
140 changes: 70 additions & 70 deletions lib/wicked_pdf/option_parser.rb
Expand Up @@ -52,12 +52,12 @@ def parse_basic_auth(options)
def parse_header_footer(options)
r = []
unless options.blank?
[:header, :footer].collect do |hf|
%i[header footer].collect do |hf|
next if options[hf].blank?

opt_hf = options[hf]
r += make_options(opt_hf, [:center, :font_name, :left, :right], hf.to_s)
r += make_options(opt_hf, [:font_size, :spacing], hf.to_s, :numeric)
r += make_options(opt_hf, %i[center font_name left right], hf.to_s)
r += make_options(opt_hf, %i[font_size spacing], hf.to_s, :numeric)
r += make_options(opt_hf, [:line], hf.to_s, :boolean)
if options[hf] && options[hf][:content]
@hf_tempfiles = [] unless defined?(@hf_tempfiles)
Expand Down Expand Up @@ -96,31 +96,31 @@ def parse_toc(options)

r = [valid_option('toc')]
unless options.blank?
r += make_options(options, [:font_name, :header_text], 'toc')
r += make_options(options, %i[font_name header_text], 'toc')
r += make_options(options, [:xsl_style_sheet])
r += make_options(options, [:depth,
:header_fs,
:text_size_shrink,
:l1_font_size,
:l2_font_size,
:l3_font_size,
:l4_font_size,
:l5_font_size,
:l6_font_size,
:l7_font_size,
:level_indentation,
:l1_indentation,
:l2_indentation,
:l3_indentation,
:l4_indentation,
:l5_indentation,
:l6_indentation,
:l7_indentation], 'toc', :numeric)
r += make_options(options, [:no_dots,
:disable_links,
:disable_back_links], 'toc', :boolean)
r += make_options(options, [:disable_dotted_lines,
:disable_toc_links], nil, :boolean)
r += make_options(options, %i[depth
header_fs
text_size_shrink
l1_font_size
l2_font_size
l3_font_size
l4_font_size
l5_font_size
l6_font_size
l7_font_size
level_indentation
l1_indentation
l2_indentation
l3_indentation
l4_indentation
l5_indentation
l6_indentation
l7_indentation], 'toc', :numeric)
r += make_options(options, %i[no_dots
disable_links
disable_back_links], 'toc', :boolean)
r += make_options(options, %i[disable_dotted_lines
disable_toc_links], nil, :boolean)
end
r
end
Expand All @@ -135,25 +135,25 @@ def parse_outline(options)
end

def parse_margins(options)
make_options(options, [:top, :bottom, :left, :right], 'margin', :numeric)
make_options(options, %i[top bottom left right], 'margin', :numeric)
end

def parse_global(options)
r = []
unless options.blank?
r += make_options(options, [:orientation,
:dpi,
:page_size,
:page_width,
:title,
:log_level])
r += make_options(options, [:lowquality,
:grayscale,
:no_pdf_compression,
:quiet], '', :boolean)
r += make_options(options, [:image_dpi,
:image_quality,
:page_height], '', :numeric)
r += make_options(options, %i[orientation
dpi
page_size
page_width
title
log_level])
r += make_options(options, %i[lowquality
grayscale
no_pdf_compression
quiet], '', :boolean)
r += make_options(options, %i[image_dpi
image_quality
page_height], '', :numeric)
r += parse_margins(options.delete(:margin))
end
r
Expand All @@ -162,35 +162,35 @@ def parse_global(options)
def parse_others(options)
r = []
unless options.blank?
r += make_options(options, [:proxy,
:username,
:password,
:encoding,
:user_style_sheet,
:viewport_size,
:window_status])
r += make_options(options, [:cookie,
:post], '', :name_value)
r += make_options(options, [:redirect_delay,
:zoom,
:page_offset,
:javascript_delay], '', :numeric)
r += make_options(options, [:book,
:default_header,
:disable_javascript,
:enable_plugins,
:disable_internal_links,
:disable_external_links,
:keep_relative_links,
:print_media_type,
:disable_local_file_access,
:enable_local_file_access,
:disable_smart_shrinking,
:use_xserver,
:no_background,
:images,
:no_images,
:no_stop_slow_scripts], '', :boolean)
r += make_options(options, %i[proxy
username
password
encoding
user_style_sheet
viewport_size
window_status])
r += make_options(options, %i[cookie
post], '', :name_value)
r += make_options(options, %i[redirect_delay
zoom
page_offset
javascript_delay], '', :numeric)
r += make_options(options, %i[book
default_header
disable_javascript
enable_plugins
disable_internal_links
disable_external_links
keep_relative_links
print_media_type
disable_local_file_access
enable_local_file_access
disable_smart_shrinking
use_xserver
no_background
images
no_images
no_stop_slow_scripts], '', :boolean)
end
r
end
Expand Down
2 changes: 1 addition & 1 deletion lib/wicked_pdf/pdf_helper.rb
Expand Up @@ -121,7 +121,7 @@ def make_and_send_pdf(pdf_name, options = {})
# Given an options hash, prerenders content for the header and footer sections
# to temp files and return a new options hash including the URLs to these files.
def prerender_header_and_footer(options)
[:header, :footer].each do |hf|
%i[header footer].each do |hf|
next unless options[hf] && options[hf][:html] && options[hf][:html][:template]

@hf_tempfiles = [] unless defined?(@hf_tempfiles)
Expand Down
2 changes: 1 addition & 1 deletion test/functional/pdf_helper_test.rb
Expand Up @@ -76,7 +76,7 @@ def teardown

# test that calling render does not trigger infinite loop
ac = ActionController::Base.new
assert_equal [:base, :patched], ac.render(:cats)
assert_equal %i[base patched], ac.render(:cats)
rescue SystemStackError
assert_equal true, false # force spec failure
ensure
Expand Down
38 changes: 19 additions & 19 deletions test/unit/wicked_pdf_option_parser_test.rb
Expand Up @@ -2,13 +2,13 @@

class WickedPdfOptionParserTest < ActiveSupport::TestCase
test 'should parse header and footer options' do
[:header, :footer].each do |hf|
[:center, :font_name, :left, :right].each do |o|
%i[header footer].each do |hf|
%i[center font_name left right].each do |o|
assert_equal "--#{hf}-#{o.to_s.tr('_', '-')} header_footer",
parse_options(hf => { o => 'header_footer' }).strip
end

[:font_size, :spacing].each do |o|
%i[font_size spacing].each do |o|
assert_equal "--#{hf}-#{o.to_s.tr('_', '-')} 12",
parse_options(hf => { o => '12' }).strip
end
Expand All @@ -23,21 +23,21 @@ class WickedPdfOptionParserTest < ActiveSupport::TestCase
test 'should parse toc options' do
toc_option = option_parser.valid_option('toc')

[:font_name, :header_text].each do |o|
%i[font_name header_text].each do |o|
assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')} toc",
parse_options(:toc => { o => 'toc' }).strip
end

[
:depth, :header_fs, :l1_font_size, :l2_font_size, :l3_font_size, :l4_font_size,
:l5_font_size, :l6_font_size, :l7_font_size, :l1_indentation, :l2_indentation,
:l3_indentation, :l4_indentation, :l5_indentation, :l6_indentation, :l7_indentation
%i[
depth header_fs l1_font_size l2_font_size l3_font_size l4_font_size
l5_font_size l6_font_size l7_font_size l1_indentation l2_indentation
l3_indentation l4_indentation l5_indentation l6_indentation l7_indentation
].each do |o|
assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')} 5",
parse_options(:toc => { o => 5 }).strip
end

[:no_dots, :disable_links, :disable_back_links].each do |o|
%i[no_dots disable_links disable_back_links].each do |o|
assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')}",
parse_options(:toc => { o => true }).strip
end
Expand All @@ -54,7 +54,7 @@ class WickedPdfOptionParserTest < ActiveSupport::TestCase
end

test 'should parse margins options' do
[:top, :bottom, :left, :right].each do |o|
%i[top bottom left right].each do |o|
assert_equal "--margin-#{o} 12", parse_options(:margin => { o => '12' }).strip
end
end
Expand All @@ -69,28 +69,28 @@ class WickedPdfOptionParserTest < ActiveSupport::TestCase
end

test 'should parse other options' do
[
:orientation, :page_size, :proxy, :username, :password, :dpi,
:encoding, :user_style_sheet
%i[
orientation page_size proxy username password dpi
encoding user_style_sheet
].each do |o|
assert_equal "--#{o.to_s.tr('_', '-')} opts", parse_options(o => 'opts').strip
end

[:cookie, :post].each do |o|
%i[cookie post].each do |o|
assert_equal "--#{o.to_s.tr('_', '-')} name value", parse_options(o => 'name value').strip

nv_formatter = proc { |number| "--#{o.to_s.tr('_', '-')} par#{number} val#{number}" }
assert_equal "#{nv_formatter.call(1)} #{nv_formatter.call(2)}", parse_options(o => ['par1 val1', 'par2 val2']).strip
end

[:redirect_delay, :zoom, :page_offset].each do |o|
%i[redirect_delay zoom page_offset].each do |o|
assert_equal "--#{o.to_s.tr('_', '-')} 5", parse_options(o => 5).strip
end

[
:book, :default_header, :disable_javascript, :grayscale, :lowquality,
:enable_plugins, :disable_internal_links, :disable_external_links,
:print_media_type, :disable_smart_shrinking, :use_xserver, :no_background
%i[
book default_header disable_javascript grayscale lowquality
enable_plugins disable_internal_links disable_external_links
print_media_type disable_smart_shrinking use_xserver no_background
].each do |o|
assert_equal "--#{o.to_s.tr('_', '-')}", parse_options(o => true).strip
end
Expand Down

0 comments on commit 8cd58cb

Please sign in to comment.