Class: Ruty::StandardFilters
- Inherits:
-
FilterCollection
- Object
- FilterCollection
- Ruty::StandardFilters
- Defined in:
- lib/ruty/filters.rb
Overview
builtin filter collection
Instance Method Summary collapse
-
#capitalize(context, value) ⇒ Object
capitalize a string.
-
#escape(context, value, attribute = false) ⇒ Object
xml escape a string.
-
#first(context, value) ⇒ Object
get the first item of an array.
-
#join(context, value, char = '') ⇒ Object
join an array with a string between the array elements.
-
#last(context, value) ⇒ Object
get the last item of an array.
-
#length(context, value) ⇒ Object
return the length of an object.
-
#lower(context, value) ⇒ Object
convert a string to lowercase.
-
#replace(context, value, search, repl = '') ⇒ Object
replace a substring with another if the replacement string isn’t given it replaces it with an empty value.
-
#reverse(context, value) ⇒ Object
reverse an item that supports reversing.
-
#sort(context, value) ⇒ Object
return a sorted version of an array or object that supports sorting.
-
#truncate(context, value, n = 80, ellipsis = '...') ⇒ Object
truncate a string down to n characters.
-
#upper(context, value) ⇒ Object
convert a string to uppercase.
-
#urlencode(context, value) ⇒ Object
urlencode an string.
Methods inherited from FilterCollection
Instance Method Details
#capitalize(context, value) ⇒ Object
capitalize a string
42 43 44 |
# File 'lib/ruty/filters.rb', line 42 def capitalize context, value value.to_s.capitalize end |
#escape(context, value, attribute = false) ⇒ Object
xml escape a string
119 120 121 122 123 124 125 |
# File 'lib/ruty/filters.rb', line 119 def escape context, value, attribute=false value = value.to_s.gsub(/&/, '&')\ .gsub(/>/, '>')\ .gsub(/</, '<') value.gsub!(/"/, '"') if attribute value end |
#first(context, value) ⇒ Object
get the first item of an array
97 98 99 100 101 102 103 104 105 |
# File 'lib/ruty/filters.rb', line 97 def first context, value if value.respond_to?(:first) value.first elsif value.respond_to?(:[]) value[0] || value else value end end |
#join(context, value, char = '') ⇒ Object
join an array with a string between the array elements
60 61 62 63 64 65 66 |
# File 'lib/ruty/filters.rb', line 60 def join context, value, char='' if value.respond_to?(:join) value.join(char) else value end end |
#last(context, value) ⇒ Object
get the last item of an array
108 109 110 111 112 113 114 115 116 |
# File 'lib/ruty/filters.rb', line 108 def last context, value if value.respond_to?(:last) value.last elsif value.respond_to?(:[]) value[-1] || value else value end end |
#length(context, value) ⇒ Object
return the length of an object
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/ruty/filters.rb', line 133 def length context, value if value.respond_to?(:size) value.size elsif value.respond_to?(:length) value.length elsif value.respond_to?(:count) value.count else 0 end end |
#lower(context, value) ⇒ Object
convert a string to lowercase
32 33 34 |
# File 'lib/ruty/filters.rb', line 32 def lower context, value value.to_s.downcase end |
#replace(context, value, search, repl = '') ⇒ Object
replace a substring with another if the replacement string isn’t given it replaces it with an empty value.
71 72 73 |
# File 'lib/ruty/filters.rb', line 71 def replace context, value, search, repl='' value.to_s.gsub(search.to_s, repl.to_s) end |
#reverse(context, value) ⇒ Object
reverse an item that supports reversing. else return the item unchanged
88 89 90 91 92 93 94 |
# File 'lib/ruty/filters.rb', line 88 def reverse context, value if value.respond_to?(:reverse) value.reverse else value end end |
#sort(context, value) ⇒ Object
return a sorted version of an array or object that supports sorting. If it does not this function returns the value unchanged.
78 79 80 81 82 83 84 |
# File 'lib/ruty/filters.rb', line 78 def sort context, value if value.respond_to?(:sort) value.sort else value end end |
#truncate(context, value, n = 80, ellipsis = '...') ⇒ Object
truncate a string down to n characters
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ruty/filters.rb', line 47 def truncate context, value, n=80, ellipsis='...' if value if (value = value.to_s).length > n value[0...n] + ellipsis else value end else '' end end |
#upper(context, value) ⇒ Object
convert a string to uppercase
37 38 39 |
# File 'lib/ruty/filters.rb', line 37 def upper context, value value.to_s.upcase end |
#urlencode(context, value) ⇒ Object
urlencode an string
128 129 130 |
# File 'lib/ruty/filters.rb', line 128 def urlencode context, value URI.escape(value.to_s) end |