Skip to content

Commit

Permalink
Passing specs on :pronounced
Browse files Browse the repository at this point in the history
  • Loading branch information
lpradovera authored and bklang committed Dec 15, 2014
1 parent 305e561 commit b02f42e
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
coverage
pkg
Gemfile.lock
.bundle
16 changes: 16 additions & 0 deletions lib/numbers_and_words/strategies/figures_converter/languages/en.rb
Expand Up @@ -26,6 +26,22 @@ def tens_with_ones
})
end

def strings_logic
if @options.pronounced.active?
@options.pronounced.process self, @figures
else
if @figures.capacity_count
number_without_capacity_to_words + complex_number_to_words
elsif @figures.hundreds
hundreds_number_to_words
elsif @figures.tens or @figures.ones
simple_number_to_words
else
[]
end
end
end

def maybe_hyphen_separator
@options.remove_hyphen.result
end
Expand Down
@@ -1,4 +1,5 @@
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/ordinal'
require 'numbers_and_words/strategies/figures_converter/options/en/remove_zero'
require 'numbers_and_words/strategies/figures_converter/options/en/pronounced'
require 'numbers_and_words/strategies/figures_converter/options/en/remove_zero'
@@ -0,0 +1,97 @@
module NumbersAndWords
module Strategies
module FiguresConverter
module Options
module En
class Pronounced
attr_accessor :strategy, :options

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


def result
active? ? 'PRONOUNCED' : 'NOTPRONOUNCED'
end

def active?
@options[:pronounced]
end

def process language, figures
@language = language
@figures = figures
units, tens, hundreds, thousands = *@figures.to_a.dup

if figures.to_a.count > 4
@language.number_without_capacity_to_words + @language.complex_number_to_words
elsif @figures.capacity_count
handle_thousands thousands, hundreds, tens, units
elsif @figures.hundreds
handle_hundreds hundreds, tens, units
elsif @figures.tens or @figures.ones
@language.simple_number_to_words
else
[]
end
end

def handle_thousands thousands, hundreds, tens, units
if hundreds == 0
@language.number_without_capacity_to_words + @language.complex_number_to_words
else
result = []
if tens == 0 && units == 0
result.push 'hundred'
else
result += tens_with_oh tens, units
end
result.push "#{thousands}#{hundreds}".to_i.to_words
result
end
end

def handle_hundreds hundreds, tens, units
result = []
if tens == 0
if units == 0
result.push @language.hundreds_number_to_words
else
result.push @language.ones
result.push 'oh'
result.push hundreds.to_words
end
else
if @figures.teens
result.push @language.teens
elsif @figures.tens
result.push @language.complex_tens
end
result.push hundreds.to_words
end
result
end

def tens_with_oh tens, units
result = []
if tens == 0
if units != 0
result.push @language.ones
result.push 'oh'
end
else
if @figures.teens
result.push @language.teens
elsif @figures.tens
result.push @language.complex_tens
end
end
end
end
end
end
end
end
end
@@ -1,4 +1,5 @@
require 'numbers_and_words/strategies/figures_converter/options/en_gb/hundreds_with_union'
require 'numbers_and_words/strategies/figures_converter/options/en_gb/remove_hyphen'
require 'numbers_and_words/strategies/figures_converter/options/en_gb/ordinal'
require 'numbers_and_words/strategies/figures_converter/options/en_gb/pronounced'
require 'numbers_and_words/strategies/figures_converter/options/en_gb/remove_zero'
@@ -0,0 +1,12 @@
module NumbersAndWords
module Strategies
module FiguresConverter
module Options
module EnGb
class Pronounced < En::Pronounced
end
end
end
end
end
end
3 changes: 3 additions & 0 deletions spec/numbers_and_words/integer/fixture_examples/en.yml
Expand Up @@ -124,5 +124,8 @@ to_words:
210: two ten
612: six twelve
1796: seventeen ninety-six
1900: nineteen hundred
1907: nineteen oh seven
2000: two thousand
2007: two thousand seven
2140: twenty-one forty

0 comments on commit b02f42e

Please sign in to comment.