Skip to content

Commit

Permalink
#50. Support ordinal form.
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'remotes/origin/feature/ordinals' into feature/ordinals
  • Loading branch information
kslazarev committed Mar 12, 2013
1 parent b6d7f55 commit 4857dfb
Show file tree
Hide file tree
Showing 22 changed files with 359 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ def fraction_sub_capacity
end

def ordinal_index
index { |i| i != 0 }
index { |figure| figure != 0 }
end

def ordinal_capacity
(ordinal_index.to_f / 3).floor
count = ordinal_index / FIGURES_IN_CAPACITY
0 == count ? nil : count
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/numbers_and_words/i18n/locales/numbers.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ en:
tens: [zero, ten, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety]
hundreds: hundred
mega: [ones, thousand, million, billion, trillion, quadrillion, quintillion, sextillion, septillion, octillion, nonillion, decillion]
ordinals:
ordinal:
ones: [zeroth, first, second, third, fourth, fifth, sixth, seventh, eighth, ninth]
teens: [tenth, eleventh, twelfth, thirteenth, fourteenth, fifteenth, sixteenth, seventeenth, eighteenth, nineteenth]
tens: [zeroth, tenth, twentieth, thirtieth, fortieth, fiftieth, sixtieth, seventieth, eightieth, ninetieth]
Expand Down
3 changes: 2 additions & 1 deletion lib/numbers_and_words/i18n/locales/numbers.hu.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
hu:
numbers:
ones: [nulla, egy, kettő, három, négy, öt, hat, hét, nyolc, kilenc]
ones_with_tens: [nulla, egy, kettő, három, négy, öt, hat, hét, nyolc, kilenc]
tens: [nulla, tíz, húsz, harminc, negyven, ötven, hatvan, hetven, nyolcvan, kilencven]
tens_with_ones: [nulla, tizen, huszon, harminc, negyven, ötven, hatvan, hetven, nyolcvan, kilencven]
hundreds: száz
mega: [egy, ezer, millió, milliárd, billió, billiárd, trillió, trilliárd, kvadrillió, kvadrilliárd, kvintillió, kvintilliárd]
fraction_separator: egész
ordinals:
ordinal:
ones: [nulladik, első, második, harmadik, negyedik, ötödik, hatodik, hetedik, nyolcadik, kilencedik]
ones_with_tens: [nulladik, egyedik, kettedik, harmadik, negyedik, ötödik, hatodik, hetedik, nyolcadik, kilencedik]
tens: [nulladik, tizedik, huszadik, harmincadik, negyvenedik, ötvenedik, hatvanadik, hetvenedik, nyolcvanadik, kilencvenedik]
Expand Down
51 changes: 43 additions & 8 deletions lib/numbers_and_words/strategies/figures_converter/languages/en.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,56 @@ def union_elements elements, options = {}
end

def complex_tens
figures.ones ?
translations.tens_with_ones(figures.tens_with_ones, hyphen_separator) :
translations.tens(figures.tens)
figures.ones ? tens_with_ones : tens
end

def hundreds_number_to_words
simple_number_to_words + union_after_hundreds([translations.hundreds(figures.hundreds)])
simple_number_to_words + maybe_union_after_hundreds([hundreds])
end

def hyphen_separator
options.remove_hyphen.result
def simple_number_to_words
if figures.teens
[teens]
elsif figures.tens
[complex_tens]
elsif figures.ones
[ones]
else
[]
end
end

def union_after_hundreds translations
options.hundreds_with_union.modify_or_leave translations
def capacity_iteration capacity
words = []
capacity_words = words_in_capacity(capacity)
words.push(megs) unless capacity_words.empty?
words + capacity_words
end

[:ones, :teens, :tens, :hundreds, :megs].each do |method_name|
define_method(method_name) { translate method_name }
end

def tens_with_ones
translate :tens_with_ones, {:separator => maybe_hyphen_separator}
end

def translate type, options = {}
maybe_ordinal type,
Proc.new { |*args| translations.send type, *args },
options
end

def maybe_hyphen_separator
@options.remove_hyphen.result
end

def maybe_ordinal type, proc_method, options = {}
@options.ordinal.result type, proc_method, options
end

def maybe_union_after_hundreds translations
@options.hundreds_with_union.modify_or_leave translations
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Languages
module Families
module Base

attr_accessor :current_capacity
attr_accessor :current_capacity, :parent_figures

private

Expand Down Expand Up @@ -56,7 +56,7 @@ def complex_tens

def simple_number_to_words
if figures.teens
[translations.teens(figures.ones)]
[translations.teens(figures.teens)]
elsif figures.tens
[complex_tens]
elsif figures.ones
Expand All @@ -67,9 +67,9 @@ def simple_number_to_words
end

def save_parent_figures
parent_figures = @figures
@parent_figures = @figures
result = yield(parent_figures)
@figures = parent_figures
@figures = @parent_figures
result
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def complex_tens

def simple_number_to_words
if figures.teens
[translations.teens(figures.ones)]
[translations.teens(figures.teens)]
elsif figures.tens
[complex_tens]
elsif figures.ones
Expand Down
38 changes: 35 additions & 3 deletions lib/numbers_and_words/strategies/figures_converter/languages/hu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Hu < Base
#include Extensions::Options::Fractional
#include NumbersAndWords::Translations::Hu

private
#private

def print_words
inner_reverse_words.reverse.join(greater_than_2000? && '-' || '')
Expand Down Expand Up @@ -35,18 +35,50 @@ def strings_logic

def complex_number_to_words
count = figures.capacity_count
(1..count).map { |capacity| capacity_iteration(capacity).flatten }.reject(&:empty?)
(1..count).map { |capacity|
@current_capacity = capacity
capacity_iteration(capacity).flatten
}.reject(&:empty?)
end

def complex_tens
figures.ones ? tens_with_ones : tens
end

def hundreds_number_to_words
simple_number_to_words + [hundreds]
end

def simple_number_to_words
if figures.teens || figures.tens
[complex_tens]
elsif figures.ones
[translations.ones(figures.ones)]
[ones]
else
[]
end
end

def capacity_iteration capacity
words = []
capacity_words = words_in_capacity(capacity)
words.push(megs) unless capacity_words.empty?
words + capacity_words
end

def translate type, options = {}
maybe_ordinal type,
Proc.new { |*args| translations.send type, *args },
options
end

[:ones, :teens, :tens_with_ones, :tens, :hundreds, :megs].each do |method_name|
define_method(method_name) { translate method_name }
end

def maybe_ordinal type, proc_method, options = {}
@options.ordinal.result type, proc_method, options
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'numbers_and_words/strategies/figures_converter/options/en'
require 'numbers_and_words/strategies/figures_converter/options/ru'
require 'numbers_and_words/strategies/figures_converter/options/ua'
require 'numbers_and_words/strategies/figures_converter/options/hu'

module NumbersAndWords
module Strategies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require 'numbers_and_words/strategies/figures_converter/options/en/hundreds_with_union'
require 'numbers_and_words/strategies/figures_converter/options/en/remove_hyphen'
require 'numbers_and_words/strategies/figures_converter/options/en/remove_hyphen'
require 'numbers_and_words/strategies/figures_converter/options/en/ordinal'
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module NumbersAndWords
module Strategies
module FiguresConverter
module Options
module En
class Ordinal
HUNDRED_TYPE = :hundreds
MEGS_TYPE = :megs

attr_accessor :strategy, :options

def initialize proxy, *args, &block
@strategy = proxy.strategy
@options = proxy.options
end

def result type, proc_method, proc_options = {}
@type, @proc_method, @proc_options = type, proc_method, proc_options
MEGS_TYPE != type ? check_simple_numbers : check_megs_numbers
end

private

def active?
@options[:ordinal]
end

def check_simple_numbers
@proc_options[:prefix] = :ordinal if simple_numbers_condition && active?
@proc_method.call figures.send(@type), @proc_options
end

def check_megs_numbers
@proc_options[:prefix] = :ordinal if megs_numbers_condition && active?
@proc_method.call current_capacity, nil, @proc_options
end

def simple_numbers_condition
current_capacity.nil? &&
(HUNDRED_TYPE != @type || (HUNDRED_TYPE == @type && simple_number_to_words.empty?))
end

def megs_numbers_condition
current_capacity == language_figures.ordinal_capacity
end

def simple_number_to_words
strategy.language.simple_number_to_words
end

def current_capacity
strategy.language.current_capacity
end

def language_figures
strategy.language.parent_figures
end

def figures
strategy.language.figures
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'numbers_and_words/strategies/figures_converter/options/hu/ordinal'
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module NumbersAndWords
module Strategies
module FiguresConverter
module Options
module Hu
class Ordinal
HUNDRED_TYPE = :hundreds
MEGS_TYPE = :megs

attr_accessor :strategy, :options

def initialize proxy, *args, &block
@strategy = proxy.strategy
@options = proxy.options
end

def result type, proc_method, proc_options = {}
@type, @proc_method, @proc_options = type, proc_method, proc_options
MEGS_TYPE != type ? check_simple_numbers : check_megs_numbers
end

private

def active?
@options[:ordinal]
end

def check_simple_numbers
@proc_options[:prefix] = :ordinal if simple_numbers_condition && active?
@proc_method.call figures.send(@type), @proc_options
end

def check_megs_numbers
@proc_options[:prefix] = :ordinal if megs_numbers_condition && active?
@proc_method.call current_capacity, nil, @proc_options
end

def simple_numbers_condition
current_capacity.nil? &&
(HUNDRED_TYPE != @type || (HUNDRED_TYPE == @type && simple_number_to_words.empty?))
end

def megs_numbers_condition
current_capacity == language_figures.ordinal_capacity
end

def simple_number_to_words
strategy.language.simple_number_to_words
end

def current_capacity
strategy.language.current_capacity
end

def language_figures
strategy.language.parent_figures
end

def figures
strategy.language.figures
end
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/numbers_and_words/translations/es.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Es < Base
include NumbersAndWords::Translations::Families::Latin

def tens_with_ones numbers
super numbers, ' y '
super numbers, :separator => ' y '
end

def hundreds number
Expand Down
17 changes: 9 additions & 8 deletions lib/numbers_and_words/translations/families/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ module Base

#private

def teens number
t(:teens)[number]
def teens numbers, options = {}
t([options[:prefix], :teens].join('.'))[numbers[0]]
end

def tens number
t(:tens)[number]
def tens number, options = {}
t([options[:prefix], :tens].join('.'))[number]
end

def tens_with_ones numbers, separator = ' '
[tens(numbers[1]), ones(numbers[0])].join separator
def tens_with_ones numbers, options = {}
[tens(numbers[1]), ones(numbers[0], options)].join options[:separator]
end

def megs capacity, number = nil
number ? t(mega(capacity), :count => number) : t(:mega)[capacity]
def megs capacity, number = nil, options = {}
mega_name = [options[:prefix], :mega].join('.')
number ? t(mega(capacity), :count => number) : t(mega_name)[capacity]
end

def mega capacity
Expand Down
Loading

0 comments on commit 4857dfb

Please sign in to comment.