to_h 0.0.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of to_h might be problematic. Click here for more details.

checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 379af43ffa11d252e2842bf58029159c5b6f8b3e
4
+ data.tar.gz: 0c34d7afca789b51057db6a055d34443cbece980
5
+ SHA512:
6
+ metadata.gz: 1efb9a474962be0a2634a7e58a5e49a9ba609907a784672d30b1e16f5a20983fe3de00d531a429a51f44a31dc6696f2489fc2abb106c87bbd48cfed11c67e2f8
7
+ data.tar.gz: b9ee0a7bd5d3409658696832d4e0eb9328c292ea360dd02ca1468a34caa57c68c23a3958ce88e6fa3dc797b7662091fe9b634010d8385327076f72f36c2615dc
data/.gitignore ADDED
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ coverage.data
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+
16
+ # bundler
17
+ vendor/
18
+
19
+ # YARD artifacts
20
+ .yardoc
21
+ _yardoc
22
+ doc/
23
+
24
+ # tmp-old
25
+ .old
26
+
27
+ # editor
28
+ *~
29
+ .redcar
30
+
31
+ # other
32
+ *.lock
33
+ *.bak
34
+ tool/
35
+ *\#*
data/.travis.yml ADDED
@@ -0,0 +1,25 @@
1
+ language: ruby
2
+
3
+ branches:
4
+ only:
5
+ - master
6
+
7
+ rvm:
8
+ - 2.2
9
+ - 2.1
10
+ - 2.0
11
+ - 1.9.3
12
+ - 1.9.2
13
+ - 1.8.7
14
+ - ruby-head
15
+ - rbx-2
16
+ - jruby-head
17
+
18
+ matrix:
19
+ allow_failures:
20
+ - rvm: ruby-head
21
+ - rvm: rbx-2
22
+ - rvm: jruby-head
23
+
24
+ before_install:
25
+ - gem install bundler
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --readme README.md lib/**/*.rb
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'rake'
7
+ end
8
+
9
+ group :test do
10
+ gem 'rake'
11
+ end
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT X11 License)
2
+
3
+ Copyright (c) 2012 Kenichi Kamiya
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 NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ to_h
2
+ =====
3
+
4
+ [![Build Status](https://secure.travis-ci.org/kachick/to_h.png)](http://travis-ci.org/kachick/to_h)
5
+ [![Gem Version](https://badge.fury.io/rb/to_h.png)](http://badge.fury.io/rb/to_h)
6
+
7
+ Description
8
+ -----------
9
+
10
+ Add SomePairs#to_h for compatibility with Ruby 2.0+
11
+ http://d.hatena.ne.jp/nagachika/20120416/ruby_trunk_changes_35337_35352
12
+
13
+ Features
14
+ --------
15
+
16
+ Ruby 2.0+
17
+
18
+ * Hash#to_h
19
+ * Struct#to_h
20
+ * ENV.to_h
21
+ * NilClass#to_h
22
+ * OpenStruct#to_h
23
+
24
+ Ruby 2.1+
25
+
26
+ * Enumerable#to_h
27
+ * Array#to_h
28
+
29
+ Usage
30
+ -----
31
+
32
+ ### Basic
33
+
34
+ ```ruby
35
+ require 'to_h'
36
+ ```
37
+
38
+ ### With OpenStruct
39
+
40
+ ```ruby
41
+ require 'ostruct'
42
+ require 'ostruct/to_h'
43
+ ```
44
+
45
+ Requirements
46
+ -------------
47
+
48
+ * Ruby - [1.8.7 or later](http://travis-ci.org/#!/kachick/to_h)
49
+
50
+ Install
51
+ -------
52
+
53
+ ```bash
54
+ $ gem install to_h
55
+ ```
56
+
57
+ Link
58
+ ----
59
+
60
+ * [Home](http://kachick.github.com/to_h/)
61
+ * [code](https://github.com/kachick/to_h)
62
+ * [API](http://kachick.github.com/to_h/yard/frames.html)
63
+ * [issues](https://github.com/kachick/to_h/issues)
64
+ * [CI](http://travis-ci.org/#!/kachick/to_h)
65
+ * [gem](https://rubygems.org/gems/to_h)
66
+
67
+ License
68
+ --------
69
+
70
+ The MIT X11 License
71
+ Copyright (c) 2012 Kenichi Kamiya
72
+ See MIT-LICENSE for further details.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rake/testtask'
5
+
6
+ task :default => [:test]
7
+
8
+ Rake::TestTask.new do |tt|
9
+ tt.verbose = true
10
+ tt.warning = true
11
+ end
@@ -0,0 +1,14 @@
1
+ require 'ostruct'
2
+
3
+ if RUBY_VERSION <= '2.0'
4
+
5
+ class OpenStruct
6
+
7
+ # @return [Hash]
8
+ def to_h
9
+ @table.dup
10
+ end
11
+
12
+ end
13
+
14
+ end
data/lib/to_h.rb ADDED
@@ -0,0 +1,92 @@
1
+ # to_h - Add SomePairs#to_h for compatibility with Ruby 2.0~
2
+ # Copyright (c) 2012 Kenichi Kamiya
3
+
4
+ # @see http://d.hatena.ne.jp/nagachika/20120416/ruby_trunk_changes_35337_35352
5
+
6
+ if RUBY_VERSION < '2.0'
7
+
8
+ class Hash
9
+
10
+ # @return [Hash]
11
+ def to_h
12
+ instance_of?(Hash) ? self : Hash[self]
13
+ end
14
+
15
+ end
16
+
17
+ class Struct
18
+
19
+ # @return [Hash]
20
+ def to_h
21
+ Hash[each_pair.to_a]
22
+ end
23
+
24
+ end
25
+
26
+ class << ENV
27
+
28
+ # @return [Hash]
29
+ alias_method :to_h, :to_hash
30
+
31
+ end
32
+
33
+ class NilClass
34
+
35
+ # @return [Hash]
36
+ def to_h
37
+ {}
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+
44
+ if RUBY_VERSION < '2.1'
45
+
46
+ module Enumerable
47
+
48
+ # @return [Hash]
49
+ def to_h(*args)
50
+ hash = {}
51
+
52
+ each(*args) do |element|
53
+ unless element.respond_to? :to_ary
54
+ raise TypeError, "wrong element type #{element.class} (expected array)"
55
+ end
56
+ pair = element.to_ary
57
+ unless pair.length == 2
58
+ raise ArgumentError, "element has wrong array length (expected 2, was #{pair.length})"
59
+ end
60
+ hash[pair[0]] = pair[1]
61
+ end
62
+
63
+ hash
64
+ end
65
+
66
+ end
67
+
68
+ class Array
69
+
70
+ # Delegating to Enumerable#to_h is OK.
71
+ # This override keeps arity and better warnings.
72
+ # @return [Hash]
73
+ def to_h
74
+ hash = {}
75
+
76
+ each_with_index do |element, index|
77
+ unless element.respond_to? :to_ary
78
+ raise TypeError, "wrong element type #{element.class} at #{index} (expected array)"
79
+ end
80
+ pair = element.to_ary
81
+ unless pair.length == 2
82
+ raise ArgumentError, "wrong array length at #{index} (expected 2, was #{pair.length})"
83
+ end
84
+ hash[pair[0]] = pair[1]
85
+ end
86
+
87
+ hash
88
+ end
89
+
90
+ end
91
+
92
+ end
data/test/test_to_h.rb ADDED
@@ -0,0 +1,130 @@
1
+ require 'test/unit'
2
+ require "#{File.dirname(File.dirname(__FILE__))}/lib/to_h"
3
+
4
+ class Test_Hash_to_h < Test::Unit::TestCase
5
+
6
+ class HashSubclass < Hash; end
7
+
8
+ def test_to_h
9
+ h = {}
10
+ assert_same h, h.to_h
11
+ assert_instance_of Hash, HashSubclass.new.to_h
12
+ end
13
+
14
+ end
15
+
16
+ class Test_Struct_to_h < Test::Unit::TestCase
17
+
18
+ FooBar = Struct.new :foo, :bar
19
+
20
+ def test_to_h
21
+ assert_equal({:foo => 456, :bar => 123}, FooBar.new(456, 123).to_h)
22
+ end
23
+
24
+ end
25
+
26
+ class Test_ENV_to_h < Test::Unit::TestCase
27
+
28
+ def test_to_h
29
+ assert_equal ENV.to_hash, ENV.to_h
30
+ end
31
+
32
+ end
33
+
34
+ class Test_NilClass_to_h < Test::Unit::TestCase
35
+
36
+ def test_to_h
37
+ assert_equal({}, nil.to_h)
38
+ end
39
+
40
+ end
41
+
42
+ class Test_Enumerable_to_h < Test::Unit::TestCase
43
+
44
+ class Enum
45
+ include Enumerable
46
+
47
+ def each
48
+ yield [1, 2]
49
+ yield [8, 9]
50
+ end
51
+ end
52
+
53
+ class EnumWithArg
54
+ include Enumerable
55
+
56
+ def each(arg)
57
+ yield arg
58
+ yield [8, 9]
59
+ end
60
+ end
61
+
62
+ class EnumWithArgs
63
+ include Enumerable
64
+
65
+ def each(*args)
66
+ yield args
67
+ yield [8, 9]
68
+ end
69
+ end
70
+
71
+ HasToAry = Object.new
72
+ def HasToAry.to_ary
73
+ [1, 2]
74
+ end
75
+
76
+ def test_to_h
77
+ assert_equal -1, Enumerable.instance_method(:to_h).arity
78
+
79
+ assert_equal({1 => 2, 8 => 9}, Enum.new.to_h)
80
+ assert_equal({1 => 2, 8 => 9}, EnumWithArg.new.to_h([1, 2]))
81
+ assert_equal({1 => 2, 8 => 9}, EnumWithArgs.new.to_h(1, 2))
82
+ assert_equal({1 => 2, 8 => 9}, EnumWithArg.new.to_h(HasToAry))
83
+
84
+ assert_raise TypeError do
85
+ EnumWithArg.new.to_h("This object does not have `.to_ary'")
86
+ end
87
+
88
+ assert_raise ArgumentError do
89
+ EnumWithArgs.new.to_h.each(1, 2, 3)
90
+ end
91
+ end
92
+
93
+ end
94
+
95
+ class Test_Array_to_h < Test::Unit::TestCase
96
+
97
+ HasToAry = Object.new
98
+ def HasToAry.to_ary
99
+ [1, 2]
100
+ end
101
+
102
+ def test_to_h
103
+ assert_equal 0, Array.instance_method(:to_h).arity
104
+
105
+ assert_equal({1 => 2, 8 => 9}, [[1, 2], [8, 9]].to_h)
106
+ assert_equal({1 => 2, 8 => 9}, [HasToAry, [8, 9]].to_h)
107
+
108
+ assert_raise TypeError do
109
+ ["This object does not have `.to_ary'", [8, 9]].to_h
110
+ end
111
+
112
+ assert_raise ArgumentError do
113
+ [[1, 2], [8, 9, 10]].to_h
114
+ end
115
+ end
116
+
117
+ end
118
+
119
+ require "#{File.dirname(File.dirname(__FILE__))}/lib/ostruct/to_h"
120
+
121
+ class Test_OpenStruct_to_h < Test::Unit::TestCase
122
+
123
+ def test_to_h
124
+ ost = OpenStruct.new
125
+ ost.foo = 456
126
+ ost.bar = 123
127
+ assert_equal({:foo => 456, :bar => 123}, ost.to_h)
128
+ end
129
+
130
+ end
data/to_h.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = 'to_h'
3
+ gem.version = '0.0.3'
4
+ gem.summary = 'Add SomePairs#to_h for compatibility with Ruby 2.0+'
5
+ gem.description = gem.summary.dup
6
+ gem.homepage = 'http://kachick.github.com/to_h/'
7
+
8
+ gem.required_ruby_version = '>= 1.8.7'
9
+
10
+ gem.add_development_dependency 'yard', '>= 0.8.7.6', '< 0.9'
11
+ gem.add_development_dependency 'rake', '~> 10'
12
+ gem.add_development_dependency 'bundler', '>= 1.10', '< 2'
13
+ gem.add_development_dependency 'test-unit', '>= 3.1.1', '< 4'
14
+
15
+ # common
16
+
17
+ gem.authors = ['Kenichi Kamiya']
18
+ gem.email = ['kachick1+ruby@gmail.com']
19
+ gem.files = `git ls-files`.split($\)
20
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
22
+ gem.require_paths = ['lib']
23
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_h
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Kenichi Kamiya
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: yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.7.6
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '0.9'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.7.6
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '0.9'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '10'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '10'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '1.10'
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '2'
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '1.10'
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '2'
67
+ - !ruby/object:Gem::Dependency
68
+ name: test-unit
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 3.1.1
74
+ - - "<"
75
+ - !ruby/object:Gem::Version
76
+ version: '4'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 3.1.1
84
+ - - "<"
85
+ - !ruby/object:Gem::Version
86
+ version: '4'
87
+ description: Add SomePairs#to_h for compatibility with Ruby 2.0+
88
+ email:
89
+ - kachick1+ruby@gmail.com
90
+ executables: []
91
+ extensions: []
92
+ extra_rdoc_files: []
93
+ files:
94
+ - ".gitignore"
95
+ - ".travis.yml"
96
+ - ".yardopts"
97
+ - Gemfile
98
+ - MIT-LICENSE
99
+ - README.md
100
+ - Rakefile
101
+ - lib/ostruct/to_h.rb
102
+ - lib/to_h.rb
103
+ - test/test_to_h.rb
104
+ - to_h.gemspec
105
+ homepage: http://kachick.github.com/to_h/
106
+ licenses: []
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 1.8.7
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.4.5
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Add SomePairs#to_h for compatibility with Ruby 2.0+
128
+ test_files:
129
+ - test/test_to_h.rb
130
+ has_rdoc: