Class: Wordify::Token::Tens

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*unit_tokens) ⇒ Tens

Returns a new instance of Tens.

Raises:

  • (StandardError)


3
4
5
6
# File 'lib/wordify/token/tens.rb', line 3

def initialize(*unit_tokens)
  @tens, @unit = unit_tokens
  raise StandardError,  'Invalid tokens' unless valid?
end

Instance Attribute Details

#tensObject (readonly)

Returns the value of attribute tens.



2
3
4
# File 'lib/wordify/token/tens.rb', line 2

def tens
  @tens
end

#unitObject (readonly)

Returns the value of attribute unit.



2
3
4
# File 'lib/wordify/token/tens.rb', line 2

def unit
  @unit
end

Instance Method Details

#<=>(other) ⇒ Object



38
39
40
# File 'lib/wordify/token/tens.rb', line 38

def <=>(other)
  to_s <=> other.to_s
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


33
34
35
# File 'lib/wordify/token/tens.rb', line 33

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

#teen?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/wordify/token/tens.rb', line 12

def teen?
  tens.number == 1
end

#ten?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/wordify/token/tens.rb', line 8

def ten?
  tens.number > 1
end

#to_sObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wordify/token/tens.rb', line 20

def to_s
  chars = []
  if ten?
    chars << Wordify.tens[tens.number]
    chars << unit.unit unless unit.zero?
  elsif teen?
    chars << Wordify.teens[unit.number]
  else
    chars << unit.unit unless unit.zero?
  end
  chars.any? ? chars.join(' ') : nil
end

#valid?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/wordify/token/tens.rb', line 16

def valid?
  tens.is_a?(Wordify::Token::Unit) and unit.is_a?(Wordify::Token::Unit)
end