parsi-localize 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown CHANGED
@@ -6,14 +6,25 @@ This gem contains two sections
6
6
 
7
7
  ParsiDigits
8
8
  -----------
9
- Simply change digits in a string/integer/float to unicode parsi digits.
10
-
11
- Example:
9
+ Simply change digits in a string/integer/float to unicode parsi digits:
12
10
 
13
11
  require 'parsi_digits'
14
- ‪"15,000 تومان".with_parsi_digits => ‫"۱۵,۰۰۰ تومان"
15
- 123.25.with_parsi_digits => "۱۲۳/۲۵"
12
+ ‪"15,000 تومان".with_parsi_digits
13
+ => "۱۵,۰۰۰ تومان"
14
+ 123.25.with_parsi_digits
15
+ => "۱۲۳/۲۵"
16
+
17
+ It also dose the reverse action:
18
+
19
+ "۱۲۳۴۵".with_western_digits
20
+ => "12345"
21
+
22
+ And it undersanad parsi digits (which is useful especially for input forms):
16
23
 
24
+ "۱۲۳۴۵".to_i
25
+ => 12345
26
+ "۱۹/۸".to_f
27
+ => 19.8
17
28
 
18
29
  ParsiLocalize
19
30
  -------------
data/lib/parsi_digits.rb CHANGED
@@ -1,14 +1,58 @@
1
1
  # encoding: utf-8
2
2
 
3
+ module Parsi
4
+ module Digits
5
+ PARSI_DIGITS = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
6
+
7
+ class << self
8
+ def to_parsi digit
9
+ PARSI_DIGITS[digit.to_i] if digit =~ /[0-9]/
10
+ end
11
+
12
+ def to_western digit
13
+ PARSI_DIGITS.index digit if digit =~ /[۰-۹]/
14
+ end
15
+ end
16
+ end
17
+ end
18
+
3
19
  class String
4
20
  def with_parsi_digits
5
- digits = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
6
- gsub(/\d/) { |d| digits[d.to_i] }
21
+ gsub(/\d/) { |d| Parsi::Digits.to_parsi d }
7
22
  end
8
23
 
9
24
  def with_parsi_digits!
10
- digits = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
11
- gsub!(/\d/) { |d| digits[d.to_i] }
25
+ gsub!(/\d/) { |d| Parsi::Digits.to_parsi d }
26
+ end
27
+
28
+ def with_western_digits
29
+ gsub(/[۰-۹]/) { |d| Parsi::Digits.to_western d }
30
+ end
31
+
32
+ def with_western_digits!
33
+ gsub!(/[۰-۹]/) { |d| Parsi::Digits.to_western d }
34
+ end
35
+
36
+ def has_parsi_digits?
37
+ self =~ /[۰-۹]/
38
+ end
39
+
40
+ alias_method :western_to_i, :to_i
41
+ def to_i base=10
42
+ if has_parsi_digits?
43
+ with_western_digits.western_to_i base
44
+ else
45
+ western_to_i base
46
+ end
47
+ end
48
+
49
+ alias_method :western_to_f, :to_f
50
+ def to_f
51
+ if has_parsi_digits?
52
+ with_western_digits.sub("/",".").western_to_f
53
+ else
54
+ western_to_f
55
+ end
12
56
  end
13
57
  end
14
58
 
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module ParsiLocalize
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
@@ -7,6 +7,20 @@ class ParsiDigitsTest < Test::Unit::TestCase
7
7
  assert_equal "۹۸۷۶۵۴۳۲۱۰", "9876543210".with_parsi_digits
8
8
  end
9
9
 
10
+ def test_string_with_western_digits
11
+ assert_equal "9876543210", "۹۸۷۶۵۴۳۲۱۰".with_western_digits
12
+ end
13
+
14
+ def test_parsi_string_to_i
15
+ assert_equal 9876543210, "۹۸۷۶۵۴۳۲۱۰".to_i
16
+ assert_equal 9876543210, "9876543210".to_i
17
+ end
18
+
19
+ def test_parsi_string_to_f
20
+ assert_equal 0.987654321, "۰/۹۸۷۶۵۴۳۲۱".to_f
21
+ assert_equal 0.987654321, "0.987654321".to_f
22
+ end
23
+
10
24
  def test_integer_with_parsi_digits
11
25
  assert_equal "۹۸۷۶۵۴۳۲۱۰", 9876543210.with_parsi_digits
12
26
  end
@@ -15,25 +29,10 @@ class ParsiDigitsTest < Test::Unit::TestCase
15
29
  assert_equal "۹/۰۸۷۶۵۴۳۲۱", 9.087654321.with_parsi_digits
16
30
  end
17
31
 
18
- def test_localize_numbers
19
- assert_equal "۹۸۷۶۵۴۳۲۱۰", I18n.l("9876543210")
20
- assert_equal "۹۸۷۶۵۴۳۲۱۰", I18n.l(9876543210)
21
- assert_equal "۹/۰۸۷۶۵۴۳۲۱", I18n.l(9.087654321)
22
- end
23
-
24
- def test_localize_date_format
25
- date = Date.new(2012, 2, 5)
26
- assert_equal "۹۰/۱۱/۱۶", I18n.l(date)
27
- assert_equal "۹۰/۱۱/۱۶", I18n.l(date, format: :default)
28
- assert_equal "۱۶ بهمن", I18n.l(date, format: :short)
29
- assert_equal "یک‌شنبه، ۱۶ بهمن ۱۳۹۰", I18n.l(date, format: :long)
30
- end
31
-
32
- def test_localize_time_format
33
- time = Time.new(2012, 2, 5, 15, 43, 30)
34
- assert_equal "۹۰/۱۱/۱۶ ۱۵:۴۳:۳۰", I18n.l(time)
35
- assert_equal "۹۰/۱۱/۱۶ ۱۵:۴۳:۳۰", I18n.l(time, format: :default)
36
- assert_equal "۱۶ بهمن، ۱۵:۴۳", I18n.l(time, format: :short)
37
- assert_equal "یک‌شنبه، ۱۶ بهمن ۱۳۹۰، ساعت ۱۵:۴۳:۳۰ (IRST)", I18n.l(time, format: :long)
32
+ def test_has_parsi_digits
33
+ assert "۱۹۴".has_parsi_digits?
34
+ assert "test۹۸".has_parsi_digits?
35
+ assert !"123".has_parsi_digits?
36
+ assert !"test".has_parsi_digits?
38
37
  end
39
38
  end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test_helper'
4
+
5
+ class ParsiLocalizeTest < Test::Unit::TestCase
6
+ def test_localize_numbers
7
+ assert_equal "۹۸۷۶۵۴۳۲۱۰", I18n.l("9876543210")
8
+ assert_equal "۹۸۷۶۵۴۳۲۱۰", I18n.l(9876543210)
9
+ assert_equal "۹/۰۸۷۶۵۴۳۲۱", I18n.l(9.087654321)
10
+ end
11
+
12
+ def test_localize_date_format
13
+ date = Date.new(2012, 2, 5)
14
+ assert_equal "۹۰/۱۱/۱۶", I18n.l(date)
15
+ assert_equal "۹۰/۱۱/۱۶", I18n.l(date, format: :default)
16
+ assert_equal "۱۶ بهمن", I18n.l(date, format: :short)
17
+ assert_equal "یک‌شنبه، ۱۶ بهمن ۱۳۹۰", I18n.l(date, format: :long)
18
+ end
19
+
20
+ def test_localize_time_format
21
+ time = Time.new(2012, 2, 5, 15, 43, 30)
22
+ assert_equal "۹۰/۱۱/۱۶ ۱۵:۴۳:۳۰", I18n.l(time)
23
+ assert_equal "۹۰/۱۱/۱۶ ۱۵:۴۳:۳۰", I18n.l(time, format: :default)
24
+ assert_equal "۱۶ بهمن، ۱۵:۴۳", I18n.l(time, format: :short)
25
+ assert_equal "یک‌شنبه، ۱۶ بهمن ۱۳۹۰، ساعت ۱۵:۴۳:۳۰ (IRST)", I18n.l(time, format: :long)
26
+ end
27
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parsi-localize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-17 00:00:00.000000000 Z
12
+ date: 2012-01-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &16268080 !ruby/object:Gem::Requirement
16
+ requirement: &16605020 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *16268080
24
+ version_requirements: *16605020
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &16267660 !ruby/object:Gem::Requirement
27
+ requirement: &16604600 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *16267660
35
+ version_requirements: *16604600
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: i18n
38
- requirement: &16267240 !ruby/object:Gem::Requirement
38
+ requirement: &16604180 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *16267240
46
+ version_requirements: *16604180
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jalalidate
49
- requirement: &16266820 !ruby/object:Gem::Requirement
49
+ requirement: &16603760 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *16266820
57
+ version_requirements: *16603760
58
58
  description: Change I18n localize to use parsi digits and jalaly dates in farsi locale
59
59
  email: hsn.zamani@gmail.com
60
60
  executables: []
@@ -73,6 +73,7 @@ files:
73
73
  - locale/fa.yml
74
74
  - parsi-localize.gemspec
75
75
  - test/parsi_digits_test.rb
76
+ - test/parsi_localize_test.rb
76
77
  - test/test_helper.rb
77
78
  homepage: http://github.com/hzamani/parsi_localize
78
79
  licenses: []
@@ -100,4 +101,5 @@ specification_version: 3
100
101
  summary: Change I18n localize to use parsi digits and jalaly dates in farsi locale
101
102
  test_files:
102
103
  - test/parsi_digits_test.rb
104
+ - test/parsi_localize_test.rb
103
105
  - test/test_helper.rb