mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
added a flag to sort hash keys
This commit is contained in:
parent
73b83af69e
commit
0132bccb83
3 changed files with 33 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -19,3 +19,4 @@ rdoc
|
||||||
pkg
|
pkg
|
||||||
|
|
||||||
## PROJECT::SPECIFIC
|
## PROJECT::SPECIFIC
|
||||||
|
.rvmrc
|
||||||
|
|
|
@ -75,7 +75,8 @@ class AwesomePrint
|
||||||
def awesome_hash(h)
|
def awesome_hash(h)
|
||||||
return "{}" if h == {}
|
return "{}" if h == {}
|
||||||
|
|
||||||
data = h.keys.map do |key|
|
keys = @options[:sorted_hash_keys] ? h.keys.sort { |a, b| a.to_s <=> b.to_s } : h.keys
|
||||||
|
data = keys.map do |key|
|
||||||
plain_single_line do
|
plain_single_line do
|
||||||
[ awesome(key), h[key] ]
|
[ awesome(key), h[key] ]
|
||||||
end
|
end
|
||||||
|
|
|
@ -263,6 +263,36 @@ EOS
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
describe "Hash with several keys" do
|
||||||
|
before(:each) do
|
||||||
|
@hash = {"b" => "b", :a => "a", :z => "z", "alpha" => "alpha"}
|
||||||
|
end
|
||||||
|
|
||||||
|
it "plain multiline" do
|
||||||
|
@hash.ai(:plain => true).should == <<-EOS.strip
|
||||||
|
{
|
||||||
|
"b" => "b",
|
||||||
|
:a => "a",
|
||||||
|
:z => "z",
|
||||||
|
"alpha" => "alpha"
|
||||||
|
}
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
it "plain multiline with sorted keys" do
|
||||||
|
@hash.ai(:plain => true, :sorted_hash_keys => true).should == <<-EOS.strip
|
||||||
|
{
|
||||||
|
:a => "a",
|
||||||
|
"alpha" => "alpha",
|
||||||
|
"b" => "b",
|
||||||
|
:z => "z"
|
||||||
|
}
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
describe "Negative options[:indent]" do
|
describe "Negative options[:indent]" do
|
||||||
before(:each) do
|
before(:each) do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue