Module: Mbrao::Validations::ClassMethods
- Defined in:
- lib/mbrao/parser.rb
Overview
Class methods.
Instance Method Summary (collapse)
-
- (Boolean) is_email?(text)
Checks if the text is a valid email.
-
- (Boolean) is_url?(text)
Checks if the text is a valid URL.
-
- (Array) sanitized_array(object, uniq = true, compact = false, sanitize_method = :ensure_string, &block)
Converts an object to a a flatten array with all values sanitized.
-
- (Object) sanitized_hash(object, sanitize_method = nil, &block)
Converts an object making sure that every
Hash
is converted to aHashWithIndifferentAccess
.
Instance Method Details
- (Boolean) is_email?(text)
Checks if the text is a valid email.
137 138 139 140 |
# File 'lib/mbrao/parser.rb', line 137 def is_email?(text) regex = /^([a-z0-9_\.\-\+]+)@([\da-z\.\-]+)\.([a-z\.]{2,6})$/i text.ensure_string.strip =~ regex end |
- (Boolean) is_url?(text)
Checks if the text is a valid URL.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/mbrao/parser.rb', line 146 def is_url?(text) regex = / ^( ([a-z0-9\-]+:\/\/) #PROTOCOL (([\w-]+\.)?) # LOWEST TLD ([\w-]+) # 2nd LEVEL TLD (\.[a-z]+) # TOP TLD ((:\d+)?) # PORT ([\S|\?]*) # PATH, QUERYSTRING AND FRAGMENT )$ /ix text.ensure_string.strip =~ regex end |
- (Array) sanitized_array(object, uniq = true, compact = false, sanitize_method = :ensure_string, &block)
Converts an object to a a flatten array with all values sanitized.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/mbrao/parser.rb', line 188 def sanitized_array(object, uniq = true, compact = false, sanitize_method = :ensure_string, &block) rv = object.ensure_array.flatten rv.uniq! if uniq rv.compact! if compact if block then rv = rv.collect(&block) elsif sanitize_method then rv = rv.collect(&sanitize_method) end rv.uniq! if uniq rv.compact! if compact rv end |
- (Object) sanitized_hash(object, sanitize_method = nil, &block)
Converts an object making sure that every Hash
is converted to a HashWithIndifferentAccess
.
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/mbrao/parser.rb', line 167 def sanitized_hash(object, sanitize_method = nil, &block) if object.is_a?(Hash) || object.is_a?(HashWithIndifferentAccess) then object.inject(HashWithIndifferentAccess.new) do |hash, pair| hash[pair[0]] = Mbrao::Parser.sanitized_hash(pair[1], sanitize_method, &block) hash end elsif object.respond_to?(:collect) then object.collect {|item| Mbrao::Parser.sanitized_hash(item, sanitize_method, &block) } else sanitized_hash_entry(object, sanitize_method, &block) end end |