pretty_hash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 76cc2db6203106932551c4da8dff1c042531e648
4
+ data.tar.gz: a827f19e13bd551dc47ad343e98e757f440971f6
5
+ SHA512:
6
+ metadata.gz: daff7c23c4761626f23e309b25304809d1dee388e1912d77e72ac47f0950572604802b8b1708c014f445944b1a74d021d0ce0ba5397046ab4f2af137f3fac1aa
7
+ data.tar.gz: 1384cb71b0e54c96ca06180590e6e5f10adc354d691c529730bb4b5ae263df64cc9953e6131cf552c52ef1e35b45ea3ee19f780e045b301bd8bacba816a36d06
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ /vendor
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pretty_hash.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Kohei MATSUSHITA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Kohei MATSUSHITA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # PrettyHash
2
+
3
+ PrettyPrint the Hash
4
+
5
+ Format to the ascii table format for Hash.
6
+ It's very small library.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'pretty_hash'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install pretty_hash
21
+
22
+ ## Quick Start
23
+
24
+ ```
25
+ require 'pretty_hash'
26
+ puts {"a" => "abcde", "bbb" => "a"}.to_pp_table
27
+ ```
28
+
29
+ ```
30
+ ---|-----
31
+ a|abcde
32
+ bbb|a
33
+ ---|-----
34
+ ```
35
+
36
+ ## Features
37
+
38
+ ### Symbol key
39
+
40
+ `{:ccc => "aa", "dddd" => "a"}.to_pp_table`
41
+
42
+ ```
43
+ ----|--
44
+ :ccc|aa
45
+ dddd|a
46
+ ----|--
47
+ ```
48
+
49
+ ### Proc is applied to the value
50
+
51
+ ```
52
+ v1 = Struct.new(:name).new("foo")
53
+ v1.name
54
+ => "foo"
55
+ v2 = Struct.new(:name).new("bar")
56
+ v2.name
57
+ => "bar"
58
+ ```
59
+
60
+
61
+ `{:k1 => v1, :k2 => v2}.to_pp_table`
62
+
63
+ ```
64
+ ---|---------------------
65
+ :k1|#<struct name="hoge">
66
+ :k2|#<struct name="foo">
67
+ ---|---------------------
68
+ ```
69
+
70
+ Too bad....
71
+
72
+ `{:k1 => v1, :k2 => v2}.to_pp_table(Proc.new{|c|c.name})`
73
+
74
+ ```
75
+ ---|----
76
+ :k1|hoge
77
+ :k2|foo
78
+ ---|----
79
+ ```
80
+
81
+ Good!
82
+
83
+ ### Multibyte
84
+
85
+ `{:ccc => "日本語講座", "dddd" => "a"}.to_pp_table`
86
+
87
+ ```
88
+ ----|----------
89
+ :ccc|日本語講座
90
+ dddd|a
91
+ ----|----------
92
+ ```
93
+
94
+ Just fit width.
95
+
96
+ ## Not implement
97
+
98
+ * Nested value (e.g.) `{ a: => {:v1 => 1 }}`
99
+ Use to Proc.
100
+
101
+ ## Contributing
102
+
103
+ 1. Fork it ( https://github.com/[my-github-username]/pretty_hash/fork )
104
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
105
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
106
+ 4. Push to the branch (`git push origin my-new-feature`)
107
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,15 @@
1
+ class Hash
2
+ def to_pp_table(p = Proc.new{|c|c})
3
+ k_max_w = self.keys.map{|i|i.is_a?(Symbol) ? i.to_s.print_length+1 : i.to_s.print_length}.sort{|a ,b|b <=> a}.first
4
+ v_max_w = self.values.map{|i|p.call(i).to_s.print_length}.sort{|a, b| b <=> a }.first
5
+ header = ["-"*k_max_w, "|", "-"*v_max_w].join + "\n"
6
+ s = ""
7
+ s << header
8
+ self.each do |i|
9
+ k = i.first.is_a?(Symbol) ? ":#{i.first}" : i.first.to_s
10
+ s << [k.lpad(k_max_w), "|", p.call(i.last).to_s].join + "\n"
11
+ end
12
+ s << header
13
+ s
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ class String
2
+ def print_length
3
+ self.each_char.map{|c| c.bytesize == 1 ? 1 : 2}.reduce(0, &:+)
4
+ end
5
+
6
+ def rpad(length, padding = " ")
7
+ self + (padding * (length - self.print_length))
8
+ end
9
+
10
+ def lpad(length, padding = " ")
11
+ (padding * (length - self.print_length)) + self
12
+ end
13
+
14
+ def padding(length, type = nil)
15
+ begin # auto decection code.
16
+ raise ArgumentError if type == :string
17
+ Float(self)
18
+ self.lpad(length, "0")
19
+ rescue ArgumentError
20
+ self.rpad(length, " ")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module PrettyHash
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ require "pretty_hash/version"
2
+ require "pretty_hash/hash"
3
+ require "pretty_hash/string"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pretty_hash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pretty_hash"
8
+ spec.version = PrettyHash::VERSION
9
+ spec.authors = ["Kohei MATSUSHITA"]
10
+ spec.email = ["ma2shita+git@ma2shita.jp"]
11
+ spec.summary = %q{PrettyPrint the Hash}
12
+ spec.description = %q{Format to the ascii table format for Hash}
13
+ spec.homepage = "https://github.com/ma2shita/pretty_hash"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
data/spec/hash_spec.rb ADDED
@@ -0,0 +1,54 @@
1
+ require "spec_helper"
2
+ require "pretty_hash"
3
+
4
+ describe Hash do
5
+ describe "to_pp_table" do
6
+ let(:h1) { {"a" => "abcde", "bbb" => "a"} }
7
+ it "pp print" do
8
+ expect(h1.to_pp_table).to eq <<-EOT
9
+ ---|-----
10
+ a|abcde
11
+ bbb|a
12
+ ---|-----
13
+ EOT
14
+ end
15
+
16
+ let(:h2) { {:ccc => "aa", "dddd" => "a"} }
17
+ it "symbol key" do
18
+ expect(h2.to_pp_table).to eq <<-EOT
19
+ ----|--
20
+ :ccc|aa
21
+ dddd|a
22
+ ----|--
23
+ EOT
24
+ end
25
+
26
+ let(:h3) { {:k1 => Struct.new(:name).new("hoge"), :k2 => Struct.new(:name).new("foo")} }
27
+ it "use proc" do
28
+ expect(h3.to_pp_table).to eq <<-EOT
29
+ ---|---------------------
30
+ :k1|#<struct name="hoge">
31
+ :k2|#<struct name="foo">
32
+ ---|---------------------
33
+ EOT
34
+ expect(h3.to_pp_table(Proc.new{|c|c.name})).to eq <<-EOT
35
+ ---|----
36
+ :k1|hoge
37
+ :k2|foo
38
+ ---|----
39
+ EOT
40
+ end
41
+
42
+ let(:h4) { {:ccc => "日本語講座", "dddd" => "a"} }
43
+ it "multibyte" do
44
+ expect(h4.to_pp_table).to eq <<-EOT
45
+ ----|----------
46
+ :ccc|日本語講座
47
+ dddd|a
48
+ ----|----------
49
+ EOT
50
+ end
51
+
52
+ end
53
+ end
54
+
@@ -0,0 +1,78 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, make a
10
+ # separate helper file that requires this one and then use it only in the specs
11
+ # that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # The settings below are suggested to provide a good initial experience
19
+ # with RSpec, but feel free to customize to your heart's content.
20
+ =begin
21
+ # These two settings work together to allow you to limit a spec run
22
+ # to individual examples or groups you care about by tagging them with
23
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
24
+ # get run.
25
+ config.filter_run :focus
26
+ config.run_all_when_everything_filtered = true
27
+
28
+ # Many RSpec users commonly either run the entire suite or an individual
29
+ # file, and it's useful to allow more verbose output when running an
30
+ # individual spec file.
31
+ if config.files_to_run.one?
32
+ # Use the documentation formatter for detailed output,
33
+ # unless a formatter has already been configured
34
+ # (e.g. via a command-line flag).
35
+ config.default_formatter = 'doc'
36
+ end
37
+
38
+ # Print the 10 slowest examples and example groups at the
39
+ # end of the spec run, to help surface which specs are running
40
+ # particularly slow.
41
+ config.profile_examples = 10
42
+
43
+ # Run specs in random order to surface order dependencies. If you find an
44
+ # order dependency and want to debug it, you can fix the order by providing
45
+ # the seed, which is printed after each run.
46
+ # --seed 1234
47
+ config.order = :random
48
+
49
+ # Seed global randomization in this process using the `--seed` CLI option.
50
+ # Setting this allows you to use `--seed` to deterministically reproduce
51
+ # test failures related to randomization by passing the same `--seed` value
52
+ # as the one that triggered the failure.
53
+ Kernel.srand config.seed
54
+
55
+ # rspec-expectations config goes here. You can use an alternate
56
+ # assertion/expectation library such as wrong or the stdlib/minitest
57
+ # assertions if you prefer.
58
+ config.expect_with :rspec do |expectations|
59
+ # Enable only the newer, non-monkey-patching expect syntax.
60
+ # For more details, see:
61
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
62
+ expectations.syntax = :expect
63
+ end
64
+
65
+ # rspec-mocks config goes here. You can use an alternate test double
66
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
67
+ config.mock_with :rspec do |mocks|
68
+ # Enable only the newer, non-monkey-patching expect syntax.
69
+ # For more details, see:
70
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
71
+ mocks.syntax = :expect
72
+
73
+ # Prevents you from mocking or stubbing a method that does not exist on
74
+ # a real object. This is generally recommended.
75
+ mocks.verify_partial_doubles = true
76
+ end
77
+ =end
78
+ end
@@ -0,0 +1,29 @@
1
+ require "spec_helper"
2
+ require "pretty_hash"
3
+
4
+ describe String do
5
+ it "print_length" do
6
+ expect("にほんごabc".print_length).to eq 11
7
+ end
8
+
9
+ it "rpad" do
10
+ expect("a".rpad(5)).to eq "a "
11
+ expect("a".rpad(5, "-")).to eq "a----"
12
+ expect("日本".rpad(5, "#")).to eq "日本#"
13
+ end
14
+ it "lpad" do
15
+ expect("a".lpad(5)).to eq " a"
16
+ expect("a".lpad(5, "-")).to eq "----a"
17
+ expect("日本".lpad(5, "#")).to eq "#日本"
18
+ end
19
+ it "auto detect padding" do
20
+ expect("aa".padding(6)).to eq "aa "
21
+ expect("120".padding(6)).to eq "000120"
22
+ expect("120.0".padding(6)).to eq "0120.0"
23
+ end
24
+ it "width over" do
25
+ expect{"abcdef".rpad(5)}.to raise_error(ArgumentError)
26
+ expect{"日本語".rpad(5)}.to raise_error(ArgumentError)
27
+ end
28
+ end
29
+
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pretty_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kohei MATSUSHITA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Format to the ascii table format for Hash
56
+ email:
57
+ - ma2shita+git@ma2shita.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/pretty_hash.rb
70
+ - lib/pretty_hash/hash.rb
71
+ - lib/pretty_hash/string.rb
72
+ - lib/pretty_hash/version.rb
73
+ - pretty_hash.gemspec
74
+ - spec/hash_spec.rb
75
+ - spec/spec_helper.rb
76
+ - spec/string_spec.rb
77
+ homepage: https://github.com/ma2shita/pretty_hash
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: PrettyPrint the Hash
101
+ test_files:
102
+ - spec/hash_spec.rb
103
+ - spec/spec_helper.rb
104
+ - spec/string_spec.rb