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(num, 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 ⇒ Object
- #word_group(group, i) ⇒ Object
- #words_for_groups(groups) ⇒ Object
Constructor Details
#initialize(num, options = {}) ⇒ NumbersToWords
Returns a new instance of NumbersToWords.
86 87 88 89 90 |
# File 'lib/modules/number_words.rb', line 86 def initialize(num, = {}) @num = num.to_s = DEFAULTS.merge() set_style_opts end |
Instance Attribute Details
#hundred ⇒ Object (readonly)
Returns the value of attribute hundred.
83 84 85 |
# File 'lib/modules/number_words.rb', line 83 def hundred @hundred end |
#large_nums ⇒ Object (readonly)
Returns the value of attribute large_nums.
83 84 85 |
# File 'lib/modules/number_words.rb', line 83 def large_nums @large_nums end |
#num ⇒ Object (readonly)
Returns the value of attribute num.
82 83 84 |
# File 'lib/modules/number_words.rb', line 82 def num @num end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
82 83 84 |
# File 'lib/modules/number_words.rb', line 82 def end |
Instance Method Details
#basic_word_group(group) ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/modules/number_words.rb', line 138 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
110 111 112 113 114 |
# File 'lib/modules/number_words.rb', line 110 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
147 148 149 150 151 152 153 154 |
# File 'lib/modules/number_words.rb', line 147 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
163 164 165 166 |
# File 'lib/modules/number_words.rb', line 163 def joined_tenner(first, second) joiner = [:styled] ? '-' : [:space] WORDS[first + '0'] + joiner + WORDS[second] end |
#large_nums_with_commas ⇒ Object
97 98 99 100 101 |
# File 'lib/modules/number_words.rb', line 97 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
127 128 129 130 |
# File 'lib/modules/number_words.rb', line 127 def rearrange_group(group) group.reverse! group.shift while group.first == '0' end |
#set_style_opts ⇒ Object
92 93 94 95 |
# File 'lib/modules/number_words.rb', line 92 def set_style_opts @hundred = [:styled] ? 'hundred and' : 'hundred' @large_nums = [:styled] ? large_nums_with_commas : LARGE_NUMS end |
#tenner(first, second) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/modules/number_words.rb', line 156 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 ⇒ Object
103 104 105 106 107 108 |
# File 'lib/modules/number_words.rb', line 103 def to_words arr = num.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
132 133 134 135 136 |
# File 'lib/modules/number_words.rb', line 132 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
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/modules/number_words.rb', line 116 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 |