encoding-kawai 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -4
- data/encoding-kawai.gemspec +1 -1
- data/lib/encoding-kawai.rb +6 -6
- data/lib/encoding-kawai/object.rb +14 -14
- data/spec/encoding-kawai_spec.rb +5 -5
- metadata +4 -4
data/README.md
CHANGED
@@ -3,15 +3,15 @@ EncodingKawai - little sintax sugar for ruby force_encoding, also working on 1.8
|
|
3
3
|
gem 'encoding-kawai'
|
4
4
|
|
5
5
|
```ruby
|
6
|
-
str.
|
7
|
-
str.
|
6
|
+
str.utf8! # str.respond_to?(:force_encoding) ? str.force_encoding('utf-8') : str
|
7
|
+
str.binary! # str.respond_to?(:force_encoding) ? str.force_encoding('binary') : str
|
8
8
|
```
|
9
9
|
|
10
10
|
Extend object.
|
11
11
|
```ruby
|
12
12
|
require 'encoding-kawai/object'
|
13
13
|
|
14
|
-
[str1, str2].
|
15
|
-
{str1 => str2}.
|
14
|
+
[str1, str2].utf8!
|
15
|
+
{str1 => str2}.utf8!
|
16
16
|
...
|
17
17
|
```
|
data/encoding-kawai.gemspec
CHANGED
data/lib/encoding-kawai.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
if RUBY_VERSION < '1.9'
|
2
2
|
|
3
3
|
class String
|
4
|
-
def
|
4
|
+
def utf8!
|
5
5
|
self
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
8
|
+
def binary!
|
9
9
|
self
|
10
10
|
end
|
11
11
|
end
|
@@ -13,11 +13,11 @@ if RUBY_VERSION < '1.9'
|
|
13
13
|
else
|
14
14
|
|
15
15
|
class String
|
16
|
-
def
|
16
|
+
def utf8!
|
17
17
|
self.force_encoding('utf-8')
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
20
|
+
def binary!
|
21
21
|
self.force_encoding('binary')
|
22
22
|
end
|
23
23
|
end
|
@@ -25,11 +25,11 @@ else
|
|
25
25
|
end
|
26
26
|
|
27
27
|
class NilClass
|
28
|
-
def
|
28
|
+
def utf8!
|
29
29
|
self
|
30
30
|
end
|
31
31
|
|
32
|
-
def
|
32
|
+
def binary!
|
33
33
|
self
|
34
34
|
end
|
35
35
|
end
|
@@ -1,44 +1,44 @@
|
|
1
1
|
class Object
|
2
|
-
def
|
2
|
+
def utf8!
|
3
3
|
self
|
4
4
|
end
|
5
5
|
|
6
|
-
def
|
6
|
+
def binary!
|
7
7
|
self
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
if RUBY_VERSION >= '1.9'
|
12
12
|
class Array
|
13
|
-
def
|
14
|
-
map { |a| a.
|
13
|
+
def utf8!
|
14
|
+
map { |a| a.utf8! }
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
18
|
-
map { |a| a.
|
17
|
+
def binary!
|
18
|
+
map { |a| a.binary! }
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
class Regexp
|
23
|
-
def
|
24
|
-
Regexp.new(to_s.
|
23
|
+
def utf8!
|
24
|
+
Regexp.new(to_s.utf8!)
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
28
|
-
Regexp.new(to_s.
|
27
|
+
def binary!
|
28
|
+
Regexp.new(to_s.binary!)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
class Hash
|
33
|
-
def
|
33
|
+
def utf8!
|
34
34
|
{}.tap{|h|
|
35
|
-
keys.each { |k| h[k.frozen? ? k.dup.
|
35
|
+
keys.each { |k| h[k.frozen? ? k.dup.utf8! : k.utf8!] = self[k].utf8! }
|
36
36
|
}
|
37
37
|
end
|
38
38
|
|
39
|
-
def
|
39
|
+
def binary!
|
40
40
|
{}.tap{|h|
|
41
|
-
keys.each { |k| h[k.frozen? ? k.dup.
|
41
|
+
keys.each { |k| h[k.frozen? ? k.dup.binary! : k.binary!] = self[k].binary! }
|
42
42
|
}
|
43
43
|
end
|
44
44
|
end
|
data/spec/encoding-kawai_spec.rb
CHANGED
@@ -11,12 +11,12 @@ describe "encoding-kawai" do
|
|
11
11
|
%w{utf8 binary}.each do |enc|
|
12
12
|
OBJS.each do |obj|
|
13
13
|
it "should word for each object to code with #{enc} on obj: #{obj.inspect}" do
|
14
|
-
obj.send("
|
14
|
+
obj.send("#{enc}!").should == obj
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should word for each object to code with #{enc} on obj: #{H.inspect}" do
|
19
|
-
H.send("
|
19
|
+
H.send("#{enc}!").should == H
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -25,19 +25,19 @@ describe "encoding-kawai" do
|
|
25
25
|
%w{utf8 binary}.each do |enc|
|
26
26
|
OBJS.each do |obj|
|
27
27
|
it "should word for each object to code with #{enc} on obj: #{obj.inspect}" do
|
28
|
-
obj.send("
|
28
|
+
obj.send("#{enc}!").should == obj
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should word for each object to code with #{enc} on obj: #{H.inspect}" do
|
33
33
|
str = enc == 'utf8' ? "бла".force_encoding('utf-8') : "бла".force_encoding('binary')
|
34
|
-
H.send("
|
34
|
+
H.send("#{enc}!").should == {:bla => 'bla', str => 1}
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should set encoding" do
|
38
38
|
str = "бла"
|
39
39
|
str.encoding.name.should == "UTF-8"
|
40
|
-
str.send("
|
40
|
+
str.send("#{enc}!")
|
41
41
|
if enc == 'utf8'
|
42
42
|
str.encoding.name.should == 'UTF-8'
|
43
43
|
else
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: encoding-kawai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -74,7 +74,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
74
74
|
version: '0'
|
75
75
|
segments:
|
76
76
|
- 0
|
77
|
-
hash: -
|
77
|
+
hash: -951121923
|
78
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
79
|
none: false
|
80
80
|
requirements:
|
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
83
|
version: '0'
|
84
84
|
segments:
|
85
85
|
- 0
|
86
|
-
hash: -
|
86
|
+
hash: -951121923
|
87
87
|
requirements: []
|
88
88
|
rubyforge_project:
|
89
89
|
rubygems_version: 1.8.24
|