Class: WWW_App::Clean
- Inherits:
-
Object
- Object
- WWW_App::Clean
- Defined in:
- lib/www_app/Clean.rb
Constant Summary collapse
- VALID_URL_REGEXP =
/\A[a-z0-9\_\-\:\/\?\&\(\)\@\.]{1,200}\Z/i- VALID_FONT_REGEXP =
HTML-specific =====================================================
/\A[a-z0-9\-\_\ ]{1,100}\Z/i
Instance Attribute Summary collapse
-
#actual ⇒ Object
readonly
Returns the value of attribute actual.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#origin ⇒ Object
readonly
Returns the value of attribute origin.
Instance Method Summary collapse
-
#clean_as(*args) ⇒ Object
Examples:.
- #color ⇒ Object
- #downcase ⇒ Object
- #fonts ⇒ Object
- #in(*raw) ⇒ Object
-
#initialize(name, val) ⇒ Clean
constructor
A new instance of Clean.
- #map(action, *args) ⇒ Object
- #match(regex, msg = nil) ⇒ Object
- #max_length(max, msg = nil) ⇒ Object
- #not_empty_string ⇒ Object
- #not_nil ⇒ Object
- #number ⇒ Object
- #number_between(min, max) ⇒ Object
- #string ⇒ Object
- #switch(choices) ⇒ Object
- #upcase ⇒ Object
- #update(val) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(name, val) ⇒ Clean
Returns a new instance of Clean.
7 8 9 10 11 |
# File 'lib/www_app/Clean.rb', line 7 def initialize name, val @name = name[" "] ? name : name.inspect @origin = val @actual = val end |
Instance Attribute Details
#actual ⇒ Object (readonly)
Returns the value of attribute actual.
5 6 7 |
# File 'lib/www_app/Clean.rb', line 5 def actual @actual end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/www_app/Clean.rb', line 5 def name @name end |
#origin ⇒ Object (readonly)
Returns the value of attribute origin.
5 6 7 |
# File 'lib/www_app/Clean.rb', line 5 def origin @origin end |
Instance Method Details
#clean_as(*args) ⇒ Object
Examples:
clean_as :upcase, :string, :in, [1,2,3]
clean_as :upcase, :string, :switch, {...}
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/www_app/Clean.rb', line 23 def clean_as *args begin cleaner = args.shift case when args.first.is_a?(Array) send cleaner, *(args.shift) when args.first.is_a?(Hash) send cleaner, args.shift else send cleaner end end while !args.empty? self end |
#color ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/www_app/Clean.rb', line 81 def color not_empty_string if !(actual =~ /\A#[A-Z0-9]{3,10}\Z/i) fail "Invalid: color for #{name}: #{origin.inspect}." end self end |
#downcase ⇒ Object
67 68 69 70 71 72 |
# File 'lib/www_app/Clean.rb', line 67 def downcase not_empty_string update actual.downcase self end |
#fonts ⇒ Object
159 160 161 162 163 164 |
# File 'lib/www_app/Clean.rb', line 159 def fonts map :not_empty_string map :match, VALID_FONT_REGEXP, "only allow 1-100 characters: letters, numbers, spaces, - _" self end |
#in(*raw) ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/www_app/Clean.rb', line 118 def in *raw choices = raw.flatten if !choices.include?(actual) fail "Invalid: #{name} can't be, #{actual.inspect}, but one of: #{choices.join ", "}" end self end |
#map(action, *args) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/www_app/Clean.rb', line 134 def map action, *args update( actual.map { |v| Clean.new("#{name} value", v). send(action, *args). actual } ) self end |
#match(regex, msg = nil) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/www_app/Clean.rb', line 99 def match regex, msg = nil string if !(actual =~ regex) fail(msg || "Invalid: #{name} has invalid chars") end self end |
#max_length(max, msg = nil) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/www_app/Clean.rb', line 90 def max_length max, msg = nil not_nil if actual.length > 200 fail(msg || "#{name} can not be more than #{max}") end self end |
#not_empty_string ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/www_app/Clean.rb', line 57 def not_empty_string string update actual.strip if actual.empty? fail "Invalid: #{name} must not be empty." end self end |
#not_nil ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/www_app/Clean.rb', line 39 def not_nil if actual.nil? fail "Invalid: #{name} is required." end self end |
#number ⇒ Object
52 53 54 55 |
# File 'lib/www_app/Clean.rb', line 52 def number return self if actual.is_a?(Numeric) fail "Invalid: #{name} must be a Number: #{actual.inspect}" end |
#number_between(min, max) ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/www_app/Clean.rb', line 146 def number_between min, max number if actual < min || actual > max fail "Invalid: #{name}, #{actual.inspect}, must be between: #{min} and #{max}" end self end |
#string ⇒ Object
47 48 49 50 |
# File 'lib/www_app/Clean.rb', line 47 def string return self if actual.is_a?(String) fail "Invalid: #{name} must be a String: #{actual.inspect}" end |
#switch(choices) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/www_app/Clean.rb', line 127 def switch choices self.in(choices.keys) update choices[actual] self end |
#upcase ⇒ Object
74 75 76 77 78 79 |
# File 'lib/www_app/Clean.rb', line 74 def upcase not_empty_string update actual.upcase self end |
#update(val) ⇒ Object
13 14 15 |
# File 'lib/www_app/Clean.rb', line 13 def update val @actual = val end |
#url ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/www_app/Clean.rb', line 109 def url max = 200 not_empty_string max_length max, "#{name} needs to be #{max} or less chars." match VALID_URL_REGEXP self end |