rmb_chinese_yuan 1.0.0 → 1.1.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: 360863d5103d53600a46d4002b0d018815bce409
4
- data.tar.gz: 7ada7fa235b4c07692c683fdc84b0eccb0225069
3
+ metadata.gz: b59417b31ca04031e25a0beeede5e5f7d42a14cf
4
+ data.tar.gz: fff437f17f0c20dc43e37ecf1a17b2aee75fc20e
5
5
  SHA512:
6
- metadata.gz: 51d1464431aef9acfb5c57eed724b7916c43970fc8d99003c18a011a535b368ee0ca8461eb801c0daf639cc6cb7324e7a38c64999957469aa254bca84329ed90
7
- data.tar.gz: d7ca2d6d2f749a13f02629d5d8e0bf1c126157aa2547782b1b07860c3f91038c1404ac1730af0033f1a0f962570269c4c45f575a39481da86729bc55c2be2b11
6
+ metadata.gz: c2406cc374e5599d3785cf6c83a107258a1b57e9cf446b7c10a6fc1dcf68e2024c180b390f81f04b9e44ac0eb971c4b36879b6f0fc243f0493a5661c990d08f9
7
+ data.tar.gz: 41f319719d0bf7f9f083f0ca3b75d8875d8ce3a86a8ee9bd5a4c2d7e95b935762e7269b65f2701481a4f86d3b74971328d8dcc2b8644f65d8a3dc5989bf50e34
data/.travis.yml CHANGED
@@ -2,3 +2,4 @@ language: ruby
2
2
  rvm:
3
3
  - 2.2.2
4
4
  before_install: gem install bundler -v 1.10.5
5
+ cache: bundler
data/Gemfile CHANGED
@@ -2,3 +2,9 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in rmb.gemspec
4
4
  gemspec
5
+
6
+ gem 'bundler', '~> 1.10'
7
+ gem 'rake', '~> 10.0'
8
+ gem 'minitest'
9
+ gem 'pry-byebug'
10
+ gem 'codeclimate-test-reporter'
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # RMB
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/rmb_chinese_yuan.svg)](https://badge.fury.io/rb/rmb_chinese_yuan)
4
+ [![Build Status](https://travis-ci.org/ifyouseewendy/rmb.svg?branch=master)](https://travis-ci.org/ifyouseewendy/rmb)
5
+ [![Code Climate](https://codeclimate.com/github/ifyouseewendy/rmb/badges/gpa.svg)](https://codeclimate.com/github/ifyouseewendy/rmb)
6
+ [![Test Coverage](https://codeclimate.com/github/ifyouseewendy/rmb/badges/coverage.svg)](https://codeclimate.com/github/ifyouseewendy/rmb/coverage)
7
+
3
8
  RMB is a gem helps you generate money in Chinese Yuan.
4
9
 
5
10
  ## Installation
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "rmb_chinese_yuan"
3
+ require 'bundler/setup'
4
+ require 'rmb_chinese_yuan'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -12,9 +12,9 @@ require "rmb_chinese_yuan"
12
12
 
13
13
 
14
14
  begin
15
- require "pry"
15
+ require 'pry'
16
16
  Pry.start
17
17
  rescue LoadError
18
- require "irb"
18
+ require 'irb'
19
19
  IRB.start
20
20
  end
@@ -1,7 +1,7 @@
1
- require "rmb_chinese_yuan/version"
1
+ require 'rmb_chinese_yuan/version'
2
2
 
3
3
  begin
4
- require "pry"
4
+ require 'pry'
5
5
  rescue LoadError
6
6
  end
7
7
 
@@ -11,7 +11,7 @@ class RMB
11
11
  INTEGER_UNIT = %w(亿 万 元)
12
12
  DECIMAL_UNIT = %w(整 角 分)
13
13
 
14
- attr_reader :integer, :decimal
14
+ attr_reader :integer, :decimal, :parts, :words
15
15
 
16
16
  def initialize(money)
17
17
  @integer, @decimal = preprocess(money)
@@ -27,19 +27,19 @@ class RMB
27
27
  def preprocess(money)
28
28
  money = money.to_s
29
29
 
30
- raise ArgumentError, "Not a valid money<#{money}>" \
30
+ fail ArgumentError, "Not a valid money<#{money}>" \
31
31
  unless money.match(/^[\d|,|\.]+/)
32
32
 
33
- integer, decimal = money.gsub(',', '').split('.')
34
- decimal = decimal ? decimal[0,2] : ''
33
+ integer, decimal = money.delete(',').split('.')
34
+ decimal = decimal ? decimal[0, 2] : ''
35
35
 
36
- raise ArgumentError, "Integer part of money<#{money}> is invalid" \
36
+ fail ArgumentError, "Integer part of money<#{money}> is invalid" \
37
37
  unless integer.match(/^\d*$/)
38
38
 
39
- raise ArgumentError, "Decimal part of money<#{money}> is invalid" \
39
+ fail ArgumentError, "Decimal part of money<#{money}> is invalid" \
40
40
  unless decimal.match(/^\d*$/)
41
41
 
42
- raise ArgumentError, "Integer part of money<#{money}> is longer than 12" \
42
+ fail ArgumentError, "Integer part of money<#{money}> is longer than 12" \
43
43
  if integer.length > 12
44
44
 
45
45
  [integer, decimal]
@@ -52,20 +52,17 @@ class RMB
52
52
  end
53
53
 
54
54
  def convert_decimal
55
- return DECIMAL_UNIT[0] if @decimal.to_i.zero?
55
+ return DECIMAL_UNIT[0] if decimal.to_i.zero?
56
56
 
57
- jiao, fen = @decimal.chars.map(&:to_i)
58
- res = ''
57
+ jiao, fen = split_into_digits(decimal, direction: :tail, count: 2)
59
58
 
60
- if jiao.zero? && fen.to_i.nonzero?
61
- res << NUMBERS[jiao]
62
- res << [ NUMBERS[fen], DECIMAL_UNIT[2] ].join
59
+ if jiao.zero? && fen.nonzero?
60
+ [NUMBERS[0], NUMBERS[fen], DECIMAL_UNIT[2]].join
63
61
  else
64
- res << [ NUMBERS[jiao], DECIMAL_UNIT[1] ].join
65
- res << [ NUMBERS[fen], DECIMAL_UNIT[2] ].join if fen && fen.nonzero?
62
+ res = [NUMBERS[jiao], DECIMAL_UNIT[1]].join
63
+ res << [NUMBERS[fen], DECIMAL_UNIT[2]].join if fen.nonzero?
64
+ res
66
65
  end
67
-
68
- res
69
66
  end
70
67
 
71
68
  def join(integer_words, decimal_words)
@@ -88,19 +85,18 @@ class RMB
88
85
  end
89
86
 
90
87
  def read_into_words
91
- @words = @parts.reduce([]){|ar, part| ar << read_integer(part) }
88
+ @words = parts.reduce([]){ |ar, part| ar << read_integer(part) }
92
89
  end
93
90
 
94
91
  def read_integer(number)
95
92
  return NUMBERS[0] if number.zero?
96
93
 
97
- numbers = number.to_s.chars.map(&:to_i)
98
- numbers.unshift 0 while numbers.count < 4
94
+ numbers = split_into_digits(number)
99
95
 
100
96
  numbers.each_with_index.reduce('') do |str, (n, i)|
101
97
  if n.nonzero?
102
- str << [ NUMBERS[n], PART_UNIT[i] ].join
103
- elsif !str.empty? && i+1 < 4 && numbers[i+1].nonzero?
98
+ str << [NUMBERS[n], PART_UNIT[i]].join
99
+ elsif !str.empty? && i + 1 < 4 && numbers[i+1].nonzero?
104
100
  str << NUMBERS[0]
105
101
  else
106
102
  str
@@ -108,35 +104,51 @@ class RMB
108
104
  end
109
105
  end
110
106
 
107
+ def split_into_digits(number, direction: :head, count: 4)
108
+ digits = number.to_s.chars.map(&:to_i)
109
+ if direction == :head
110
+ digits.unshift 0 while digits.count < count
111
+ else
112
+ digits.push 0 while digits.count < count
113
+ end
114
+ digits
115
+ end
116
+
111
117
  def join_words
112
- res = ''
118
+ res = [part_yi, part_wan, part_ge].join
119
+ res << INTEGER_UNIT[2]
120
+ end
113
121
 
114
- yi, wan, ge = @parts
122
+ def part_yi
123
+ [words[0], INTEGER_UNIT[0]].join unless parts[0].zero?
124
+ end
115
125
 
116
- res << [ @words[0], INTEGER_UNIT[0] ].join if yi.nonzero?
126
+ def part_wan
127
+ yi, wan, ge = parts
117
128
 
118
129
  if wan.zero?
119
130
  if yi.nonzero? && ge.nonzero?
120
- res << NUMBERS[0]
131
+ NUMBERS[0]
121
132
  end
122
133
  else
123
- if yi.nonzero? && (wan/1000).zero?
124
- res << NUMBERS[0]
125
- end
126
- res << [ @words[1], INTEGER_UNIT[1] ].join
134
+ res = [words[1], INTEGER_UNIT[1]]
135
+ res.unshift NUMBERS[0] if yi.nonzero? && (wan / 1000).zero?
136
+ res.join
127
137
  end
138
+ end
139
+
140
+ def part_ge
141
+ yi, wan, ge = parts
128
142
 
129
143
  if ge.zero?
130
144
  if yi.zero? && wan.zero?
131
- res << @words[2]
145
+ words[2]
132
146
  end
133
147
  else
134
- if wan.nonzero? && (ge/1000).zero?
135
- res << NUMBERS[0]
136
- end
137
- res << @words[2]
148
+ res = [words[2]]
149
+ res.unshift NUMBERS[0] if wan.nonzero? && (ge / 1000).zero?
150
+ res.join
138
151
  end
139
152
 
140
- res << INTEGER_UNIT[2]
141
153
  end
142
154
  end
@@ -1,3 +1,3 @@
1
1
  module RmbChineseYuan
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -4,23 +4,20 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'rmb_chinese_yuan/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "rmb_chinese_yuan"
7
+ spec.name = 'rmb_chinese_yuan'
8
8
  spec.version = RmbChineseYuan::VERSION
9
- spec.authors = ["Di Wen"]
10
- spec.email = ["ifyouseewendy@gmail.com"]
9
+ spec.authors = ['Di Wen']
10
+ spec.email = ['ifyouseewendy@gmail.com']
11
11
 
12
12
  spec.summary = %q{RMB is a gem helps you generate money in Chinese Yuan.}
13
13
  spec.description = %q{RMB is a gem helps you generate money in Chinese Yuan.}
14
- spec.homepage = "http://github.com/ifyouseewendy/rmb"
15
- spec.license = "MIT"
14
+ spec.homepage = 'http://github.com/ifyouseewendy/rmb'
15
+ spec.license = 'MIT'
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "exe"
18
+ spec.bindir = 'exe'
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
20
+ spec.require_paths = ['lib']
21
21
 
22
- spec.add_development_dependency "bundler", "~> 1.10"
23
- spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "minitest"
25
- spec.add_development_dependency "pry-byebug"
22
+ spec.required_ruby_version = '>= 2.0.0'
26
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmb_chinese_yuan
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Di Wen
@@ -9,63 +9,7 @@ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
11
  date: 2015-11-14 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.10'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.10'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '10.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '10.0'
41
- - !ruby/object:Gem::Dependency
42
- name: minitest
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: pry-byebug
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
12
+ dependencies: []
69
13
  description: RMB is a gem helps you generate money in Chinese Yuan.
70
14
  email:
71
15
  - ifyouseewendy@gmail.com
@@ -97,7 +41,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
41
  requirements:
98
42
  - - ">="
99
43
  - !ruby/object:Gem::Version
100
- version: '0'
44
+ version: 2.0.0
101
45
  required_rubygems_version: !ruby/object:Gem::Requirement
102
46
  requirements:
103
47
  - - ">="