Module: Humanize

Included in:
Numeric
Defined in:
lib/humanize.rb

Defined Under Namespace

Classes: Configuration

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configObject



50
51
52
# File 'lib/humanize.rb', line 50

def self.config
  @config ||= Configuration.new
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:



58
59
60
# File 'lib/humanize.rb', line 58

def self.configure
  yield(config)
end

.reset_configObject



54
55
56
# File 'lib/humanize.rb', line 54

def self.reset_config
  @config = Configuration.new
end

Instance Method Details

#humanize(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/humanize.rb', line 9

def humanize(options = {})
  locale = options[:locale] || Humanize.config.default_locale
  decimals_as = options[:decimals_as] || Humanize.config.decimals_as
  num = self.to_i
  o = ''
  if num < 0
    o += WORDS[locale][:negative] + ' '
    num = num.abs
  end
  if num.zero?
    o += WORDS[locale][:zero]
  else
    sets = []
    i = 0
    f = false
    while !num.zero?
      num, r = num.divmod(1000)
      sets << LOTS[locale][i] + (!sets.empty? ? (f ? ' ' + WORDS[locale][:and] : WORDS[locale][:comma]) : '') if !(r.zero? || i.zero?)
      f = true if i.zero? && r < 100

      sets << SUB_ONE_THOUSAND[locale][r] if !r.zero? && !exactly_one_thousand_in_french?(locale, r, sets)
      i = i.succ

    end
    o += sets.reverse.join(' ')
  end
  if self.class == Float
    decimals = self.to_s.split(/\./, 2).last
    decimals_as_words = case decimals_as
                        when :digits then decimals.scan(/./).map { |n| SUB_ONE_THOUSAND[locale][n.to_i] }.join(' ')
                        when :number then decimals.to_i.humanize(:locale => locale)
                        end
    o += ' ' + WORDS[locale][:point] + ' ' + decimals_as_words
  end
  o.gsub(/ +/, ' ')
end