From d45d127afc040fbcece26a4e3e684d7a5ca355c8 Mon Sep 17 00:00:00 2001 From: Aaron Heesakkers Date: Tue, 10 Jan 2017 13:58:55 +0100 Subject: [PATCH] sort_vars option --- README.md | 1 + .../formatters/object_formatter.rb | 2 +- lib/awesome_print/inspector.rb | 1 + spec/objects_spec.rb | 36 +++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7e60824..a418e10 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ 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. +sort_vars: true, # Sort instance variables. limit: false, # Limit arrays & hashes. Accepts bool or int. ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output. color: { diff --git a/lib/awesome_print/formatters/object_formatter.rb b/lib/awesome_print/formatters/object_formatter.rb index 655a8e6..a63d0f5 100644 --- a/lib/awesome_print/formatters/object_formatter.rb +++ b/lib/awesome_print/formatters/object_formatter.rb @@ -28,7 +28,7 @@ module AwesomePrint end end - data = vars.sort.map do |declaration, var| + data = (options[:sort_vars] ? vars.sort : vars).map do |declaration, var| key = left_aligned do align(declaration, declaration.size) end diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 2a226b7..d0e131f 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -20,6 +20,7 @@ module AwesomePrint plain: false, # Use colors. raw: false, # Do not recursively format instance variables. sort_keys: false, # Do not sort hash keys. + sort_vars: true, # Sort instance variables. limit: false, # Limit arrays & hashes. Accepts bool or int. ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output. color: { diff --git a/spec/objects_spec.rb b/spec/objects_spec.rb index 3aaef90..f4bdde4 100644 --- a/spec/objects_spec.rb +++ b/spec/objects_spec.rb @@ -127,6 +127,42 @@ EOS out = hello.ai(multiline: false, plain: true, raw: true) str = <<-EOS.strip # +EOS + expect(out).to be_similar_to(str) + expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect) + end + + it 'without the sort_vars option does not sort instance variables' do + class Hello + attr_reader :abra + attr_writer :ca + attr_accessor :dabra + + def initialize + @abra = 1 + @ca = 2 + @dabra = 3 + @scooby = 3 + @dooby = 2 + @doo = 1 + end + + def instance_variables + [:@scooby, :@dooby, :@doo, :@abra, :@ca, :@dabra] + end + end + + hello = Hello.new + out = hello.ai(plain: true, raw: true, sort_vars: false) + str = <<-EOS.strip +# EOS expect(out).to be_similar_to(str) expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect)