Module: PureValidator::Humanize

Defined in:
lib/pure_validator/core_extensions/humanize.rb

Class Method Summary collapse

Class Method Details

.humanize(value, options = {}) ⇒ Object

poor mans humanize… (to not depend on inflector) puts humanize “hello_there”, format: :class



5
6
7
8
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
# File 'lib/pure_validator/core_extensions/humanize.rb', line 5

def self.humanize(value, options = {})
  options[:format] = :sentence if options.empty?

  values = if value.include? '_'
   value.split('_')
  else
    [value]
  end
  values.each { |v| v.downcase! }

  if options[:format] == :allcaps
    values.each do |value|
      value.capitalize!
    end

    if options.empty?
      options[:seperator] = " "
    end

    return values.join " "
  end

  if options[:format] == :class
    values.each do |value|
      value.capitalize!
    end
    return values.join ""
  end

  if options[:format] == :sentence
    values[0].capitalize!
    return values.join " "
  end

  if options[:format] == :nocaps
    return values.join " "
  end
end