parsi-digits 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in .gemspec
4
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,23 @@
1
+ ParsiDigits
2
+ ===========
3
+
4
+ Some simple utilities to help working with parsi unicode digits.
5
+
6
+ Convertion between parsi and western digits:
7
+
8
+ require 'parsi_digits'
9
+ ‪"15,000 تومان".with_parsi_digits
10
+ => ‫"۱۵,۰۰۰ تومان"
11
+ 123.25.with_parsi_digits
12
+ => "۱۲۳/۲۵"
13
+ "۱۲۳۴۵".with_western_digits
14
+ => "12345"
15
+
16
+ Easy convertion of parsi digits to Float or Integer (which is useful especially for input forms):
17
+
18
+ "۱۲۳۴۵".to_i
19
+ => 12345
20
+ "۱۹/۸".to_f
21
+ => 19.8
22
+
23
+ Copyright (c) 2012 Hassan Zamani, released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ task default: :test
5
+
6
+ desc 'Test the parsi-localize plugin.'
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'lib'
9
+ t.libs << 'test'
10
+ t.pattern = 'test/**/*_test.rb'
11
+ t.verbose = true
12
+ end
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+
3
+ module ParsiDigits
4
+ PARSI_DIGITS = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
5
+
6
+ class << self
7
+ def to_parsi digit
8
+ PARSI_DIGITS[digit.to_i] if digit =~ /[0-9]/
9
+ end
10
+
11
+ def to_western digit
12
+ PARSI_DIGITS.index digit if digit =~ /[۰-۹]/
13
+ end
14
+ end
15
+ end
16
+
17
+ class String
18
+ def with_parsi_digits
19
+ gsub(/\d/) { |d| ParsiDigits.to_parsi d }
20
+ end
21
+
22
+ def with_parsi_digits!
23
+ gsub!(/\d/) { |d| ParsiDigits.to_parsi d }
24
+ end
25
+
26
+ def with_western_digits
27
+ gsub(/[۰-۹]/) { |d| ParsiDigits.to_western d }
28
+ end
29
+
30
+ def with_western_digits!
31
+ gsub!(/[۰-۹]/) { |d| ParsiDigits.to_western d }
32
+ end
33
+
34
+ def has_parsi_digits?
35
+ self =~ /[۰-۹]/
36
+ end
37
+
38
+ alias_method :western_to_i, :to_i
39
+ def to_i base=10
40
+ if has_parsi_digits?
41
+ with_western_digits.western_to_i base
42
+ else
43
+ western_to_i base
44
+ end
45
+ end
46
+
47
+ alias_method :western_to_f, :to_f
48
+ def to_f
49
+ if has_parsi_digits?
50
+ with_western_digits.sub("/",".").western_to_f
51
+ else
52
+ western_to_f
53
+ end
54
+ end
55
+ end
56
+
57
+ class Integer
58
+ def with_parsi_digits
59
+ to_s.with_parsi_digits
60
+ end
61
+ end
62
+
63
+ class Float
64
+ def with_parsi_digits
65
+ to_s.with_parsi_digits.sub '.', '/'
66
+ end
67
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module ParsiDigits
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'version'
3
+
4
+ Gem::Specification.new do |s|
5
+
6
+ # Description Meta...
7
+ s.name = 'parsi-digits'
8
+ s.version = ParsiDigits::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.author = 'Hassan Zamani'
11
+ s.email = 'hsn.zamani@gmail.com'
12
+ s.homepage = 'http://github.com/hzamani/parsi_digits'
13
+ s.summary = 'Some utils to support parsi digits'
14
+ s.description = 'Some utils to support parsi digits including convertion between parsi and western digits and convertion from String to Float or Fixnum'
15
+
16
+
17
+ # Load Paths...
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ['lib']
22
+
23
+
24
+ # Dependencies (installed via 'bundle install')...
25
+ s.add_development_dependency("bundler")
26
+ s.add_development_dependency("rake")
27
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test_helper'
4
+
5
+ class ParsiDigitsTest < Test::Unit::TestCase
6
+ def test_string_with_parsi_digits
7
+ assert_equal "۹۸۷۶۵۴۳۲۱۰", "9876543210".with_parsi_digits
8
+ end
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
+
24
+ def test_integer_with_parsi_digits
25
+ assert_equal "۹۸۷۶۵۴۳۲۱۰", 9876543210.with_parsi_digits
26
+ end
27
+
28
+ def test_float_with_parsi_digits
29
+ assert_equal "۹/۰۸۷۶۵۴۳۲۱", 9.087654321.with_parsi_digits
30
+ end
31
+
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?
37
+ end
38
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require 'parsi_digits'
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parsi-digits
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hassan Zamani
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &19273340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *19273340
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &19272920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *19272920
36
+ description: Some utils to support parsi digits including convertion between parsi
37
+ and western digits and convertion from String to Float or Fixnum
38
+ email: hsn.zamani@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - README.markdown
46
+ - Rakefile
47
+ - lib/parsi_digits.rb
48
+ - lib/version.rb
49
+ - parsi-digits.gemspec
50
+ - test/parsi_digits_test.rb
51
+ - test/test_helper.rb
52
+ homepage: http://github.com/hzamani/parsi_digits
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.10
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Some utils to support parsi digits
76
+ test_files:
77
+ - test/parsi_digits_test.rb
78
+ - test/test_helper.rb