Module: IceCube::StringBuilder::Helpers

Included in:
IceCube::StringBuilder
Defined in:
lib/ice_cube/builders/string_builder.rb

Instance Method Summary collapse

Instance Method Details

#literal_ordinal(number) ⇒ Object


59
60
61
# File 'lib/ice_cube/builders/string_builder.rb', line 59

def literal_ordinal(number)
  IceCube::I18n.t("ice_cube.integer.literal_ordinals")[number]
end

#nice_number(number) ⇒ Object


51
52
53
# File 'lib/ice_cube/builders/string_builder.rb', line 51

def nice_number(number)
  literal_ordinal(number) || ordinalize(number)
end

#ordinal(number) ⇒ Object


63
64
65
66
67
68
# File 'lib/ice_cube/builders/string_builder.rb', line 63

def ordinal(number)
  ord = IceCube::I18n.t("ice_cube.integer.ordinals")[number] ||
    IceCube::I18n.t("ice_cube.integer.ordinals")[number % 10] ||
    IceCube::I18n.t('ice_cube.integer.ordinals')[:default]
  number >= 0 ? ord : IceCube::I18n.t("ice_cube.integer.negative", ordinal: ord)
end

#ordinalize(number) ⇒ Object


55
56
57
# File 'lib/ice_cube/builders/string_builder.rb', line 55

def ordinalize(number)
  IceCube::I18n.t('ice_cube.integer.ordinal', number: number, ordinal: ordinal(number))
end

#sentence(array) ⇒ Object

influenced by ActiveSupport's to_sentence


42
43
44
45
46
47
48
49
# File 'lib/ice_cube/builders/string_builder.rb', line 42

def sentence(array)
  case array.length
  when 0 ; ''
  when 1 ; array[0].to_s
  when 2 ; "#{array[0]}#{IceCube::I18n.t('ice_cube.array.two_words_connector')}#{array[1]}"
  else ; "#{array[0...-1].join(IceCube::I18n.t('ice_cube.array.words_connector'))}#{IceCube::I18n.t('ice_cube.array.last_word_connector')}#{array[-1]}"
  end
end