From b25b16484e3c496f5d9b9e6019fd67bae8f97f1d Mon Sep 17 00:00:00 2001 From: Gerard Caulfield Date: Tue, 5 Jul 2016 16:46:14 +1000 Subject: [PATCH] Fix some Rubocop/Codeclimate issues --- README.md | 58 ++++++++++----------- lib/awesome_print/custom_defaults.rb | 19 +++++-- lib/awesome_print/inspector.rb | 76 ++++++++++++++++------------ 3 files changed, 86 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index bd2b85e..21aae68 100644 --- a/README.md +++ b/README.md @@ -33,35 +33,35 @@ ap object, options = {} Default options: ```ruby -:indent => 4, # Indent using 4 spaces. -:index => true, # Display array indices. -:html => false, # Use ANSI color codes rather than HTML. -:multiline => true, # Display in multiple lines. -:plain => false, # Use colors. -:raw => false, # Do not recursively format object instance variables. -:sort_keys => false, # Do not sort hash keys. -:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. -:new_hash_syntax => false, # Use the JSON like syntax { foo: 'bar' }, when the key is a symbol -:color => { - :args => :pale, - :array => :white, - :bigdecimal => :blue, - :class => :yellow, - :date => :greenish, - :falseclass => :red, - :fixnum => :blue, - :float => :blue, - :hash => :pale, - :keyword => :cyan, - :method => :purpleish, - :nilclass => :red, - :rational => :blue, - :string => :yellowish, - :struct => :pale, - :symbol => :cyanish, - :time => :greenish, - :trueclass => :green, - :variable => :cyanish +indent: 4, # Number of spaces for indenting. +index: true, # Display array indices. +html: false, # Use ANSI color codes rather than HTML. +multiline: true, # Display in multiple lines. +plain: false, # Use colors. +raw: false, # Do not recursively format instance variables. +sort_keys: false, # Do not sort hash keys. +limit: false, # Limit arrays & hashes. Accepts bool or int. +new_hash_syntax: false, # Use Ruby 1.9 hash syntax in output. +color: { + args: :pale, + array: :white, + bigdecimal: :blue, + class: :yellow, + date: :greenish, + falseclass: :red, + fixnum: :blue, + float: :blue, + hash: :pale, + keyword: :cyan, + method: :purpleish, + nilclass: :red, + rational: :blue, + string: :yellowish, + struct: :pale, + symbol: :cyanish, + time: :greenish, + trueclass: :green, + variable: :cyanish } ``` diff --git a/lib/awesome_print/custom_defaults.rb b/lib/awesome_print/custom_defaults.rb index 9b5bea7..aec402a 100644 --- a/lib/awesome_print/custom_defaults.rb +++ b/lib/awesome_print/custom_defaults.rb @@ -4,17 +4,17 @@ module AwesomePrint # Class accessor to force colorized output (ex. forked subprocess where TERM # might be dumb). - #------------------------------------------------------------------------------ + #--------------------------------------------------------------------------- def force_colors!(value = true) @force_colors = value end def console? - !!(defined?(IRB) || defined?(Pry)) + boolean(defined?(IRB) || defined?(Pry)) end def rails_console? - console? && !!(defined?(Rails::Console) || ENV["RAILS_ENV"]) + console? && boolean(defined?(Rails::Console) || ENV['RAILS_ENV']) end def diet_rb @@ -29,7 +29,7 @@ module AwesomePrint IRB::Irb.class_eval do def output_value ap @context.last_value - rescue NoMethodError + rescue NoMethodError puts "(Object doesn't support #ai)" end end @@ -38,11 +38,20 @@ module AwesomePrint def irb! return unless defined?(IRB) - IRB.version.include?("DietRB") ? diet_rb : usual_rb + IRB.version.include?('DietRB') ? diet_rb : usual_rb end def pry! Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry) end + + private + + # Takes a value and returns true unless it is false or nil + # This is an alternative to the less readable !!(value) + # https://github.com/bbatsov/ruby-style-guide#no-bang-bang + def boolean(value) + value ? true : false + end end end diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 9da697d..f5a43a6 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -13,35 +13,35 @@ module AwesomePrint def initialize(options = {}) @options = { - :indent => 4, # Indent using 4 spaces. - :index => true, # Display array indices. - :html => false, # Use ANSI color codes rather than HTML. - :multiline => true, # Display in multiple lines. - :plain => false, # Use colors. - :raw => false, # Do not recursively format object instance variables. - :sort_keys => false, # Do not sort hash keys. - :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. - :new_hash_syntax => false, # Use the JSON like syntax { foo: 'bar' }, when the key is a symbol - :color => { - :args => :pale, - :array => :white, - :bigdecimal => :blue, - :class => :yellow, - :date => :greenish, - :falseclass => :red, - :fixnum => :blue, - :float => :blue, - :hash => :pale, - :keyword => :cyan, - :method => :purpleish, - :nilclass => :red, - :rational => :blue, - :string => :yellowish, - :struct => :pale, - :symbol => :cyanish, - :time => :greenish, - :trueclass => :green, - :variable => :cyanish + indent: 4, # Number of spaces for indenting. + index: true, # Display array indices. + html: false, # Use ANSI color codes rather than HTML. + multiline: true, # Display in multiple lines. + plain: false, # Use colors. + raw: false, # Do not recursively format instance variables. + sort_keys: false, # Do not sort hash keys. + limit: false, # Limit arrays & hashes. Accepts bool or int. + new_hash_syntax: false, # Use Ruby 1.9 hash syntax in output. + color: { + args: :pale, + array: :white, + bigdecimal: :blue, + class: :yellow, + date: :greenish, + falseclass: :red, + fixnum: :blue, + float: :blue, + hash: :pale, + keyword: :cyan, + method: :purpleish, + nilclass: :red, + rational: :blue, + string: :yellowish, + struct: :pale, + symbol: :cyanish, + time: :greenish, + trueclass: :green, + variable: :cyanish } } @@ -81,7 +81,15 @@ module AwesomePrint #--------------------------------------------------------------------------- def colorize? AwesomePrint.force_colors ||= false - AwesomePrint.force_colors || (STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON'])) + AwesomePrint.force_colors || ( + STDOUT.tty? && ( + ( + ENV['TERM'] && + ENV['TERM'] != 'dumb' + ) || + ENV['ANSICON'] + ) + ) end private @@ -106,8 +114,9 @@ module AwesomePrint @formatter.format(object, printable(object)) end - # Turn class name into symbol, ex: Hello::World => :hello_world. Classes that - # inherit from Array, Hash, File, Dir, and Struct are treated as the base class. + # Turn class name into symbol, ex: Hello::World => :hello_world. Classes + # that inherit from Array, Hash, File, Dir, and Struct are treated as the + # base class. #--------------------------------------------------------------------------- def printable(object) case object @@ -120,7 +129,8 @@ module AwesomePrint end end - # Update @options by first merging the :color hash and then the remaining keys. + # Update @options by first merging the :color hash and then the remaining + # keys. #--------------------------------------------------------------------------- def merge_options!(options = {}) @options[:color].merge!(options.delete(:color) || {})