renc 1.2.0 → 1.3.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: d23dd022e00e54050a4613a1aae4999e49046876
4
- data.tar.gz: 5e0c6ca26059dcfaa1c52ff9f522a5e3bb35362d
3
+ metadata.gz: d3125b37f8be1fbbb5c13ed9b8cfc46a9b194842
4
+ data.tar.gz: e4728c765ba5fa9e0ee91bcf529eea90291582e3
5
5
  SHA512:
6
- metadata.gz: 6bd1436484490cf7f116aad27f3b7eb044292148857f11b6956e7369822f4ec3aa708d2dd9ad100252964ca05c01108723b971eb5c33ac7862ad0f1301e7f703
7
- data.tar.gz: 7424d0ddbcd033c1416a01d34bb39db2dc4a98b222fead380395cc1d9d44f3c89b4897ee9f7610f4db2d919be9fae0de65c9c58597c5a7300ef3cc889076e70e
6
+ metadata.gz: 0fa3a777853a2e7e32df811616bcf2379150dc8537696c0575d24d3ed1208724a858c2891558e8232617073b781387f4046b5a829cb825172ed42bbba70e3f56
7
+ data.tar.gz: 28f87fdcf7561059f7b48580ef1cfa3d38af1ca586a2cfc230a05c6bb8feb85c3d5134893e3728cd2c84e6ceda03277b38925784e63a8afa9da032e71329df63
@@ -1,3 +1,7 @@
1
+ # v1.3.0
2
+ - [issue #7](https://github.com/k-ta-yamada/tee_logger/issues/7)
3
+ configuration default encoding
4
+
1
5
  # v1.2.0
2
6
  - remove `Renc#enc` method
3
7
 
data/README.md CHANGED
@@ -26,20 +26,72 @@ Or install it yourself as:
26
26
 
27
27
  ## Usage
28
28
 
29
+ ### Basic
30
+
29
31
  ```ruby
30
32
  require 'renc'
31
33
 
34
+ # String
32
35
  str_ascii = 'hello renc!'.encode(Encoding::ASCII)
33
- str_ascii.encoding # => #<Encoding::US-ASCII>
34
- Renc.enc(str_ascii, Encoding::UTF_8).encoding # => #<Encoding::UTF-8>
36
+ str_ascii.encoding # => #<Encoding::US-ASCII>
37
+ str_ascii.renc.encoding # => #<Encoding::UTF-8>
38
+ str_ascii == str_ascii.renc # => true
39
+
40
+ # Array
41
+ array_val = [str_ascii]
42
+ array_val.first.encoding # => #<Encoding::US-ASCII>
43
+ array_val.renc.first.encoding # => #<Encoding::UTF-8>
44
+ array_val.renc == array_val # => ture
35
45
 
46
+ # Hash
36
47
  hash_val = { a: str_ascii }
37
- hash_val[:a].encoding # => Encoding::ASCII
38
- Renc.enc(hash_val, Encoding::UTF_8)[:a].encoding # => #<Encoding::UTF-8>
48
+ hash_val[:a].encoding # => #<Encoding::US-ASCII>
49
+ hash_val.renc[:a].encoding # => #<Encoding::UTF-8>
50
+ hash_val.renc == hash_val # => true
39
51
 
40
- array_val = [str_ascii]
41
- array_val.first.encoding # => Encoding::ASCII
42
- Renc.enc(array_val, Encoding::UTF_8).first.encoding # => #<Encoding::UTF-8>
52
+ ```
53
+
54
+ ### Nested Hash, Array, and others
55
+ > @ref [./spec/spec_helper.rb](https://github.com/k-ta-yamada/renc/blob/feature/issue7/spec/spec_helper.rb#L18)
56
+
57
+ ```ruby
58
+ # @ref ./spec/spec_helper.rb#L18
59
+ class Hash
60
+ def values_in_nested_hash
61
+ map { |_k, v| v.is_a?(Hash) ? v.values_in_nested_hash : v }
62
+ end
63
+ end
64
+
65
+ nested = { a: 'a', # encoding target1
66
+ b: { ba: 1,
67
+ bb: [
68
+ 'bb0', # encoding target2
69
+ :bb2,
70
+ 3
71
+ ]
72
+ }
73
+ }
74
+ nested.renc == nested # => true
75
+
76
+ encoded_string_values = nested.values_in_nested_hash
77
+ encoded_string_values.flatten!
78
+ encoded_string_values.select! { |v| v.is_a?(String) } # => ["a", "bb0"]
79
+ encoded_string_values.all? { |v| v.encoding == Encoding::UTF_8 } # => true
80
+ ```
81
+
82
+ ### Configuration
83
+
84
+ ```ruby
85
+ # default configure encoding is utf-8
86
+ Renc.default_encoding # => #<Encoding::UTF-8>
87
+ 'test'.renc.encoding # => #<Encoding::UTF-8>
88
+
89
+ # if you want to change to sjis
90
+ Renc.default_encoding = Encoding::Windows_31J
91
+ 'test'.renc.encoding # => #<Encoding::Windows-31J>
92
+
93
+ # change temporaly
94
+ 'test'.renc(Encoding::ASCII).encoding # => #<Encoding:US-ASCII>
43
95
  ```
44
96
 
45
97
  ## Development
@@ -75,15 +127,11 @@ under the terms of the [MIT License](http://opensource.org/licenses/MIT).
75
127
 
76
128
  [gem_version]: http://badge.fury.io/rb/renc
77
129
  [gem_version-svg]: https://badge.fury.io/rb/renc.svg
78
-
79
130
  [travis]: https://travis-ci.org/k-ta-yamada/renc
80
131
  [travis-svg]: https://travis-ci.org/k-ta-yamada/renc.svg
81
-
82
132
  [codeclimate]: https://codeclimate.com/github/k-ta-yamada/renc
83
133
  [codeclimate-svg]: https://codeclimate.com/github/k-ta-yamada/renc/badges/gpa.svg
84
-
85
134
  [codeclimate_cov]: https://codeclimate.com/github/k-ta-yamada/renc/coverage
86
135
  [codeclimate_cov-svg]: https://codeclimate.com/github/k-ta-yamada/renc/badges/coverage.svg
87
-
88
136
  [inch-ci]: http://inch-ci.org/github/k-ta-yamada/renc
89
137
  [inch-ci-svg]: http://inch-ci.org/github/k-ta-yamada/renc.svg?branch=master
@@ -1,34 +1,60 @@
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
22
4
  module Renc
5
+ # raise unless encoding.is_a?(Encoding)
6
+ class ConfigureError < StandardError; end
7
+
8
+ # this gem's default configured encoding
9
+ DEFAULT_ENCODING = Encoding::UTF_8
10
+
11
+ # for include #renc method
23
12
  TARGET_CLASS = [String, Array, Hash].freeze
24
- # include #renc method
25
13
  TARGET_CLASS.each { |klass| klass.include self }
26
14
 
15
+ class << self
16
+ # return @default_encoding
17
+ # @return [Encoding] @default_encoding
18
+ # @see DEFAULT_ENCODING
19
+ def default_encoding
20
+ @default_encoding ||= DEFAULT_ENCODING
21
+ end
22
+
23
+ # configure default encoding
24
+ # @param encoding [Encoding]
25
+ # @example
26
+ # Renc.default_encoding = 1 # => Renc::ConfigureError
27
+ # Renc.default_encoding = Encoding::ASCII
28
+ def default_encoding=(encoding)
29
+ raise ConfigureError,
30
+ 'Encoding class only allow' unless encoding.is_a?(Encoding)
31
+ @default_encoding = encoding
32
+ end
33
+ end
34
+
27
35
  # recurse encoding for Hash and Array.
28
36
  # @param encoding [Encoding]
29
37
  # @param obj [Object]
30
38
  # @return [Object]
31
- def renc(encoding = Encoding::UTF_8, obj = self)
39
+ # @example
40
+ # default_src_encoding # => #<Encoding:UTF-8>
41
+ #
42
+ # # Hash values
43
+ # result = { a: 'a', b: 'b', c: 'c' }.renc(Encoding::Windows_31J)
44
+ # result # => { a: 'a', b: 'b', c: 'c' }
45
+ # result.values.map(&:encoding).all? { Encoding::Windows_31J } # => true
46
+ #
47
+ # # Array values
48
+ # result = %w(a b c).renc(Encoding::Windows_31J)
49
+ # result # => ['a', 'b', 'c']
50
+ # result.map(&:encoding).all? { Encoding::Windows_31J } # => true
51
+ #
52
+ # # if u define Kernel.renc method.
53
+ # Kernel.include Renc
54
+ # Object.include Kernel
55
+ # # or context `main`
56
+ # extend Renc
57
+ def renc(encoding = Renc.default_encoding, obj = self)
32
58
  case obj
33
59
  when String then obj.encode(encoding)
34
60
  when Array then renc_array(obj, encoding)
@@ -1,3 +1,3 @@
1
1
  module Renc
2
- VERSION = '1.2.0'.freeze
2
+ VERSION = '1.3.0'.freeze
3
3
  end
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: 1.2.0
4
+ version: 1.3.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: 2016-03-02 00:00:00.000000000 Z
11
+ date: 2016-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler