renc 2.1.0 → 2.2.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: b87be5d18214f6f0241ba83689760173435cd375
4
- data.tar.gz: 7473827a7e4e0d4a383ed28fa148b820978dc96f
3
+ metadata.gz: 7f70f4e782058204a07038072f5f044f6c70a6ac
4
+ data.tar.gz: c8b8331054b0c93b40fca1fa9d7db3d27c0e4810
5
5
  SHA512:
6
- metadata.gz: ca57295a0c03eb1e6cc5b08cb96908c9d3a5e73654c38ee90ff2315aa958f497d86ff8c56519abd1690b12c809248648f35d227a6dce009d5661f19df69e6f83
7
- data.tar.gz: 6c47aac8531825d80c21c4c64ec2798a57b44ccb85b7e1093bfc20155bd09a8209d7fff2787cc384c2838c8de8528e7abf27f07ca54dd89b348f74976df08538
6
+ metadata.gz: 0c5f4a131d471a306f3f1cf58cef2eee70e5df70bbda6c3c46b2de09ab67e5299d8cc1348fdb9ec0ecab155364ada431d64ad5e0e369bc0917fadcb13f55aa67
7
+ data.tar.gz: d632b298ec50c4c6ca6e693b0d50cb07df1acf9603102e55c0a6e0c6835a10616be4cb696ab5d8754652597d2d11125ca4b7f4a222e5937784efcbe89102d51e
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /tmp/
10
10
 
11
11
  /vendor
12
+ /bin
@@ -9,6 +9,7 @@ AllCops:
9
9
  Exclude:
10
10
  - renc.gemspec
11
11
  - vendor/**/*
12
+ - bin/**/*
12
13
 
13
14
  Style/AsciiComments:
14
15
  Enabled: false
@@ -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, Array, and others
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 ascii
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
 
@@ -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
- renc_internal(self, encoding, options)
47
+ @encoding = encoding
48
+ @options = options
49
+
50
+ _renc(self)
48
51
  end
49
52
 
50
53
  private
51
54
 
52
- def renc_internal(obj, encoding, options)
55
+ def _renc(obj)
53
56
  case obj
54
- when Hash then renc_hash(obj, encoding, options)
55
- when Array then renc_array(obj, encoding, options)
56
- when String then obj.encode(encoding, options)
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 renc_hash(obj, encoding, options)
63
- obj.each_with_object({}) do |(key, val), h|
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 renc_array(obj, encoding, options)
70
- obj.map { |val| renc_internal(val, encoding, options) }
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
@@ -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 # => Renc::ConfigureError
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 # => Renc::ConfigureError
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
@@ -1,3 +1,3 @@
1
1
  module Renc
2
- VERSION = '2.1.0'.freeze
2
+ VERSION = '2.2.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: 2.1.0
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-02-19 00:00:00.000000000 Z
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.8
181
+ rubygems_version: 2.6.11
184
182
  signing_key:
185
183
  specification_version: 4
186
184
  summary: recursive encode for Hash and Array.
@@ -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
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here