hash_formatter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0cc56465f40a0aa9a676fd0121e9a2c72428d8ec
4
+ data.tar.gz: 12c9d34af09dd26d34c9cd14196085207087ae86
5
+ SHA512:
6
+ metadata.gz: 76c6693a600ccb8d05bf5ba394dc546c1652ee38bd0ccb3aa9ee5dab3a3c15b39aeb974d890dce1306907428ae334211764142666b29c90897da126e0ad28e95
7
+ data.tar.gz: 4c5523d3fee191937f87943c2b25c13d1ec0b66c6d83227c73d8a821e472e00f2283c315b4bf64aab4113e9b648ea2043bc20ac4bba70395780df05ab55495d9
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2015 Norman Clarke
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ # Hash Formatter
2
+
3
+ ## Summary
4
+
5
+ This is a small, simple library for formatting Ruby Hashes, intended to be used by text editors. It uses [Ruby Parser](https://github.com/seattlerb/ruby_parser) and [Ruby2Ruby](https://github.com/seattlerb/ruby2ruby) rather than regular expressions, letting it parse even the trickiest Ruby code with ease.
6
+
7
+ At the moment, its formatting options are opinionated and simplistic. You can format Ruby hashes in the following formats:
8
+
9
+ Single-line, Ruby 1.9:
10
+
11
+ {foo: bar, baz: bam}
12
+
13
+ Single-line, Ruby 1.8:
14
+
15
+ {:foo => bar, :baz => bam}
16
+
17
+ Multiline, Ruby 1.8:
18
+
19
+ {
20
+ :foo => bar,
21
+ :baz => bam,
22
+ :hash_keys => 'are aligned'
23
+ }
24
+
25
+ These choices allow me to edit code using my completely subjective, arbitrary personal preferences:
26
+
27
+ * Use hashrockets and align assignments for multiline Hashes.
28
+ * Use 1.9 style Hashes for single-line hashes, except when the values are symbols, because `{foo: :bar}` looks stupid to me.
29
+
30
+ ## API
31
+
32
+ ```ruby
33
+ require 'hash_formatter'
34
+ hash = {:foo => 'bar'}
35
+ formatter = HashFormatter.new(hash.inspect)
36
+ formatter.to_18 #=> '{:foo => "bar"}'
37
+ formatter.to_19 #=> '{foo: "bar"}'
38
+ formatter.to_multiline #=> "{\n :foo => "bar"\n}"
39
+ ```
40
+
41
+ ## Docs
42
+
43
+ None, yet, but the source is less than 100 lines so you might want to start there.
44
+
45
+ ## Vim Integration
46
+
47
+ Coming Real Soon™.
48
+
49
+ ## Author
50
+
51
+ [Norman Clarke](mailto:norman@njclarke.com)
52
+
53
+ ## License
54
+
55
+ Copyright (c) 2015 Norman Clarke
56
+
57
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
58
+
59
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
60
+
61
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,10 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "lib"
8
+ t.test_files = FileList['test/*_test.rb']
9
+ t.verbose = true
10
+ end
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "hash_formatter"
3
+ s.version = "0.0.1"
4
+ s.summary = "Formats Ruby Hashes"
5
+ s.description = "Hash Formatter is a library that formats Ruby hashes for code editors."
6
+ s.authors = ["Norman Clarke"]
7
+ s.email = ["norman@njclarke.com"]
8
+ s.homepage = "https://github.com/norman/hash_formatter"
9
+ s.files = `git ls-files`.split("\n")
10
+ s.license = "MIT"
11
+
12
+ s.add_dependency 'ruby2ruby'
13
+ s.add_dependency 'ruby_parser'
14
+ s.add_development_dependency 'rake'
15
+ end
16
+
@@ -0,0 +1,58 @@
1
+ require 'ruby_parser'
2
+ require 'ruby2ruby'
3
+
4
+ class HashFormatter
5
+ include Enumerable
6
+
7
+ def initialize(string)
8
+ @parser = RubyParser.new
9
+ @r2r = Ruby2Ruby.new
10
+ @hash = parse(string)
11
+ end
12
+
13
+ def to_multiline
14
+ "{\n%s\n}" % map {|(k, v)| " #{pad(k)} => #{v}"}.join(",\n")
15
+ end
16
+
17
+ def to_18
18
+ "{%s}" % map {|a| a.join(' => ')}.join(', ')
19
+ end
20
+
21
+ def to_19
22
+ "{%s}" % map {|a| format_pair(*a)}.join(', ')
23
+ end
24
+
25
+ def each
26
+ return to_enum unless block_given?
27
+ @hash.map {|*a| yield a.flatten(1).map(&method(:to_ruby))}
28
+ end
29
+
30
+ private
31
+
32
+ def format_pair(k, v)
33
+ if k =~ /\A:[a-z0-9A-Z]+\z/
34
+ "%s: %s" % [k[1..-1], v]
35
+ else
36
+ "%s => %s" % [k, v]
37
+ end
38
+ end
39
+
40
+ def pad(key)
41
+ "%-#{max}s" % key
42
+ end
43
+
44
+ def max
45
+ @max ||= map {|a| a[0].length}.max
46
+ end
47
+
48
+ def to_ruby(sexp)
49
+ sexp = sexp.deep_clone
50
+ @r2r.process(sexp)
51
+ end
52
+
53
+ def parse(string)
54
+ sexp = @parser.process(string)
55
+ raise "Not a hash." unless sexp.shift == :hash
56
+ Hash[*sexp]
57
+ end
58
+ end
@@ -0,0 +1,25 @@
1
+ require 'minitest/autorun'
2
+ require 'hash_formatter'
3
+
4
+ class HashFormatterTest < Minitest::Test
5
+ def test_should_format_to_18_hash
6
+ input = "{foo: 'bar'}"
7
+ expected = '{:foo => "bar"}'
8
+ hf = HashFormatter.new(input)
9
+ assert_equal expected, hf.to_18
10
+ end
11
+
12
+ def test_should_format_to_19_hash
13
+ input = "{:foo => 'bar'}"
14
+ expected = '{foo: "bar"}'
15
+ hf = HashFormatter.new(input)
16
+ assert_equal expected, hf.to_19
17
+ end
18
+
19
+ def test_should_format_to_multiline_hash
20
+ input = "{:foo => 'bar', a => 'b'}"
21
+ expected = %Q|{\n :foo => "bar",\n a => "b"\n}|
22
+ hf = HashFormatter.new(input)
23
+ assert_equal expected, hf.to_multiline
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_formatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Norman Clarke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby2ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby_parser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: rake
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: Hash Formatter is a library that formats Ruby hashes for code editors.
56
+ email:
57
+ - norman@njclarke.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - hash_formatter.gemspec
66
+ - lib/hash_formatter.rb
67
+ - test/hash_formatter_test.rb
68
+ homepage: https://github.com/norman/hash_formatter
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.4.7
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Formats Ruby Hashes
92
+ test_files: []
93
+ has_rdoc: