renc 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{CAHNGELOG.md → CHANGELOG.md} +4 -0
- data/lib/renc.rb +31 -8
- data/lib/renc/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb71daa147a8f421b6596731e638044fa7dae64f
|
4
|
+
data.tar.gz: d01f8c0d216baaf810601d557280af7db92f1aed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 837078d4f5c516c5efa4d4d61340c70c46a7deae5dbed5491bc9801dd3260e2c860564f2ae3a63d568cbc0cd2057cfa227d05a83c47a76d90b0eae54233a253b
|
7
|
+
data.tar.gz: b314a50cfbeb37858b7b8dbb7b3854cfbcc9f774ac7ef2acc9b30954a566609b41c126f47af815c0b7f17788bb2675d6db64c43f5ad3bf679cd56df81524ee44
|
data/lib/renc.rb
CHANGED
@@ -1,16 +1,38 @@
|
|
1
1
|
require 'renc/version'
|
2
2
|
|
3
|
-
#
|
3
|
+
# recurse encoding for Hash and Array.
|
4
|
+
# @example
|
5
|
+
# default_src_encoding # => #<Encoding:UTF-8>
|
6
|
+
#
|
7
|
+
# # Hash values
|
8
|
+
# result = { a: 'a', b: 'b', c: 'c' }.renc(Encoding::Windows_31J)
|
9
|
+
# result # => { a: 'a', b: 'b', c: 'c' }
|
10
|
+
# result.values.map(&:encoding).all? { Encoding::Windows_31J } # => true
|
11
|
+
#
|
12
|
+
# # Array values
|
13
|
+
# result = %w(a b c).renc(Encoding::Windows_31J)
|
14
|
+
# result # => ['a', 'b', 'c']
|
15
|
+
# result.map(&:encoding).all? { Encoding::Windows_31J } # => true
|
16
|
+
#
|
17
|
+
# # if u define Kernel.renc method.
|
18
|
+
# Kernel.include Renc
|
19
|
+
# Object.include Kernel
|
20
|
+
# # or context `main`
|
21
|
+
# extend Renc
|
4
22
|
module Renc
|
23
|
+
# include #renc method
|
24
|
+
[String, Hash, Array].each { |klass| klass.include self }
|
25
|
+
|
5
26
|
# recurse encoding for Hash and Array.
|
6
27
|
# @param obj [Object]
|
7
28
|
# @param encoding [Encoding]
|
8
29
|
# @return [Object]
|
9
|
-
def renc(
|
30
|
+
def renc(encoding = Encoding::UTF_8, obj = self)
|
31
|
+
# binding.pry
|
10
32
|
case obj
|
11
33
|
when String then obj.encode(encoding)
|
12
|
-
when Hash then
|
13
|
-
when Array then
|
34
|
+
when Hash then renc_hash(obj, encoding)
|
35
|
+
when Array then renc_array(obj, encoding)
|
14
36
|
else obj
|
15
37
|
end
|
16
38
|
end
|
@@ -23,15 +45,16 @@ module Renc
|
|
23
45
|
private
|
24
46
|
|
25
47
|
# recurse encoding for Hash values of String.
|
26
|
-
def
|
48
|
+
def renc_hash(obj, encoding)
|
27
49
|
obj.each_with_object({}) do |args, h|
|
28
50
|
key, val = args
|
29
|
-
h[key] = renc(
|
51
|
+
h[key] = renc(encoding, val)
|
52
|
+
# h[key] = val.renc(encoding)
|
30
53
|
end
|
31
54
|
end
|
32
55
|
|
33
56
|
# recurse encoding for Array values of String.
|
34
|
-
def
|
35
|
-
obj.map { |val| renc(
|
57
|
+
def renc_array(obj, encoding)
|
58
|
+
obj.map { |val| renc(encoding, val) }
|
36
59
|
end
|
37
60
|
end
|
data/lib/renc/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: renc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- k-ta-yamada
|
@@ -148,7 +148,7 @@ files:
|
|
148
148
|
- ".rubocop.yml"
|
149
149
|
- ".rubocop_todo.yml"
|
150
150
|
- ".travis.yml"
|
151
|
-
-
|
151
|
+
- CHANGELOG.md
|
152
152
|
- CODE_OF_CONDUCT.md
|
153
153
|
- Gemfile
|
154
154
|
- LICENSE.txt
|