Class: LocalModel::Functions

Inherits:
Object
  • Object
show all
Defined in:
lib/local_model/helpers/functions.rb

Class Method Summary collapse

Class Method Details

.camel_to_snake(camel) ⇒ Object



9
10
11
# File 'lib/local_model/helpers/functions.rb', line 9

def self.camel_to_snake(camel)
  camel.to_s.split(/(?=[A-Z])/).map(&:downcase).join("_")
end

.pluralize(word) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/local_model/helpers/functions.rb', line 36

def self.pluralize(word)
  word = word.to_s
  return LocalModel::PluralizedWords::IRREGULAR_PLURALIZED_WORDS[word] if LocalModel::PluralizedWords::IRREGULAR_PLURALIZED_WORDS[word]
  if word[-2..-1] == "us"
    "#{word[0...-2]}i"
  elsif word[-2..-1] == "on"
    "#{word[0...-2]}a"
  elsif word =~ /s$|ss$|sh$|ch$|x$|z$|o$/
    "#{word}es"
  elsif word =~ /f$|fe$/
    word[-1] == "f" ? word[-1] = "v" : word[-2] = "v"
    word[-1] == "e" ? "#{word}s" : "#{word}es"
  elsif word =~ /[^aeiou]y$/
    "#{word[0...-1]}ies"
  elsif word[-2..-1] == "is"
    "#{word[0...-2]}es"
  else
    "#{word}s"
  end
end

.singularize(word) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/local_model/helpers/functions.rb', line 13

def self.singularize(word)
  word = word.to_s
  return LocalModel::PluralizedWords::IRREGULAR_SINGULARIZED_WORDS[word] if LocalModel::PluralizedWords::IRREGULAR_SINGULARIZED_WORDS[word]
  if word[-1] == "i"
    "#{word[0...-1]}us"
  elsif word[-1] == "a"
    "#{word[0..-1]}on"
  elsif word =~ /ies$/
    "#{word[0...-3]}y"
  elsif word =~ /es$/
    if word[-3] == "v"
      word[-3] = "f"
      word[0...-2]
    elsif word[0...-2] =~ /s$|ss$|sh$|ch$|x$|z$|o$/
      word[0...-2]
    else
      "#{word[0...-1]}"
    end
  else
    "#{word[0...-1]}"
  end
end

.snake_to_camel(snakecase) ⇒ Object



3
4
5
6
7
# File 'lib/local_model/helpers/functions.rb', line 3

def self.snake_to_camel(snakecase)
  snakecase.to_s.split("_").map.with_index do |word, i|
    i == 0 ? word : word.capitalize
  end.join('')
end