Class: Wordify::Constructor
- Inherits:
-
Object
- Object
- Wordify::Constructor
- Defined in:
- lib/wordify/constructor.rb
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #decimals ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(number) ⇒ Constructor
constructor
A new instance of Constructor.
- #out_of_range? ⇒ Boolean
- #parts ⇒ Object
- #qnts ⇒ Object
- #to_sentence ⇒ Object (also: #to_s)
- #tokenised_decimals ⇒ Object
- #tokens ⇒ Object
- #tokens_in_hundreds ⇒ Object
- #unreadable? ⇒ Boolean
Constructor Details
#initialize(number) ⇒ Constructor
Returns a new instance of Constructor.
4 5 6 |
# File 'lib/wordify/constructor.rb', line 4 def initialize(number) @number = number end |
Instance Attribute Details
#number ⇒ Object (readonly)
Returns the value of attribute number.
2 3 4 |
# File 'lib/wordify/constructor.rb', line 2 def number @number end |
Instance Method Details
#<=>(other) ⇒ Object
35 36 37 |
# File 'lib/wordify/constructor.rb', line 35 def <=>(other) to_sentence <=> other.to_sentence end |
#decimals ⇒ Object
59 60 61 |
# File 'lib/wordify/constructor.rb', line 59 def decimals @decimals ||= parts[1] ? ".#{parts[1]}" : nil end |
#eql?(other) ⇒ Boolean Also known as: ==
30 31 32 |
# File 'lib/wordify/constructor.rb', line 30 def eql?(other) to_sentence == other.to_sentence end |
#out_of_range? ⇒ Boolean
39 40 41 |
# File 'lib/wordify/constructor.rb', line 39 def out_of_range? tokens.size > 23 end |
#parts ⇒ Object
47 48 49 |
# File 'lib/wordify/constructor.rb', line 47 def parts @parts ||= number.to_s.split('.') end |
#qnts ⇒ Object
67 68 69 |
# File 'lib/wordify/constructor.rb', line 67 def qnts Wordify.qnts.first(tokens_in_hundreds.size).reverse end |
#to_sentence ⇒ Object Also known as: to_s
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/wordify/constructor.rb', line 8 def to_sentence if unreadable? || out_of_range? sentence = number.to_s else sentence = [] tokens_in_hundreds.each_with_index do |h, i| sentence << (qnts[i] and i+1 != qnts.size ? "#{h} #{qnts[i]}" : h) if h end last = sentence.pop prev = sentence.compact sentence = if qnts.detect{ |q| last.include?(q) } [prev, last].flatten.join(', ') else [(prev.any? ? prev.join(', ') : nil), last].compact.join(' and ') end sentence += ' ' + tokenised_decimals if decimals end sentence end |
#tokenised_decimals ⇒ Object
63 64 65 |
# File 'lib/wordify/constructor.rb', line 63 def tokenised_decimals @decimals_to_tokens ||= decimals ? Wordify::Tokeniser.new(decimals).to_s : nil end |
#tokens ⇒ Object
51 52 53 |
# File 'lib/wordify/constructor.rb', line 51 def tokens @tokens ||= parts[0].reverse.scan(/([0-9]{1,3})/).flatten.map { |i| i.reverse }.reverse end |
#tokens_in_hundreds ⇒ Object
55 56 57 |
# File 'lib/wordify/constructor.rb', line 55 def tokens_in_hundreds @tokens_in_hundreds ||= tokens.map { |token| Wordify::Token::Hundreds.new(token).to_s } end |
#unreadable? ⇒ Boolean
43 44 45 |
# File 'lib/wordify/constructor.rb', line 43 def unreadable? number.to_s.match(/[\+\-]/) != nil end |