Class: Wordify::Constructor

Inherits:
Object
  • Object
show all
Defined in:
lib/wordify/constructor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#numberObject (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

#decimalsObject



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: ==

Returns:

  • (Boolean)


30
31
32
# File 'lib/wordify/constructor.rb', line 30

def eql?(other)
  to_sentence == other.to_sentence
end

#out_of_range?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/wordify/constructor.rb', line 39

def out_of_range?
  tokens.size > 23
end

#partsObject



47
48
49
# File 'lib/wordify/constructor.rb', line 47

def parts
  @parts ||= number.to_s.split('.')
end

#qntsObject



67
68
69
# File 'lib/wordify/constructor.rb', line 67

def qnts
  Wordify.qnts.first(tokens_in_hundreds.size).reverse
end

#to_sentenceObject 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_decimalsObject



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

#tokensObject



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_hundredsObject



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

Returns:

  • (Boolean)


43
44
45
# File 'lib/wordify/constructor.rb', line 43

def unreadable?
  number.to_s.match(/[\+\-]/) != nil
end