mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Left align instance variables and accessors
This commit is contained in:
parent
8044c29b50
commit
ac5c9fa043
2 changed files with 90 additions and 4 deletions
|
@ -124,11 +124,11 @@ module AwesomePrint
|
|||
end
|
||||
end
|
||||
|
||||
width = vars.map { |declaration,| declaration.size }.max || 0
|
||||
width += @indentation if @options[:indent] > 0
|
||||
|
||||
data = vars.sort.map do |declaration, var|
|
||||
key = align(declaration, width)
|
||||
key = left_aligned do
|
||||
align(declaration, declaration.size)
|
||||
end
|
||||
|
||||
unless @options[:plain]
|
||||
if key =~ /(@\w+)/
|
||||
key.sub!($1, colorize($1, :variable))
|
||||
|
@ -288,6 +288,13 @@ module AwesomePrint
|
|||
@indentation -= @options[:indent].abs
|
||||
end
|
||||
|
||||
def left_aligned
|
||||
current, @options[:indent] = @options[:indent], @options[:indent].abs * -1
|
||||
yield
|
||||
ensure
|
||||
@options[:indent] = current
|
||||
end
|
||||
|
||||
def indent
|
||||
' ' * @indentation
|
||||
end
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||
|
||||
describe "Single method" do
|
||||
before do
|
||||
stub_dotfile!
|
||||
end
|
||||
|
||||
after do
|
||||
Object.instance_eval{ remove_const :Hello } if defined?(Hello)
|
||||
end
|
||||
|
||||
describe "object" do
|
||||
it "attributes" do
|
||||
class Hello
|
||||
attr_reader :abra
|
||||
attr_writer :ca
|
||||
attr_accessor :dabra
|
||||
|
||||
def initialize
|
||||
@abra, @ca, @dabra = 1, 2, 3
|
||||
end
|
||||
end
|
||||
|
||||
out = Hello.new.ai(:plain => true)
|
||||
str = <<-EOS.strip
|
||||
#<Hello:0x01234567
|
||||
attr_accessor :dabra = 3,
|
||||
attr_reader :abra = 1,
|
||||
attr_writer :ca = 2
|
||||
>
|
||||
EOS
|
||||
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
||||
end
|
||||
|
||||
it "instance variables" do
|
||||
class Hello
|
||||
def initialize
|
||||
@abra, @ca, @dabra = 1, 2, 3
|
||||
end
|
||||
end
|
||||
|
||||
out = Hello.new.ai(:plain => true)
|
||||
str = <<-EOS.strip
|
||||
#<Hello:0x01234567
|
||||
@abra = 1,
|
||||
@ca = 2,
|
||||
@dabra = 3
|
||||
>
|
||||
EOS
|
||||
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
||||
end
|
||||
|
||||
it "attributes and instance variables" do
|
||||
class Hello
|
||||
attr_reader :abra
|
||||
attr_writer :ca
|
||||
attr_accessor :dabra
|
||||
|
||||
def initialize
|
||||
@abra, @ca, @dabra = 1, 2, 3
|
||||
@scooby, @dooby, @doo = 3, 2, 1
|
||||
end
|
||||
end
|
||||
|
||||
out = Hello.new.ai(:plain => true)
|
||||
str = <<-EOS.strip
|
||||
#<Hello:0x01234567
|
||||
@doo = 1,
|
||||
@dooby = 2,
|
||||
@scooby = 3,
|
||||
attr_accessor :dabra = 3,
|
||||
attr_reader :abra = 1,
|
||||
attr_writer :ca = 2
|
||||
>
|
||||
EOS
|
||||
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue