numbers_and_words 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +15 -0
  2. data/README.rdoc +90 -1
  3. data/lib/numbers_and_words.rb +4 -3
  4. data/lib/numbers_and_words/core_ext.rb +1 -0
  5. data/lib/numbers_and_words/core_ext/array.rb +1 -1
  6. data/lib/numbers_and_words/core_ext/float.rb +5 -0
  7. data/lib/numbers_and_words/core_ext/integer.rb +2 -9
  8. data/lib/numbers_and_words/helper_classes.rb +2 -0
  9. data/lib/numbers_and_words/helper_classes/array_extensions/helpers.rb +81 -0
  10. data/lib/numbers_and_words/helper_classes/array_extensions/validations.rb +19 -0
  11. data/lib/numbers_and_words/helper_classes/figures_array.rb +18 -0
  12. data/lib/numbers_and_words/helper_classes/words_array.rb +11 -0
  13. data/lib/numbers_and_words/i18n/locales/numbers.en.yml +48 -0
  14. data/lib/numbers_and_words/i18n/locales/numbers.es.yml +15 -0
  15. data/lib/numbers_and_words/i18n/locales/numbers.hu.yml +16 -0
  16. data/lib/numbers_and_words/i18n/locales/numbers.ru.yml +27 -3
  17. data/lib/numbers_and_words/i18n/locales/numbers.se.yml +7 -0
  18. data/lib/numbers_and_words/i18n/locales/numbers.ua.yml +14 -2
  19. data/lib/numbers_and_words/i18n/pluralization.rb +1 -0
  20. data/lib/numbers_and_words/i18n/plurals/es.rb +11 -0
  21. data/lib/numbers_and_words/strategies.rb +16 -11
  22. data/lib/numbers_and_words/strategies/array_joiner.rb +31 -0
  23. data/lib/numbers_and_words/strategies/figures_converter.rb +33 -0
  24. data/lib/numbers_and_words/strategies/figures_converter/decorators.rb +39 -0
  25. data/lib/numbers_and_words/strategies/figures_converter/decorators/base.rb +18 -0
  26. data/lib/numbers_and_words/strategies/figures_converter/decorators/en.rb +3 -0
  27. data/lib/numbers_and_words/strategies/figures_converter/decorators/en/base.rb +12 -0
  28. data/lib/numbers_and_words/strategies/figures_converter/decorators/en/fractional.rb +38 -0
  29. data/lib/numbers_and_words/strategies/figures_converter/decorators/en/integral.rb +12 -0
  30. data/lib/numbers_and_words/strategies/figures_converter/decorators/hu.rb +3 -0
  31. data/lib/numbers_and_words/strategies/figures_converter/decorators/hu/base.rb +12 -0
  32. data/lib/numbers_and_words/strategies/figures_converter/decorators/hu/fractional.rb +38 -0
  33. data/lib/numbers_and_words/strategies/figures_converter/decorators/hu/integral.rb +12 -0
  34. data/lib/numbers_and_words/strategies/figures_converter/decorators/ru.rb +3 -0
  35. data/lib/numbers_and_words/strategies/figures_converter/decorators/ru/base.rb +12 -0
  36. data/lib/numbers_and_words/strategies/figures_converter/decorators/ru/fractional.rb +39 -0
  37. data/lib/numbers_and_words/strategies/figures_converter/decorators/ru/integral.rb +27 -0
  38. data/lib/numbers_and_words/strategies/figures_converter/decorators/ua.rb +3 -0
  39. data/lib/numbers_and_words/strategies/figures_converter/decorators/ua/base.rb +12 -0
  40. data/lib/numbers_and_words/strategies/figures_converter/decorators/ua/fractional.rb +39 -0
  41. data/lib/numbers_and_words/strategies/figures_converter/decorators/ua/integral.rb +27 -0
  42. data/lib/numbers_and_words/strategies/figures_converter/languages.rb +39 -0
  43. data/lib/numbers_and_words/strategies/figures_converter/languages/base.rb +29 -0
  44. data/lib/numbers_and_words/strategies/figures_converter/languages/en.rb +48 -0
  45. data/lib/numbers_and_words/strategies/figures_converter/languages/es.rb +37 -0
  46. data/lib/numbers_and_words/strategies/figures_converter/languages/families/base.rb +77 -0
  47. data/lib/numbers_and_words/strategies/figures_converter/languages/families/cyrillic.rb +34 -0
  48. data/lib/numbers_and_words/strategies/figures_converter/languages/families/helpers.rb +36 -0
  49. data/lib/numbers_and_words/strategies/figures_converter/languages/families/latin.rb +13 -0
  50. data/lib/numbers_and_words/strategies/figures_converter/languages/fr.rb +37 -0
  51. data/lib/numbers_and_words/strategies/figures_converter/languages/hu.rb +63 -0
  52. data/lib/numbers_and_words/strategies/figures_converter/languages/it.rb +11 -0
  53. data/lib/numbers_and_words/strategies/figures_converter/languages/nl.rb +26 -0
  54. data/lib/numbers_and_words/strategies/figures_converter/languages/ru.rb +11 -0
  55. data/lib/numbers_and_words/strategies/figures_converter/languages/se.rb +11 -0
  56. data/lib/numbers_and_words/strategies/figures_converter/languages/tr.rb +11 -0
  57. data/lib/numbers_and_words/strategies/figures_converter/languages/ua.rb +11 -0
  58. data/lib/numbers_and_words/strategies/figures_converter/options.rb +41 -0
  59. data/lib/numbers_and_words/strategies/figures_converter/options/en.rb +4 -0
  60. data/lib/numbers_and_words/strategies/figures_converter/options/en/hundreds_with_union.rb +33 -0
  61. data/lib/numbers_and_words/strategies/figures_converter/options/en/ordinal.rb +60 -0
  62. data/lib/numbers_and_words/strategies/figures_converter/options/en/remove_hyphen.rb +29 -0
  63. data/lib/numbers_and_words/strategies/figures_converter/options/en/remove_zero.rb +29 -0
  64. data/lib/numbers_and_words/strategies/figures_converter/options/hu.rb +1 -0
  65. data/lib/numbers_and_words/strategies/figures_converter/options/hu/ordinal.rb +60 -0
  66. data/lib/numbers_and_words/strategies/figures_converter/options/ru.rb +1 -0
  67. data/lib/numbers_and_words/strategies/figures_converter/options/ru/gender.rb +28 -0
  68. data/lib/numbers_and_words/strategies/figures_converter/options/ua.rb +1 -0
  69. data/lib/numbers_and_words/strategies/figures_converter/options/ua/gender.rb +28 -0
  70. data/lib/numbers_and_words/translations.rb +25 -0
  71. data/lib/numbers_and_words/{translations_helpers → translations}/base.rb +2 -2
  72. data/lib/numbers_and_words/translations/en.rb +8 -0
  73. data/lib/numbers_and_words/translations/es.rb +37 -0
  74. data/lib/numbers_and_words/translations/extensions/fraction_significance.rb +25 -0
  75. data/lib/numbers_and_words/translations/families/base.rb +33 -0
  76. data/lib/numbers_and_words/translations/families/cyrillic.rb +29 -0
  77. data/lib/numbers_and_words/translations/families/latin.rb +23 -0
  78. data/lib/numbers_and_words/translations/fr.rb +29 -0
  79. data/lib/numbers_and_words/translations/hu.rb +26 -0
  80. data/lib/numbers_and_words/translations/it.rb +15 -0
  81. data/lib/numbers_and_words/translations/nl.rb +15 -0
  82. data/lib/numbers_and_words/translations/ru.rb +25 -0
  83. data/lib/numbers_and_words/translations/se.rb +11 -0
  84. data/lib/numbers_and_words/translations/tr.rb +11 -0
  85. data/lib/numbers_and_words/translations/ua.rb +21 -0
  86. data/lib/numbers_and_words/version.rb +1 -1
  87. data/lib/numbers_and_words/wrappers.rb +2 -0
  88. data/lib/numbers_and_words/wrappers/float.rb +60 -0
  89. data/lib/numbers_and_words/wrappers/integer.rb +21 -0
  90. metadata +81 -44
  91. data/lib/numbers_and_words/array_additions.rb +0 -2
  92. data/lib/numbers_and_words/array_additions/helpers.rb +0 -44
  93. data/lib/numbers_and_words/array_additions/validations.rb +0 -9
  94. data/lib/numbers_and_words/figures_array.rb +0 -15
  95. data/lib/numbers_and_words/strategies/base.rb +0 -18
  96. data/lib/numbers_and_words/strategies/en.rb +0 -8
  97. data/lib/numbers_and_words/strategies/families/base.rb +0 -71
  98. data/lib/numbers_and_words/strategies/families/cyrillic.rb +0 -18
  99. data/lib/numbers_and_words/strategies/families/latin.rb +0 -9
  100. data/lib/numbers_and_words/strategies/fr.rb +0 -32
  101. data/lib/numbers_and_words/strategies/it.rb +0 -8
  102. data/lib/numbers_and_words/strategies/nl.rb +0 -23
  103. data/lib/numbers_and_words/strategies/ru.rb +0 -8
  104. data/lib/numbers_and_words/strategies/tr.rb +0 -8
  105. data/lib/numbers_and_words/strategies/ua.rb +0 -8
  106. data/lib/numbers_and_words/translations_helpers.rb +0 -11
  107. data/lib/numbers_and_words/translations_helpers/en.rb +0 -12
  108. data/lib/numbers_and_words/translations_helpers/families/base.rb +0 -30
  109. data/lib/numbers_and_words/translations_helpers/families/cyrillic.rb +0 -28
  110. data/lib/numbers_and_words/translations_helpers/families/latin.rb +0 -23
  111. data/lib/numbers_and_words/translations_helpers/fr.rb +0 -34
  112. data/lib/numbers_and_words/translations_helpers/it.rb +0 -16
  113. data/lib/numbers_and_words/translations_helpers/nl.rb +0 -18
  114. data/lib/numbers_and_words/translations_helpers/ru.rb +0 -8
  115. data/lib/numbers_and_words/translations_helpers/tr.rb +0 -8
  116. data/lib/numbers_and_words/translations_helpers/ua.rb +0 -8
@@ -0,0 +1,39 @@
1
+ module NumbersAndWords
2
+ module Strategies
3
+ module FiguresConverter
4
+ module Decorators
5
+ module Ru
6
+ class Fractional < Base
7
+
8
+ def run
9
+ @options[:gender] = :female
10
+ "#{super} #{fraction_significance}"
11
+ end
12
+
13
+ private
14
+
15
+ def fraction_significance
16
+ @strategy.translations.micros full_fraction.fraction_capacity, figures.reverse.join.to_i
17
+ end
18
+
19
+ def full_fraction
20
+ (0..zero_length).inject(figures.clone) { |result, el| result.unshift '0' }.to_figures
21
+ end
22
+
23
+ def figures
24
+ @strategy.figures
25
+ end
26
+
27
+ def zero_length
28
+ fraction_length - figures.length
29
+ end
30
+
31
+ def fraction_length
32
+ @options[:fractional][:length].to_i
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ module NumbersAndWords
2
+ module Strategies
3
+ module FiguresConverter
4
+ module Decorators
5
+ module Ru
6
+ class Integral < Base
7
+
8
+ def run
9
+ @options[:gender] = :female
10
+ "#{super} #{integral_significance}"
11
+ end
12
+
13
+ private
14
+
15
+ def integral_significance
16
+ @strategy.translations.integral figures.reverse.join.to_i
17
+ end
18
+
19
+ def figures
20
+ @strategy.language.figures
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ require 'numbers_and_words/strategies/figures_converter/decorators/ua/base'
2
+ require 'numbers_and_words/strategies/figures_converter/decorators/ua/fractional'
3
+ require 'numbers_and_words/strategies/figures_converter/decorators/ua/integral'
@@ -0,0 +1,12 @@
1
+ module NumbersAndWords
2
+ module Strategies
3
+ module FiguresConverter
4
+ module Decorators
5
+ module Ua
6
+ class Base < Decorators::Base
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,39 @@
1
+ module NumbersAndWords
2
+ module Strategies
3
+ module FiguresConverter
4
+ module Decorators
5
+ module Ua
6
+ class Fractional < Base
7
+
8
+ def run
9
+ @options[:gender] = :female
10
+ "#{super} #{fraction_significance}"
11
+ end
12
+
13
+ private
14
+
15
+ def fraction_significance
16
+ @strategy.translations.micros full_fraction.fraction_capacity, figures.reverse.join.to_i
17
+ end
18
+
19
+ def full_fraction
20
+ (0..zero_length).inject(figures.clone) { |result, el| result.unshift '0' }.to_figures
21
+ end
22
+
23
+ def figures
24
+ @strategy.figures
25
+ end
26
+
27
+ def zero_length
28
+ fraction_length - figures.length
29
+ end
30
+
31
+ def fraction_length
32
+ @options[:fractional][:length].to_i
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ module NumbersAndWords
2
+ module Strategies
3
+ module FiguresConverter
4
+ module Decorators
5
+ module Ua
6
+ class Integral < Base
7
+
8
+ def run
9
+ @options[:gender] = :female
10
+ "#{super} #{integral_significance}"
11
+ end
12
+
13
+ private
14
+
15
+ def integral_significance
16
+ @strategy.translations.integral figures.reverse.join.to_i
17
+ end
18
+
19
+ def figures
20
+ @strategy.language.figures
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,39 @@
1
+ require 'numbers_and_words/strategies/figures_converter/languages/families/helpers'
2
+ require 'numbers_and_words/strategies/figures_converter/languages/families/base'
3
+
4
+ require 'numbers_and_words/strategies/figures_converter/languages/base'
5
+
6
+ require 'numbers_and_words/strategies/figures_converter/languages/families/latin'
7
+
8
+ require 'numbers_and_words/strategies/figures_converter/languages/en'
9
+ require 'numbers_and_words/strategies/figures_converter/languages/fr'
10
+ require 'numbers_and_words/strategies/figures_converter/languages/hu'
11
+ require 'numbers_and_words/strategies/figures_converter/languages/se'
12
+ require 'numbers_and_words/strategies/figures_converter/languages/it'
13
+ require 'numbers_and_words/strategies/figures_converter/languages/nl'
14
+ require 'numbers_and_words/strategies/figures_converter/languages/tr'
15
+ require 'numbers_and_words/strategies/figures_converter/languages/es'
16
+
17
+ require 'numbers_and_words/strategies/figures_converter/languages/families/cyrillic'
18
+
19
+ require 'numbers_and_words/strategies/figures_converter/languages/ru'
20
+ require 'numbers_and_words/strategies/figures_converter/languages/ua'
21
+
22
+
23
+ module NumbersAndWords
24
+ module Strategies
25
+ module FiguresConverter
26
+ module Languages
27
+ class << self
28
+ def factory strategy
29
+ "#{name}::#{language_class_name}".constantize.new strategy
30
+ end
31
+
32
+ def language_class_name
33
+ ::I18n.locale.to_s.titleize.gsub ' ', '::'
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,29 @@
1
+ module NumbersAndWords
2
+ module Strategies
3
+ module FiguresConverter
4
+ module Languages
5
+ class Base
6
+ include Languages::Families::Base
7
+
8
+ attr_accessor :strategy, :figures, :translations, :strings, :options
9
+
10
+ def initialize strategy
11
+ @strategy = strategy
12
+ @figures = strategy.figures
13
+ @translations = strategy.translations
14
+ @options = strategy.options
15
+ end
16
+
17
+ def words
18
+ @strings = strings_logic
19
+ @strings.empty? && zero || print_words
20
+ end
21
+
22
+ def print_words
23
+ @strings.reverse.join ' '
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,48 @@
1
+ module NumbersAndWords
2
+ module Strategies
3
+ module FiguresConverter
4
+ module Languages
5
+ class En < Base
6
+ include Families::Latin
7
+
8
+ [:ones, :teens, :tens, :megs].each do |method_name|
9
+ define_method(method_name) {
10
+ super({:prefix => maybe_ordinal(method_name)})
11
+ }
12
+ end
13
+
14
+ def hundreds
15
+ maybe_union_after_hundreds super({:prefix => maybe_ordinal(:hundreds)})
16
+ end
17
+
18
+ def zero
19
+ super({:prefix => maybe_ordinal(:zero)}) unless maybe_remove_zero
20
+ end
21
+
22
+ def tens_with_ones
23
+ super({
24
+ :separator => maybe_hyphen_separator,
25
+ :prefix => maybe_ordinal(:tens_with_ones)
26
+ })
27
+ end
28
+
29
+ def maybe_hyphen_separator
30
+ @options.remove_hyphen.result
31
+ end
32
+
33
+ def maybe_ordinal type
34
+ @options.ordinal.result type
35
+ end
36
+
37
+ def maybe_union_after_hundreds translations
38
+ @options.hundreds_with_union.modify_or_leave translations
39
+ end
40
+
41
+ def maybe_remove_zero
42
+ @options.remove_zero.result
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,37 @@
1
+ module NumbersAndWords
2
+ module Strategies
3
+ module FiguresConverter
4
+ module Languages
5
+ class Es < Base
6
+ include Families::Latin
7
+
8
+ def capacity_iteration
9
+ words = []
10
+ capacity_words = words_in_capacity(@current_capacity)
11
+ words.push megs(capacity_words) unless capacity_words.empty?
12
+
13
+ if 0 < @current_capacity
14
+ # eg 1000000 should be "un millon" not "uno millon"
15
+ # 501000 should be "quinientos un mil" not "quinientos uno mil"
16
+ # so we replace "uno" with "un" throughout
17
+
18
+ capacity_words = capacity_words.map{|word|
19
+ word.gsub( @translations.ones(1), @translations.one )
20
+ }
21
+ end
22
+
23
+ words + capacity_words
24
+ end
25
+
26
+ def hundreds
27
+ super({:is_hundred => (figures[1,2] == [0,1] && simple_number_to_words.empty?)})
28
+ end
29
+
30
+ def megs capacity_words
31
+ super({:is_one => capacity_words == [@translations.ones(1)]})
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,77 @@
1
+ module NumbersAndWords
2
+ module Strategies
3
+ module FiguresConverter
4
+ module Languages
5
+ module Families
6
+ module Base
7
+ include Helpers
8
+
9
+ attr_accessor :current_capacity, :parent_figures
10
+
11
+ def strings_logic
12
+ if @figures.capacity_count
13
+ number_without_capacity_to_words + complex_number_to_words
14
+ elsif @figures.hundreds
15
+ hundreds_number_to_words
16
+ elsif @figures.tens or @figures.ones
17
+ simple_number_to_words
18
+ else
19
+ []
20
+ end
21
+ end
22
+
23
+ def complex_number_to_words
24
+ (1..@figures.capacity_count).map { |capacity|
25
+ @current_capacity = capacity
26
+ capacity_iteration
27
+ }.flatten
28
+ end
29
+
30
+ def capacity_iteration
31
+ words = []
32
+ capacity_words = words_in_capacity(@current_capacity)
33
+ words.push(megs) unless capacity_words.empty?
34
+ words + capacity_words
35
+ end
36
+
37
+ def words_in_capacity capacity = 0
38
+ save_parent_figures do
39
+ @figures = @parent_figures.figures_array_in_capacity(capacity)
40
+ strings_logic
41
+ end
42
+ end
43
+
44
+ alias_method :number_without_capacity_to_words, :words_in_capacity
45
+
46
+ def hundreds_number_to_words
47
+ simple_number_to_words + [hundreds]
48
+ end
49
+
50
+ def complex_tens
51
+ @figures.ones ? tens_with_ones : tens
52
+ end
53
+
54
+ def simple_number_to_words
55
+ if @figures.teens
56
+ [teens]
57
+ elsif @figures.tens
58
+ [complex_tens]
59
+ elsif @figures.ones
60
+ [ones]
61
+ else
62
+ []
63
+ end
64
+ end
65
+
66
+ def save_parent_figures
67
+ @parent_figures = @figures
68
+ result = yield
69
+ @figures = @parent_figures
70
+ result
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,34 @@
1
+ module NumbersAndWords
2
+ module Strategies
3
+ module FiguresConverter
4
+ module Languages
5
+ module Families
6
+ module Cyrillic
7
+ include Base
8
+
9
+ def gender
10
+ case @current_capacity.to_i
11
+ when 1
12
+ :female
13
+ when 0
14
+ options.gender.result
15
+ else
16
+ :male
17
+ end
18
+ end
19
+
20
+ [:ones, :tens_with_ones].each do |method_name|
21
+ define_method(method_name) {
22
+ super({:gender => gender})
23
+ }
24
+ end
25
+
26
+ def megs
27
+ super({:number => @figures.number_in_capacity(@current_capacity)})
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,36 @@
1
+ module NumbersAndWords
2
+ module Strategies
3
+ module FiguresConverter
4
+ module Languages
5
+ module Families
6
+ module Helpers
7
+ [:zero].each do |method_name|
8
+ define_method(method_name) { |*args|
9
+ options = args.first || {}
10
+ translate method_name, options
11
+ }
12
+ end
13
+
14
+ [:ones, :teens, :tens, :tens_with_ones, :hundreds].each do |method_name|
15
+ define_method(method_name) { |*args|
16
+ options = args.first || {}
17
+ translate method_name, @figures.send(method_name), options
18
+ }
19
+ end
20
+
21
+ [:megs].each do |method_name|
22
+ define_method(method_name) { |*args|
23
+ options = args.first || {}
24
+ translate method_name, @current_capacity, options
25
+ }
26
+ end
27
+
28
+ def translate method_name, *args
29
+ translations.send method_name, *args
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end