Class: NumberWords::NumbersToWords
- Inherits:
-
Object
- Object
- NumberWords::NumbersToWords
- Defined in:
- lib/modules/number_words.rb
Constant Summary collapse
- DEFAULTS =
{styled: false, case: 'lower', space: ' '}
Instance Attribute Summary collapse
-
#hundred ⇒ Object
readonly
Returns the value of attribute hundred.
-
#large_nums ⇒ Object
readonly
Returns the value of attribute large_nums.
-
#num ⇒ Object
readonly
Returns the value of attribute num.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #basic_word_group(group) ⇒ Object
- #formatted_words(collector) ⇒ Object
- #hundreder(group) ⇒ Object
-
#initialize(options = {}) ⇒ NumbersToWords
constructor
A new instance of NumbersToWords.
- #joined_tenner(first, second) ⇒ Object
- #large_nums_with_commas ⇒ Object
- #rearrange_group(group) ⇒ Object
- #set_style_opts ⇒ Object
- #tenner(first, second) ⇒ Object
- #to_words(num) ⇒ Object
- #word_group(group, i) ⇒ Object
- #words_for_groups(groups) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ NumbersToWords
Returns a new instance of NumbersToWords.
34 35 36 37 |
# File 'lib/modules/number_words.rb', line 34 def initialize( = {}) = DEFAULTS.merge() set_style_opts end |
Instance Attribute Details
#hundred ⇒ Object (readonly)
Returns the value of attribute hundred.
31 32 33 |
# File 'lib/modules/number_words.rb', line 31 def hundred @hundred end |
#large_nums ⇒ Object (readonly)
Returns the value of attribute large_nums.
31 32 33 |
# File 'lib/modules/number_words.rb', line 31 def large_nums @large_nums end |
#num ⇒ Object (readonly)
Returns the value of attribute num.
30 31 32 |
# File 'lib/modules/number_words.rb', line 30 def num @num end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
30 31 32 |
# File 'lib/modules/number_words.rb', line 30 def end |
Instance Method Details
#basic_word_group(group) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/modules/number_words.rb', line 85 def basic_word_group(group) case group.size when 0; '' when 1; WORDS[group[0]] when 2; tenner(group[0], group[1]) when 3; hundreder(group) end end |
#formatted_words(collector) ⇒ Object
57 58 59 60 61 |
# File 'lib/modules/number_words.rb', line 57 def formatted_words(collector) words = collector.reverse.join([:space]).gsub(/,$/,'') return words unless [:case] == 'upper' words.split(/(\W)/).map(&:capitalize).join.gsub(/ and/i, ' and') end |
#hundreder(group) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/modules/number_words.rb', line 94 def hundreder(group) tens = tenner(group[1], group[2]) hundred_joiner = tens.empty? ? 'hundred' : hundred [WORDS[group[0]], hundred_joiner, tens ].reject { |n| n.empty? }.join([:space]) end |
#joined_tenner(first, second) ⇒ Object
110 111 112 113 |
# File 'lib/modules/number_words.rb', line 110 def joined_tenner(first, second) joiner = [:styled] ? '-' : [:space] WORDS[first + '0'] + joiner + WORDS[second] end |
#large_nums_with_commas ⇒ Object
44 45 46 47 48 |
# File 'lib/modules/number_words.rb', line 44 def large_nums_with_commas large_nums = LARGE_NUMS.dup large_nums.each { |k,v| large_nums[k] = "#{v}," } large_nums end |
#rearrange_group(group) ⇒ Object
74 75 76 77 |
# File 'lib/modules/number_words.rb', line 74 def rearrange_group(group) group.reverse! group.shift while group.first == '0' end |
#set_style_opts ⇒ Object
39 40 41 42 |
# File 'lib/modules/number_words.rb', line 39 def set_style_opts @hundred = [:styled] ? 'hundred and' : 'hundred' @large_nums = [:styled] ? large_nums_with_commas : LARGE_NUMS end |
#tenner(first, second) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/modules/number_words.rb', line 103 def tenner(first, second) return '' if first == '0' and second == '0' return WORDS[second] if first == '0' and second != '0' return WORDS[first + second] if first == '1' or second == '0' joined_tenner(first, second) end |
#to_words(num) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/modules/number_words.rb', line 50 def to_words(num) arr = num.to_s.each_char.to_a groups = arr.reverse.each_slice(3).to_a collector = words_for_groups(groups) formatted_words(collector) end |
#word_group(group, i) ⇒ Object
79 80 81 82 83 |
# File 'lib/modules/number_words.rb', line 79 def word_group(group, i) words = basic_word_group(group) words += [:space] + large_nums[i] if large_nums[i] words end |
#words_for_groups(groups) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/modules/number_words.rb', line 63 def words_for_groups(groups) collector = [] groups.each_with_index do |group, i| next if group.all? { |e| e == '0' } rearrange_group(group) words = word_group(group, i) collector << words end collector end |