Module: Reality::Naming

Defined in:
lib/reality/naming.rb

Class Method Summary collapse

Class Method Details

.add_pluralization_rule(&block) ⇒ Object



120
121
122
# File 'lib/reality/naming.rb', line 120

def add_pluralization_rule(&block)
  pluralization_rules.unshift(block)
end

.camelize(input_word) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/reality/naming.rb', line 22

def camelize(input_word)
  word_parts = split_into_words(input_word).collect { |part| part[0...1].upcase + part[1..-1] }
  if word_parts.size > 0 && word_parts[0] == word_parts[0].upcase
    word_parts[0] = word_parts[0].downcase
  end
  word = word_parts.join('')
  word[0...1].downcase + word[1..-1]
end

.camelize?(word) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/reality/naming.rb', line 18

def camelize?(word)
  camelize(word) == word.to_s
end

.clear_pluralization_rulesObject



124
125
126
# File 'lib/reality/naming.rb', line 124

def clear_pluralization_rules
  pluralization_rules.clear
end

.humanize(word) ⇒ Object



45
46
47
48
49
# File 'lib/reality/naming.rb', line 45

def humanize(word)
  word_parts = split_into_words(word).collect { |part| part[0...1].upcase + part[1..-1] }
  return word_parts[0] if (word_parts.size == 1 && word_parts[0] == word_parts[0].upcase)
  word_parts.join(' ')
end

.humanize?(word) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/reality/naming.rb', line 41

def humanize?(word)
  humanize(word) == word.to_s
end

.jsonize(word) ⇒ Object



79
80
81
# File 'lib/reality/naming.rb', line 79

def jsonize(word)
  camelize(word)
end

.jsonize?(word) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/reality/naming.rb', line 75

def jsonize?(word)
  jsonize(word) == word.to_s
end

.kebabcase(word) ⇒ Object



63
64
65
# File 'lib/reality/naming.rb', line 63

def kebabcase(word)
  underscore(word).tr('_', '-')
end

.kebabcase?(word) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/reality/naming.rb', line 59

def kebabcase?(word)
  kebabcase(word) == word.to_s
end

.pascal_case(word) ⇒ Object



35
36
37
38
39
# File 'lib/reality/naming.rb', line 35

def pascal_case(word)
  word_parts = split_into_words(word).collect { |part| part[0...1].upcase + part[1..-1] }
  return word_parts[0] if (word_parts.size == 1 && word_parts[0] == word_parts[0].upcase)
  word_parts.join('')
end

.pascal_case?(word) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/reality/naming.rb', line 31

def pascal_case?(word)
  pascal_case(word) == word.to_s
end

.pluralize(string) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/reality/naming.rb', line 91

def pluralize(string)
  singular = string.to_s
  length = singular.size
  last_ch = last(singular)
  last_2ch = last(singular, 2)

  plural = nil
  pluralization_rules.each do |rule|
    plural = rule.call(singular)
    break unless plural.nil?
  end
  if plural.nil?
    if last_ch == 'y'
      plural = "#{singular[0, length - 1]}ies" unless singular =~ /[aeiou]y$/
    elsif last_ch == 'f'
      plural = "#{singular[0, length - 1]}ves" unless singular =~ /[aeiou][aeiou]f$/
    elsif last_2ch == 'fe'
      plural = "#{singular[0, length - 2]}ves"
    elsif %w(is).include?(last_2ch)
      plural = "#{singular[0, length - 2]}es"
    elsif %w(ss ch sh).include?(last_2ch) || %w(s x z).include?(last_ch)
      plural = "#{singular}es"
    elsif %w(o).include?(last_ch)
      plural = "#{singular}es"
    end
  end
  plural || "#{singular}s"
end

.split_into_words(word) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/reality/naming.rb', line 128

def split_into_words(word)
  word = word.to_s.dup
  word.gsub!(/^[_-]/, '')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', '_')
  word.split('_')
end

.underscore(input_word) ⇒ Object



55
56
57
# File 'lib/reality/naming.rb', line 55

def underscore(input_word)
  split_into_words(input_word).join('_').downcase
end

.underscore?(word) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/reality/naming.rb', line 51

def underscore?(word)
  underscore(word) == word.to_s
end

.uppercase_constantize(word) ⇒ Object



87
88
89
# File 'lib/reality/naming.rb', line 87

def uppercase_constantize(word)
  underscore(word).upcase
end

.uppercase_constantize?(word) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/reality/naming.rb', line 83

def uppercase_constantize?(word)
  uppercase_constantize(word) == word.to_s
end

.xmlize(word) ⇒ Object



71
72
73
# File 'lib/reality/naming.rb', line 71

def xmlize(word)
  kebabcase(word)
end

.xmlize?(word) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/reality/naming.rb', line 67

def xmlize?(word)
  xmlize(word) == word.to_s
end