renc 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +1 -0
- data/CHANGELOG.md +9 -0
- data/README.md +2 -2
- data/lib/renc.rb +21 -14
- data/lib/renc/configuration.rb +7 -4
- data/lib/renc/version.rb +1 -1
- metadata +3 -5
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f70f4e782058204a07038072f5f044f6c70a6ac
|
4
|
+
data.tar.gz: c8b8331054b0c93b40fca1fa9d7db3d27c0e4810
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c5f4a131d471a306f3f1cf58cef2eee70e5df70bbda6c3c46b2de09ab67e5299d8cc1348fdb9ec0ecab155364ada431d64ad5e0e369bc0917fadcb13f55aa67
|
7
|
+
data.tar.gz: d632b298ec50c4c6ca6e693b0d50cb07df1acf9603102e55c0a6e0c6835a10616be4cb696ab5d8754652597d2d11125ca4b7f4a222e5937784efcbe89102d51e
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
# v2.2.0
|
2
|
+
2017-08-11 JST
|
3
|
+
- [issue #25](https://github.com/k-ta-yamada/renc/issues/25)
|
4
|
+
[add] Add target Class Struct #25
|
5
|
+
- [issue #26](https://github.com/k-ta-yamada/renc/issues/26)
|
6
|
+
[mod] Add error message details #26
|
7
|
+
- [issue #23](https://github.com/k-ta-yamada/renc/issues/23)
|
8
|
+
[clean] Renc::Configuration.default_xxx='s comment is wrong #23
|
9
|
+
|
1
10
|
# v2.1.0
|
2
11
|
- [issue #21](https://github.com/k-ta-yamada/renc/issues/21)
|
3
12
|
change class structure; Separate Configuration #21
|
data/README.md
CHANGED
@@ -52,7 +52,7 @@ hash_val.renc[:a].encoding # => #<Encoding::UTF-8>
|
|
52
52
|
hash_val.renc == hash_val # => true
|
53
53
|
```
|
54
54
|
|
55
|
-
### Nested Hash
|
55
|
+
### Nested Hash and Array.
|
56
56
|
> @ref [./spec/spec_helper.rb](https://github.com/k-ta-yamada/renc/blob/master/spec/spec_helper.rb#L18)
|
57
57
|
|
58
58
|
```ruby
|
@@ -107,7 +107,7 @@ Renc.default_encoding = Encoding::ASCII
|
|
107
107
|
Renc.default_options # => { undef: :replace }
|
108
108
|
'🐘'.renc # => '?'
|
109
109
|
|
110
|
-
# if you want to change to
|
110
|
+
# if you want to change to { undef: nil }
|
111
111
|
Renc.default_options = { undef: nil }
|
112
112
|
'🐘'.renc # => Encoding::UndefinedConversionError: U+1F418 from UTF-8 to US-ASCII
|
113
113
|
|
data/lib/renc.rb
CHANGED
@@ -12,7 +12,7 @@ module Renc
|
|
12
12
|
extend Configuration
|
13
13
|
|
14
14
|
# for include #renc method
|
15
|
-
TARGET_CLASS = [String, Array, Hash].freeze
|
15
|
+
TARGET_CLASS = [String, Array, Hash, Struct].freeze
|
16
16
|
TARGET_CLASS.each { |klass| klass.send(:include, self) }
|
17
17
|
|
18
18
|
# recursive encode for Hash and Array.
|
@@ -41,32 +41,39 @@ module Renc
|
|
41
41
|
# @see .default_encoding
|
42
42
|
# @see .default_options
|
43
43
|
def renc(encoding = Renc.default_encoding, options = Renc.default_options)
|
44
|
-
raise TypeError unless encoding.is_a?(Encoding)
|
45
|
-
raise TypeError unless options.is_a?(Hash)
|
44
|
+
raise TypeError, ERR_MESSAGE_ENCODING unless encoding.is_a?(Encoding)
|
45
|
+
raise TypeError, ERR_MESSAGE_OPTIONS unless options.is_a?(Hash)
|
46
46
|
|
47
|
-
|
47
|
+
@encoding = encoding
|
48
|
+
@options = options
|
49
|
+
|
50
|
+
_renc(self)
|
48
51
|
end
|
49
52
|
|
50
53
|
private
|
51
54
|
|
52
|
-
def
|
55
|
+
def _renc(obj)
|
53
56
|
case obj
|
54
|
-
when
|
55
|
-
when
|
56
|
-
when
|
57
|
+
when String then obj.encode(@encoding, @options)
|
58
|
+
when Hash then _hash(obj)
|
59
|
+
when Array then _array(obj)
|
60
|
+
when Struct then _struct(obj)
|
57
61
|
else obj
|
58
62
|
end
|
59
63
|
end
|
60
64
|
|
61
65
|
# recursive encode for Hash values of String.
|
62
|
-
def
|
63
|
-
obj.each_with_object({})
|
64
|
-
h[key] = renc_internal(val, encoding, options)
|
65
|
-
end
|
66
|
+
def _hash(obj)
|
67
|
+
obj.each_with_object({}) { |(k, v), h| h[k] = _renc(v) }
|
66
68
|
end
|
67
69
|
|
68
70
|
# recursive encode for Array values of String.
|
69
|
-
def
|
70
|
-
obj.map { |
|
71
|
+
def _array(obj)
|
72
|
+
obj.map { |v| _renc(v) }
|
73
|
+
end
|
74
|
+
|
75
|
+
# recursive encode for Hash values of Struct.
|
76
|
+
def _struct(obj)
|
77
|
+
obj.class.new(*_renc(obj.to_h).values)
|
71
78
|
end
|
72
79
|
end
|
data/lib/renc/configuration.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
module Renc
|
2
|
+
ERR_MESSAGE_ENCODING = 'argument `encoding` is not a Encoding Class'.freeze
|
3
|
+
ERR_MESSAGE_OPTIONS = 'argument `options` is not a Hash Class'.freeze
|
4
|
+
|
2
5
|
# namespace
|
3
6
|
module Configuration
|
4
7
|
# this gem's default configured encoding
|
@@ -18,11 +21,11 @@ module Renc
|
|
18
21
|
|
19
22
|
# configure default encoding
|
20
23
|
# @example
|
21
|
-
# Renc.default_encoding = 1 # =>
|
24
|
+
# Renc.default_encoding = 1 # => TypeError
|
22
25
|
# Renc.default_encoding = Encoding::ASCII
|
23
26
|
# @param encoding [Encoding]
|
24
27
|
def default_encoding=(encoding)
|
25
|
-
raise TypeError unless encoding.is_a?(Encoding)
|
28
|
+
raise TypeError, ERR_MESSAGE_ENCODING unless encoding.is_a?(Encoding)
|
26
29
|
@default_encoding = encoding
|
27
30
|
end
|
28
31
|
|
@@ -35,11 +38,11 @@ module Renc
|
|
35
38
|
|
36
39
|
# configure default options
|
37
40
|
# @example
|
38
|
-
# Renc.default_options = 1 # =>
|
41
|
+
# Renc.default_options = 1 # => TypeError
|
39
42
|
# Renc.default_options = { undef: nil }
|
40
43
|
# @param options [Hash]
|
41
44
|
def default_options=(options)
|
42
|
-
raise TypeError unless options.is_a?(Hash)
|
45
|
+
raise TypeError, ERR_MESSAGE_OPTIONS unless options.is_a?(Hash)
|
43
46
|
@default_options = options
|
44
47
|
end
|
45
48
|
end
|
data/lib/renc/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: renc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- k-ta-yamada
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -154,8 +154,6 @@ files:
|
|
154
154
|
- LICENSE.txt
|
155
155
|
- README.md
|
156
156
|
- Rakefile
|
157
|
-
- bin/console
|
158
|
-
- bin/setup
|
159
157
|
- lib/renc.rb
|
160
158
|
- lib/renc/configuration.rb
|
161
159
|
- lib/renc/version.rb
|
@@ -180,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
180
178
|
version: '0'
|
181
179
|
requirements: []
|
182
180
|
rubyforge_project:
|
183
|
-
rubygems_version: 2.6.
|
181
|
+
rubygems_version: 2.6.11
|
184
182
|
signing_key:
|
185
183
|
specification_version: 4
|
186
184
|
summary: recursive encode for Hash and Array.
|
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'bundler/setup'
|
4
|
-
require 'renc'
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
require 'pry'
|
11
|
-
Pry.start
|
12
|
-
|
13
|
-
# require 'irb'
|
14
|
-
# IRB.start
|