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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c42bc4c16709bca6d4f1a198045fa91e538482dc
4
- data.tar.gz: e4b69739039e38f7f549d3fcdab970f05a4b6029
3
+ metadata.gz: bb71daa147a8f421b6596731e638044fa7dae64f
4
+ data.tar.gz: d01f8c0d216baaf810601d557280af7db92f1aed
5
5
  SHA512:
6
- metadata.gz: ce09c021db1adb9f7a25b0920351f27e03dc82edda91551de606cd8f6e5f36f4d1506173a415e4ed04b6f7599896ad612e3d666d4ca848175f27fab6ed00e72b
7
- data.tar.gz: 0ded2abe46c1c8370be06868aa7da87ad88d588561c69c514560f79275a527977e8be7cf4fbc7db85baaf4f0f2826598b7046e56810b427f4704b7ed9f992ca3
6
+ metadata.gz: 837078d4f5c516c5efa4d4d61340c70c46a7deae5dbed5491bc9801dd3260e2c860564f2ae3a63d568cbc0cd2057cfa227d05a83c47a76d90b0eae54233a253b
7
+ data.tar.gz: b314a50cfbeb37858b7b8dbb7b3854cfbcc9f774ac7ef2acc9b30954a566609b41c126f47af815c0b7f17788bb2675d6db64c43f5ad3bf679cd56df81524ee44
@@ -1,3 +1,7 @@
1
+ # v1.1.0
2
+ - [issue #4](https://github.com/k-ta-yamada/tee_logger/issues/4)
3
+ #renc for Hash and Array
4
+
1
5
  # v1.0.1
2
6
  - required Ruby version is change to over 2.0.0.
3
7
  - gemspec: `spec.required_ruby_version = '>= 2.0.0'`
data/lib/renc.rb CHANGED
@@ -1,16 +1,38 @@
1
1
  require 'renc/version'
2
2
 
3
- # namespace
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(obj, encoding = Encoding::UTF_8)
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 enc_hash(obj, encoding)
13
- when Array then enc_array(obj, encoding)
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 enc_hash(obj, encoding)
48
+ def renc_hash(obj, encoding)
27
49
  obj.each_with_object({}) do |args, h|
28
50
  key, val = args
29
- h[key] = renc(val, encoding)
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 enc_array(obj, encoding)
35
- obj.map { |val| renc(val, encoding) }
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
@@ -1,3 +1,3 @@
1
1
  module Renc
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
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.1
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
- - CAHNGELOG.md
151
+ - CHANGELOG.md
152
152
  - CODE_OF_CONDUCT.md
153
153
  - Gemfile
154
154
  - LICENSE.txt