Module: Modifiers

Defined in:
lib/mods-eng-basic.rb

Class Method Summary collapse

Class Method Details

.baseEngModifiersObject


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mods-eng-basic.rb', line 25

def self.baseEngModifiers
    {
        "replace" => lambda do |s, parameters|
            return s.gsub(/#{Regexp.quote(parameters[0])}/, parameters[1])
        end,

        "capitalizeAll" => lambda do |s, parameters|
            return s.gsub(/\w+/) {|word| word.capitalize}
        end,
        
        "capitalize" => lambda do |s, parameters|
            return s.capitalize
        end,
        
        "a" => lambda do |s, parameters|
            if(s.length > 0) then
                if(s =~ /^u((\wi)|(\W))/) then
                    #catches "university" and "u-boat"
                    return "a #{s}"
                end
                
                if(isVowel(s[0])) then
                    return "an #{s}"
                end
            end
            
            return "a #{s}"
        end,

        "firstS" => lambda do |s, parameters|
            words = s.split(" ")
            if(words.length > 0) then
                words[0] = pluralize words[0]
            end
            return words.join " "
        end,
        
        "s" => lambda do |s, parameters|
            return pluralize(s)
        end,
        
        "ed" => lambda do |s, parameters|
            case(s[-1])
                when 's' then
                    return s + "ed"
                when 'e' then
                    return s + "d"
                when 'h' then
                    return s + "ed"
                when 'x' then
                    return s + "ed"
                when 'y' then
                    if(!isVowel(s[-2])) then
                        return s[0...-1] + "ied"
                    else
                        return s + "d"
                    end
                else
                    return s + "ed"
            end
        end
    }
end

.isVowel(c) ⇒ Object


2
3
4
# File 'lib/mods-eng-basic.rb', line 2

def self.isVowel(c)
    return ['a', 'e', 'i', 'o', 'u'].member?(c.downcase)
end

.pluralize(s) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mods-eng-basic.rb', line 6

def self.pluralize(s)
    case(s[-1])
    when 's' then
        return s + "es"
    when 'h' then
        return s + "es"
    when 'x' then
        return s + "es"
    when 'y' then
        if(!isVowel(s[-2])) then
            return s[0...-1] + "ies"
        else
            return s + "s"
        end
    else
        return s + "s"
    end
end