rusprice 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 277b2fb49cab5bb70f20a965e1edfd89d899e24f
4
- data.tar.gz: 18c21fe8b0354a578e4a958e1523aebb5a5af9bf
3
+ metadata.gz: d4450791fc15bf4d40cc9110f279c1ffc43b336f
4
+ data.tar.gz: 0b3695d814808f8dea8a92e5ae0f092a1a1690f5
5
5
  SHA512:
6
- metadata.gz: 39869eab8d29bc3700a8171880b73b8eedebd68b8a6c1b8b98c11881bacca283b8006a4eb215f55af3696497c23801f96509bf21e939221601ec539c0348dbbd
7
- data.tar.gz: e7f162823449c26b01019913da63398cbecb5279da9467a0fdcbce21988bbb09cb36755bf5ed70839c1a9ac2450abb6eb871b719fe21f6fbc73a5e66dcb31adc
6
+ metadata.gz: 95a0465000ed348acbb3ecc7705cdead348284d92d8f8237e1f631fa0c5de583ecfe8ae2c24e31a2d6ab2e95ab3fa0b78951cff93ce77bbcc3f3a00b186a92cf
7
+ data.tar.gz: d9cfe63ed7ac32b8e1ad1a407ecb25012ebe0118ea9b7aa6d3058fbf0dd6f83a28029f1166114c73d8ebe8d11c5ee0506d7cdd3734b56cace3aac5a86e3553f5
data/README.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # RusPrice
2
2
 
3
3
  Гем позволяет преобразовать число, представляющее некую цену в строку вида "5 рублей 10 копеек". Форма слов "рубль" и "копейка" будет изменена согласно правилам русского языка в зависимости от данного числа.
4
+ Модуль с методом "rusprice" добавляется в класс Numeric и может быть вызван на любом числе (см. "Примеры использования").
5
+ ####Примечание:
6
+ - Отрицательное число будет преобразовано в положительное;
7
+ - Дробная часть будет округлена до двух знаков (используется метод "Float#round");
8
+ - Числа 0 и 0.0 преобразуются к строке "0 рублей";
9
+ - Число 0.001 преобразуются к строке "0 рублей";
10
+ - Число 0.005 преобразуются к строке "1 копейка".
4
11
 
5
12
  ## Установка
6
13
 
@@ -18,25 +25,18 @@ gem 'rusprice'
18
25
 
19
26
  $ gem install rusprice
20
27
 
21
- ### Примеры использования
28
+ ## Примеры использования
22
29
  ```ruby
23
- price = RusPrice::Converter.new 123.45
24
- price.russify
30
+ 123.45.rusprice
25
31
  # "123 рубля 45 копеек"
26
32
 
27
- price = RusPrice::Converter.new 98
28
- price.russify
33
+ 98.rusprice
29
34
  # "98 рублей"
30
35
 
31
- price = RusPrice::Converter.new 0.72
32
- price.russify
36
+ 0.72.rusprice
33
37
  # "72 копейки"
34
38
  ```
35
39
 
36
- ## Contributing
40
+ ## Лицензия
37
41
 
38
- Bug reports and pull requests are welcome on GitHub at https://github.com/klekot/rusprice. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
39
-
40
- ## License
41
-
42
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
42
+ Этот гем распространяется по [лицензии MIT](http://opensource.org/licenses/MIT).
data/lib/rusprice.rb CHANGED
@@ -1,65 +1,57 @@
1
- #require "rusprice/version"
1
+ require "rusprice/version"
2
2
 
3
3
  module RusPrice
4
- class Converter
5
- RUBLE = "рубль"
6
- PENNY = 'копейка'
4
+ RUBLE = "рубль"
5
+ PENNY = 'копейка'
7
6
 
8
- attr_accessor :price
9
-
10
- def initialize(price = 0)
11
- begin
12
- raise ArgumentError, "Only Numeric accepted!" unless price.is_a? Numeric
13
- @price = price
14
- rescue => exception
15
- puts exception.message
16
- end
17
- end
18
-
19
- def russify()
20
- @price = @price * (-1) if @price < 0
21
- dec_part = (@price % 1).round(2)
22
- rub_part = (@price - (@price % 1)).to_i
23
- kop_part = (dec_part * 100).to_i
24
- rub_case = cases rub_part, RUBLE
25
- kop_case = cases kop_part, PENNY
26
- unless kop_part == 0
27
- unless rub_part == 0
28
- output = "#{rub_part} #{rub_case} #{kop_part} #{kop_case}"
29
- else
30
- output = "#{kop_part} #{kop_case}"
31
- end
7
+ def rusprice()
8
+ self_value = self
9
+ self_value = self * (-1) if self.negative?
10
+ dec_part = (self_value % 1).round(2)
11
+ rub_part = (self_value - (self_value % 1)).to_i
12
+ kop_part = (dec_part * 100).to_i
13
+ rub_case = cases rub_part, RUBLE
14
+ kop_case = cases kop_part, PENNY
15
+ unless kop_part == 0
16
+ unless rub_part == 0
17
+ output = "#{rub_part} #{rub_case} #{kop_part} #{kop_case}"
32
18
  else
33
- output = "#{rub_part} #{rub_case}"
19
+ output = "#{kop_part} #{kop_case}"
34
20
  end
21
+ else
22
+ output = "#{rub_part} #{rub_case}"
35
23
  end
24
+ end
36
25
 
37
- private
26
+ private
38
27
 
39
- def cases (number, kind)
40
- if kind == 'рубль'
41
- if (number % 100 == 11) || (number % 100 == 12) || (number % 100 == 13) || (number % 100 == 14)
42
- 'рублей'
43
- elsif (number % 10 == 1)
44
- 'рубль'
45
- elsif (number % 10 == 2) || (number % 10 == 3) || (number % 10 == 4)
46
- 'рубля'
47
- else
48
- 'рублей'
49
- end
50
- elsif kind == 'копейка'
51
- if (number == 11) || (number == 12) || (number == 13) || (number == 14)
52
- 'копеек'
53
- elsif (number % 10 == 1)
54
- 'копейка'
55
- elsif (number % 10 == 2) || (number % 10 == 3) || (number % 10 == 4)
56
- 'копейки'
57
- else
58
- 'копеек'
59
- end
28
+ def cases (number, kind)
29
+ if kind == 'рубль'
30
+ if (number % 100 == 11) || (number % 100 == 12) || (number % 100 == 13) || (number % 100 == 14)
31
+ 'рублей'
32
+ elsif (number % 10 == 1)
33
+ 'рубль'
34
+ elsif (number % 10 == 2) || (number % 10 == 3) || (number % 10 == 4)
35
+ 'рубля'
60
36
  else
61
- ''
37
+ 'рублей'
62
38
  end
39
+ elsif kind == 'копейка'
40
+ if (number == 11) || (number == 12) || (number == 13) || (number == 14)
41
+ 'копеек'
42
+ elsif (number % 10 == 1)
43
+ 'копейка'
44
+ elsif (number % 10 == 2) || (number % 10 == 3) || (number % 10 == 4)
45
+ 'копейки'
46
+ else
47
+ 'копеек'
48
+ end
49
+ else
50
+ ''
63
51
  end
64
52
  end
65
53
  end
54
+
55
+ class Numeric
56
+ include RusPrice
57
+ end
@@ -1,3 +1,3 @@
1
1
  module RusPrice
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rusprice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Klekotnev