renc 1.3.1 → 2.0.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: 2961272d2bc8daa940bc880bbfb4016333c56a8d
4
- data.tar.gz: fa2ca88bf5c656d5ed89930a41a9d6a184ad8b46
3
+ metadata.gz: 6d9665ebabc71fd95740388777621dce95bb7b7c
4
+ data.tar.gz: ca951228ca44d73c03df61a4054cc90a3147e55f
5
5
  SHA512:
6
- metadata.gz: '0877c18c27f378076e68127a825f371e8cdad3b001d48ce6897d9eaf880cc085f544cc4b54747c807d35784f3e6da195f36403f69a4f6c7db4a8317d5dcb7a8e'
7
- data.tar.gz: 949a2b453def851195f5e4509a0d45045f6f704243b46227ff0b39c1b1f2e28aeee27a88f0ca7e69575de935c32d94b8b397cbe09e713b099cbe0ddbe99acf57
6
+ metadata.gz: 95ec060b743f14a0585dacc96222fe8a77a68bce26d7931868ece0c8140192972f820ad3ff326d87a41136d4949eb093ffaa126c9a021dad2d995b6f2b042eaa
7
+ data.tar.gz: 58cfba8cbf227d6230bc32e0dd5b62a65103ea2ce97a9ef377a3782006ad7256affe5733bc13ed2f32c86e6e232e5e3b23ac0449f7c72f5596e6a2e7c2c6a427
@@ -12,3 +12,11 @@ AllCops:
12
12
 
13
13
  Style/AsciiComments:
14
14
  Enabled: false
15
+
16
+ Metrics/BlockLength:
17
+ Exclude:
18
+ - spec/**/*
19
+
20
+ Metrics/LineLength:
21
+ Exclude:
22
+ - spec/**/*
@@ -9,3 +9,5 @@ rvm:
9
9
  before_install:
10
10
  - gem update --system --no-doc
11
11
  - gem install bundler
12
+ after_success:
13
+ - bundle exec codeclimate-test-reporter
@@ -1,3 +1,12 @@
1
+ # v2.0.0
2
+ - [issue #10](https://github.com/k-ta-yamada/renc/issues/10)
3
+ Changing default_encoding by os #10
4
+ - change default encoding is `Encoding.default_external`
5
+ - [issue #18](https://github.com/k-ta-yamada/renc/issues/18)
6
+ Encoding::UndefinedConversionError #18
7
+ - change `#renc`'s arguments is change.
8
+ #renc(encoding, `obj`) => #renc(encoding, `options`)
9
+
1
10
  # v1.3.1
2
11
  - [issue #14](https://github.com/k-ta-yamada/renc/issues/14)
3
12
  String.include is NoMethodError on Ruby 2.0.0
data/README.md CHANGED
@@ -8,6 +8,7 @@
8
8
 
9
9
  recurse encoding for Hash and Array.
10
10
 
11
+
11
12
  ## Installation
12
13
 
13
14
  Add this line to your application's Gemfile:
@@ -24,6 +25,7 @@ Or install it yourself as:
24
25
 
25
26
  $ gem install renc
26
27
 
28
+
27
29
  ## Usage
28
30
 
29
31
  ### Basic
@@ -48,13 +50,25 @@ hash_val = { a: str_ascii }
48
50
  hash_val[:a].encoding # => #<Encoding::US-ASCII>
49
51
  hash_val.renc[:a].encoding # => #<Encoding::UTF-8>
50
52
  hash_val.renc == hash_val # => true
51
-
52
53
  ```
53
54
 
54
55
  ### Nested Hash, Array, and others
55
56
  > @ref [./spec/spec_helper.rb](https://github.com/k-ta-yamada/renc/blob/master/spec/spec_helper.rb#L18)
56
57
 
57
58
  ```ruby
59
+ nested = {
60
+ a: 'a', # encoding target1
61
+ b: {
62
+ ba: 1,
63
+ bb: [
64
+ 'bb0', # encoding target2
65
+ :bb2,
66
+ 3
67
+ ]
68
+ }
69
+ }
70
+ nested.renc == nested # => true
71
+
58
72
  # @ref ./spec/spec_helper.rb#L18
59
73
  class Hash
60
74
  def values_in_nested_hash
@@ -62,38 +76,46 @@ class Hash
62
76
  end
63
77
  end
64
78
 
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!
79
+ encoded_string_values = nested.values_in_nested_hash # => ["a", [1, ["bb0", :bb2, 3]]]
80
+ encoded_string_values.flatten! # => ["a", 1, "bb0", :bb2, 3]
78
81
  encoded_string_values.select! { |v| v.is_a?(String) } # => ["a", "bb0"]
79
82
  encoded_string_values.all? { |v| v.encoding == Encoding::UTF_8 } # => true
80
83
  ```
81
84
 
82
85
  ### Configuration
83
86
 
87
+ #### Renc.default_encoding
88
+
84
89
  ```ruby
85
- # default configure encoding is utf-8
90
+ # default configure encoding is Encoding.default_external
91
+ Encoding.default_external # => #<Encoding::UTF-8>
86
92
  Renc.default_encoding # => #<Encoding::UTF-8>
87
93
  'test'.renc.encoding # => #<Encoding::UTF-8>
88
94
 
89
- # if you want to change to sjis
90
- Renc.default_encoding = Encoding::Windows_31J
91
- 'test'.renc.encoding # => #<Encoding::Windows-31J>
95
+ # if you want to change to ascii
96
+ Renc.default_encoding = Encoding::ASCII
97
+ 'test'.renc.encoding # => #<Encoding::ASCII>
92
98
 
93
99
  # change temporaly
94
100
  'test'.renc(Encoding::ASCII).encoding # => #<Encoding:US-ASCII>
95
101
  ```
96
102
 
103
+ #### Renc.default_options
104
+
105
+ ```ruby
106
+ # default configure options is { undef: :replace }
107
+ Renc.default_options # => { undef: :replace }
108
+ '🐘'.renc # => '?'
109
+
110
+ # if you want to change to ascii
111
+ Renc.default_options = { undef: nil }
112
+ '🐘'.renc # => Encoding::UndefinedConversionError: U+1F418 from UTF-8 to US-ASCII
113
+
114
+ # change temporaly
115
+ '🐘'.renc(Encoding::ASCII, undef: nil).encoding # => Encoding::UndefinedConversionError: U+1F418 from UTF-8 to US-ASCII
116
+ ```
117
+
118
+
97
119
  ## Development
98
120
 
99
121
  After checking out the repo, run `bin/setup` to install dependencies.
@@ -108,6 +130,7 @@ which will create a git tag for the version,
108
130
  push git commits and tags,
109
131
  and push the `.gem` file to [rubygems.org](https://rubygems.org).
110
132
 
133
+
111
134
  ## Contributing
112
135
 
113
136
  Bug reports and pull requests are welcome on
@@ -1,17 +1,25 @@
1
1
  require 'renc/version'
2
2
 
3
3
  # recurse encoding for Hash and Array.
4
+ # @example
5
+ # require 'renc'
6
+ #
7
+ # # or context `main`
8
+ # extend Renc
9
+ # @see #renc
4
10
  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
11
  # for include #renc method
12
12
  TARGET_CLASS = [String, Array, Hash].freeze
13
13
  TARGET_CLASS.each { |klass| klass.send(:include, self) }
14
14
 
15
+ # this gem's default configured encoding
16
+ # @see Encoding.default_external
17
+ DEFAULT_ENCODING = Encoding.default_external
18
+
19
+ # this gem's default options for String#encode
20
+ # @see String#encode
21
+ DEFAULT_OPTIONS = { undef: :replace }.freeze
22
+
15
23
  class << self
16
24
  # return @default_encoding
17
25
  # @return [Encoding] @default_encoding
@@ -21,60 +29,85 @@ module Renc
21
29
  end
22
30
 
23
31
  # configure default encoding
24
- # @param encoding [Encoding]
25
32
  # @example
26
33
  # Renc.default_encoding = 1 # => Renc::ConfigureError
27
34
  # Renc.default_encoding = Encoding::ASCII
35
+ # @param encoding [Encoding]
28
36
  def default_encoding=(encoding)
29
- raise ConfigureError,
30
- 'Encoding class only allow' unless encoding.is_a?(Encoding)
37
+ raise TypeError unless encoding.is_a?(Encoding)
31
38
  @default_encoding = encoding
32
39
  end
40
+
41
+ # return @default_options
42
+ # @return [Encoding] @default_options
43
+ # @see DEFAULT_OPTIONS
44
+ def default_options
45
+ @default_options ||= DEFAULT_OPTIONS
46
+ end
47
+
48
+ # configure default options
49
+ # @example
50
+ # Renc.default_options = 1 # => Renc::ConfigureError
51
+ # Renc.default_options = { undef: nil }
52
+ # @param options [Hash]
53
+ def default_options=(options)
54
+ raise TypeError unless options.is_a?(Hash)
55
+ @default_options = options
56
+ end
33
57
  end
34
58
 
35
59
  # recurse encoding for Hash and Array.
36
- # @param encoding [Encoding]
37
- # @param obj [Object]
38
- # @return [Object]
39
60
  # @example
61
+ # # for example
40
62
  # default_src_encoding # => #<Encoding:UTF-8>
41
63
  #
42
64
  # # Hash values
43
- # result = { a: 'a', b: 'b', c: 'c' }.renc(Encoding::Windows_31J)
65
+ # result = { a: 'a', b: 'b', c: 'c' }.renc(Encoding::ASCII)
44
66
  # result # => { a: 'a', b: 'b', c: 'c' }
45
- # result.values.map(&:encoding).all? { Encoding::Windows_31J } # => true
67
+ # result.values.map(&:encoding).all? { Encoding::ASCII } # => true
46
68
  #
47
69
  # # Array values
48
- # result = %w(a b c).renc(Encoding::Windows_31J)
70
+ # result = %w(a b c).renc(Encoding::ASCII)
49
71
  # result # => ['a', 'b', 'c']
50
- # result.map(&:encoding).all? { Encoding::Windows_31J } # => true
72
+ # result.map(&:encoding).all? { Encoding::ASCII } # => true
51
73
  #
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)
58
- case obj
59
- when String then obj.encode(encoding)
60
- when Array then renc_array(obj, encoding)
61
- when Hash then renc_hash(obj, encoding)
62
- else obj
63
- end
74
+ # # with configure default_encoding
75
+ # Renc.default_encoding = Encoding::ASCII
76
+ # result = 'hello'.renc
77
+ # result # => 'hello'
78
+ # result.encoding # => Encoding::ASCII
79
+ # @param encoding [Encoding]
80
+ # @param options [Hash]
81
+ # @return [Object]
82
+ # @see .default_encoding
83
+ # @see .default_options
84
+ def renc(encoding = Renc.default_encoding, options = Renc.default_options)
85
+ raise TypeError unless encoding.is_a?(Encoding)
86
+ raise TypeError unless options.is_a?(Hash)
87
+
88
+ renc_internal(self, encoding, options)
64
89
  end
65
90
 
66
91
  private
67
92
 
93
+ def renc_internal(obj, encoding, options)
94
+ case obj
95
+ when Hash then renc_hash(obj, encoding, options)
96
+ when Array then renc_array(obj, encoding, options)
97
+ when String then obj.encode(encoding, options)
98
+ else obj
99
+ end
100
+ end
101
+
68
102
  # recurse encoding for Hash values of String.
69
- def renc_hash(obj, encoding)
70
- obj.each_with_object({}) do |args, h|
71
- key, val = args
72
- h[key] = renc(encoding, val)
103
+ def renc_hash(obj, encoding, options)
104
+ obj.each_with_object({}) do |(key, val), h|
105
+ h[key] = renc_internal(val, encoding, options)
73
106
  end
74
107
  end
75
108
 
76
109
  # recurse encoding for Array values of String.
77
- def renc_array(obj, encoding)
78
- obj.map { |val| renc(encoding, val) }
110
+ def renc_array(obj, encoding, options)
111
+ obj.map { |val| renc_internal(val, encoding, options) }
79
112
  end
80
113
  end
@@ -1,3 +1,3 @@
1
1
  module Renc
2
- VERSION = '1.3.1'.freeze
2
+ VERSION = '2.0.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.3.1
4
+ version: 2.0.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-06 00:00:00.000000000 Z
11
+ date: 2017-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler