Module: Slick::Util

Defined in:
lib/slick/util.rb

Defined Under Namespace

Modules: Inflections Classes: ParamsHash

Class Method Summary collapse

Class Method Details

.html_escape(stringable) ⇒ Object



24
25
26
# File 'lib/slick/util.rb', line 24

def html_escape(stringable)
    CGI::escapeHTML(stringable.to_s)
end

.html_unescape(stringable) ⇒ Object



28
29
30
# File 'lib/slick/util.rb', line 28

def html_unescape(stringable)
    CGI::unescapeHTML(stringable.to_s)
end

.paramify(hashable) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/slick/util.rb', line 40

def paramify(hashable)
    if hashable.kind_of?(Hash)
        out = ParamsHash.new
        hashable.each{|name, value| out[name] = paramify(value)}
        out
    elsif hashable.kind_of?(Array)
        out = ParamsHash.new
        hashable.each_with_index{|value, index| out[index] = paramify(value)}
        out
    else
        return hashable
    end
end

.pluralize(word) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/slick/util.rb', line 8

def pluralize(word)
    word = word.to_s
    Inflections.pluralize_rules.each do |rule|
        pattern, replacement = rule
        return word.sub(pattern, replacement) if word.match?(pattern)
    end
end

.singularize(word) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/slick/util.rb', line 16

def singularize(word)
    word = word.to_s
    Inflections.singularize_rules.each do |rule|
        pattern, replacement = rule
        return word.sub(pattern, replacement) if word.match?(pattern)
    end
end

.url_encode(hashable, path = [], out = []) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/slick/util.rb', line 54

def url_encode(hashable, path = [], out = [])
    hashable = paramify(hashable) if !hashable.kind_of?(ParamsHash)
    if hashable.kind_of?(ParamsHash)
        hashable.each do |name, value|
            path.push(name)
            url_encode(value, path, out)
            path.pop
        end
        return out.join('&') if path.count == 0
    elsif path.count == 0
        return url_escape(hashable)
    else 
        out << "#{url_escape(path.first)}#{path[1..path.length].map{|name| "[#{url_escape(name)}]"}.join}=#{url_escape(hashable)}"
    end
end

.url_escape(stringable) ⇒ Object



32
33
34
# File 'lib/slick/util.rb', line 32

def url_escape(stringable)
    CGI::escape(stringable.to_s)
end

.url_unescape(stringable) ⇒ Object



36
37
38
# File 'lib/slick/util.rb', line 36

def url_unescape(stringable)
    CGI::unescape(stringable.to_s)
end