Решение на Четвърта задача от Милан Миланов

Обратно към всички решения

Към профила на Милан Миланов

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 25 успешни тест(а)
  • 16 неуспешни тест(а)

Код

module Validations
PHONE = /(0|((00|\+)[1-9]{1}\d{,2}))((( |\-){,2}\d){6,11})/
INTERNATIONAL_PHONE = /((00|\+)[1-9]{1}\d{,2})((( |\-){,2}\d){6,11})/
HOSTNAME = /((([a-zA-Z\d][a-zA-Z\d-]{,60}[a-zA-Z\d]|[a-zA-Z\d])\.)+([a-zA-Z]{2,3}(\.[a-zA-Z]{2})?))/
EMAIL_USERNAME = /([a-zA-Z\d][\w\+\.-]{,200})/
EMAIL = /#{EMAIL_USERNAME}@#{HOSTNAME}/
DATE = /((\d{4})-(0[1-9]|1[0-2])-(0[1-9]|1\d|2\d|3[01]))/
TIME = /((0\d|1\d|2[0-3]):(0\d|1\d|2\d|3\d|4\d|5\d):(0\d|1\d|2\d|3\d|4\d|5\d))/
DATE_TIME = /#{DATE}( |T)#{TIME}/
IP_ADDRESS = /(([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])\.){3}([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])/
NUMBER = /(-?([1-9][0-9]*|[1-9][0-9]*\.[1-9][0-9]*|0\.[1-9][0-9]*))/
INTEGER = /(-?([1-9][0-9]*|0))/
def self.test(regex, value)
regex.match(value).nil? ? false : true
end
[:email, :phone, :hostname, :ip_address, :number, :integer, :date, :time, :date_time].each do |validation_method|
define_singleton_method("#{validation_method}?") do |value|
test(/^#{const_get(validation_method.upcase)}$/, value)
end
end
end
class PrivacyFilter
include Validations
attr_writer :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@text = text
@preserve_phone_country_code, @preserve_email_hostname, @partially_preserve_email_username = false, false, false
end
def filtered
filter_phones(filter_emails @text)
end
private
def filter_emails(text)
text.gsub(Validations::EMAIL) do |email|
if @preserve_email_hostname then email.sub(/^.+?@/, '[FILTERED]@')
elsif @partially_preserve_email_username then preserve_username email
else '[EMAIL]'
end
end
end
def preserve_username(email)
username = /^(.+?)@/.match(email)[1]
username = (username.size < 6 ? '' : username[0..2]) + '[FILTERED]@'
email.sub(/^.+?@/, username)
end
def filter_phones(text)
text.gsub(Validations::PHONE) do |phone|
@preserve_phone_country_code ? preserve_country_code(phone) : '[PHONE]'
end
end
def preserve_country_code(phone)
phone = /^#{Validations::INTERNATIONAL_PHONE}$/.match phone
phone = phone.nil? ? '' : phone[1] + ' '
phone + '[FILTERED]'
end
end

Лог от изпълнението

...F....FF.FFF...F.FF.F.F.FF..F.....F...F

Failures:

  1) PrivacyFilter does not filter invalid emails
     Failure/Error: filter(text_with_invalid_emails).should eq text_with_invalid_emails
       
       expected: "Contact me here: _invalid@email.com"
            got: "Contact me here: _[EMAIL]"
       
       (compared using ==)
     # /tmp/d20130203-23049-1s23e3z/spec.rb:52:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1s23e3z/spec.rb:46:in `each'
     # /tmp/d20130203-23049-1s23e3z/spec.rb:46:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) PrivacyFilter filters more complex phone numbers
     Failure/Error: filter(text).should eq filtered
       
       expected: "[PHONE]"
            got: "+1 (555) 123-456-99"
       
       (compared using ==)
     # /tmp/d20130203-23049-1s23e3z/spec.rb:85:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1s23e3z/spec.rb:84:in `each'
     # /tmp/d20130203-23049-1s23e3z/spec.rb:84:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) PrivacyFilter does not filter invalid phone numbers
     Failure/Error: filter(text).should eq filtered
       
       expected: "Reach me at: 0885123"
            got: "Reach me at: [PHONE]"
       
       (compared using ==)
     # /tmp/d20130203-23049-1s23e3z/spec.rb:96:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1s23e3z/spec.rb:95:in `each'
     # /tmp/d20130203-23049-1s23e3z/spec.rb:95:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) PrivacyFilter filters more than one phone or email
     Failure/Error: filter(text).should eq filtered
       
       expected: "\n      Contacts\n\n      Phones: [PHONE] or [PHONE]\n      Email: [EMAIL] or [EMAIL]\n    "
            got: "\n      Contacts\n\n      Phones: +1 (555) 123-456-99 or [PHONE]\n      Email: [EMAIL] or [EMAIL]\n    "
       
       (compared using ==)
       
       Diff:
       @@ -1,7 +1,7 @@
        
              Contacts
        
       -      Phones: [PHONE] or [PHONE]
       +      Phones: +1 (555) 123-456-99 or [PHONE]
              Email: [EMAIL] or [EMAIL]
            
     # /tmp/d20130203-23049-1s23e3z/spec.rb:119:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) PrivacyFilter allows country code to be preserved for internationally-formatted phone numbers
     Failure/Error: filter.filtered.should eq filtered
       
       expected: "Phone: [PHONE]"
            got: "Phone: [FILTERED]"
       
       (compared using ==)
     # /tmp/d20130203-23049-1s23e3z/spec.rb:132:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1s23e3z/spec.rb:129:in `each'
     # /tmp/d20130203-23049-1s23e3z/spec.rb:129:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) PrivacyFilter separates preserved country code from filtered phone with a space
     Failure/Error: filter.filtered.should eq filtered
       
       expected: "Phone: 0025 [FILTERED]"
            got: "Phone: 0025 (55) 12 12255"
       
       (compared using ==)
     # /tmp/d20130203-23049-1s23e3z/spec.rb:144:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1s23e3z/spec.rb:141:in `each'
     # /tmp/d20130203-23049-1s23e3z/spec.rb:141:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) Validations does not break on emails in multiline strings
     Failure/Error: Validations.email?("foo@bar.com\nwat?").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1s23e3z/spec.rb:176:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) Validations can validate more complex phone numbers
     Failure/Error: Validations.phone?(phone).should be(valid)
       
       expected #<TrueClass:2> => true
            got #<FalseClass:0> => false
       
       Compared using equal?, which compares object identity,
       but expected and actual are not the same object. Use
       'actual.should eq(expected)' if you don't care about
       object identity in this example.
     # /tmp/d20130203-23049-1s23e3z/spec.rb:210:in `block (3 levels) in <top (required)>'
     # /tmp/d20130203-23049-1s23e3z/spec.rb:209:in `each'
     # /tmp/d20130203-23049-1s23e3z/spec.rb:209:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) Validations does not break on phones in multiline strings
     Failure/Error: Validations.phone?("0885123123\nwat?").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1s23e3z/spec.rb:215:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  10) Validations handles multiline strings in hostname validation properly
     Failure/Error: Validations.hostname?("foo.com\n").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1s23e3z/spec.rb:233:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  11) Validations handles multiline strings in IP validation properly
     Failure/Error: Validations.ip_address?("8.8.8.8\n").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1s23e3z/spec.rb:245:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  12) Validations validates more complex numbers
     Failure/Error: Validations.number?('0').should be_true
       expected: true value
            got: false
     # /tmp/d20130203-23049-1s23e3z/spec.rb:262:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) Validations handles multiline strings in numbers validation properly
     Failure/Error: Validations.number?("42\n24").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1s23e3z/spec.rb:274:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  14) Validations handles multiline strings in integer validation properly
     Failure/Error: Validations.number?("42\n24").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1s23e3z/spec.rb:295:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) Validations handles newlines in date validation
     Failure/Error: Validations.date?("2012-11-19\n").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1s23e3z/spec.rb:327:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  16) Validations handles newlines in time and datetime validation
     Failure/Error: Validations.time?("12:01:01\n").should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-1s23e3z/spec.rb:360:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.07234 seconds
41 examples, 16 failures

Failed examples:

rspec /tmp/d20130203-23049-1s23e3z/spec.rb:44 # PrivacyFilter does not filter invalid emails
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:76 # PrivacyFilter filters more complex phone numbers
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:89 # PrivacyFilter does not filter invalid phone numbers
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:104 # PrivacyFilter filters more than one phone or email
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:122 # PrivacyFilter allows country code to be preserved for internationally-formatted phone numbers
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:136 # PrivacyFilter separates preserved country code from filtered phone with a space
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:175 # Validations does not break on emails in multiline strings
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:184 # Validations can validate more complex phone numbers
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:214 # Validations does not break on phones in multiline strings
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:232 # Validations handles multiline strings in hostname validation properly
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:244 # Validations handles multiline strings in IP validation properly
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:257 # Validations validates more complex numbers
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:273 # Validations handles multiline strings in numbers validation properly
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:294 # Validations handles multiline strings in integer validation properly
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:326 # Validations handles newlines in date validation
rspec /tmp/d20130203-23049-1s23e3z/spec.rb:359 # Validations handles newlines in time and datetime validation

История (5 версии и 4 коментара)

Милан обнови решението на 24.11.2012 13:11 (преди почти 12 години)

+module Validations_regexps
+ def self.phone_regex
+ '(0|((00|\+)[1-9]{1}\d{,2}))((( |\-){,2}\d){6,11})'
+ end
+
+ def self.international_phone_regex
+ '((00|\+)[1-9]{1}\d{,2})((( |\-){,2}\d){6,11})'
+ end
+
+ def self.hostname_regex
+ '((([a-zA-Z\d][a-zA-Z\d-]{,60}[a-zA-Z\d]|[a-zA-Z\d])\.)+([a-zA-Z]{2,3}(\.[a-zA-Z]{2})?))'
+ end
+
+ def self.email_username_regex
+ '([a-zA-Z\d][\w\+\.-]{,200})'
+ end
+
+ def self.email_regex
+ self.email_username_regex + '@' + self.hostname_regex
+ end
+
+ def self.date_regex
+ '((\d{4})-(0[1-9]|1[0-2])-(0[1-9]|1\d|2\d|3[01]))'
+ end
+
+ def self.time_regex
+ '((0\d|1\d|2[0-3]):(0\d|1\d|2\d|3\d|4\d|5\d):(0\d|1\d|2\d|3\d|4\d|5\d))'
+ end
+
+ def self.date_time_regex
+ self.date_regex + '( |T)' + self.time_regex
+ end
+
+ def self.ip_address_regex
+ '(([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])\.){3}([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])'
+ end
+
+ def self.number_regex
+ '(-?([1-9][0-9]*|[1-9][0-9]*\.[1-9][0-9]*|0\.[1-9][0-9]*))'
+ end
+
+ def self.integer_regex
+ '(-?([1-9][0-9]*|0))'
+ end
+end
+
+module Validations
+ include Validations_regexps
+
+ def self.email?(value)
+ self.test(/^#{Validations_regexps.email_regex}$/, value)
+ end
+
+ def self.phone?(value)
+ self.test(/^#{Validations_regexps.phone_regex}$/, value)
+ end
+
+ def self.hostname?(value)
+ self.test(/^#{Validations_regexps.hostname_regex}$/, value)
+ end
+
+ def self.ip_address?(value)
+ self.test(/^#{Validations_regexps.ip_address_regex}$/, value)
+ end
+
+ def self.number?(value)
+ self.test(/^#{Validations_regexps.number_regex}$/, value)
+ end
+
+ def self.integer?(value)
+ self.test(/^#{Validations_regexps.integer_regex}$/, value)
+ end
+
+ def self.date?(value)
+ self.test(/^#{Validations_regexps.date_regex}$/, value)
+ end
+
+ def self.time?(value)
+ self.test(/^#{Validations_regexps.time_regex}$/, value)
+ end
+
+ def self.date_time?(value)
+ self.test(/^#{Validations_regexps.date_time_regex}$/, value)
+ end
+
+ def self.test(regex, value)
+ (regex =~ value).nil? ? false : true
+ end
+end
+
+class PrivacyFilter
+ include Validations_regexps
+ attr_writer :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
+
+ def initialize(text)
+ @text = text
+ @preserve_phone_country_code, @preserve_email_hostname, @partially_preserve_email_username = false, false, false
+ end
+
+ def filtered
+ filter_phones(filter_emails(@text))
+ end
+
+ private
+ def filter_phones(text)
+ text.gsub(/#{Validations_regexps.phone_regex}/) do |phone|
+ @preserve_phone_country_code ? preserve_country_code(phone) : '[PHONE]'
+ end
+ end
+
+ def filter_emails(text)
+ text.gsub(/#{Validations_regexps.email_regex}/) do |email|
+ if @preserve_email_hostname then preserve_hostname(email)
+ elsif @partially_preserve_email_username then preserve_username(email)
+ else '[EMAIL]'
+ end
+ end
+ end
+
+ def preserve_hostname(email)
+ email.sub(/^#{Validations_regexps.email_username_regex}/, '[FILTERED]')
+ end
+
+ def preserve_username(email)
+ email_parts = /^#{Validations_regexps.email_regex}/.match(email)
+ username, hostname = email_parts[1], email_parts[2]
+ username = (username.size < 6 ? '' : username[0, username.size / 2]) + '[FILTERED]'
+ email.replace(username + '@' + hostname)
+ end
+
+ def preserve_country_code(phone)
+ filtered_phone = /^#{Validations_regexps.international_phone_regex}$/.match(phone)
+ filtered_phone = filtered_phone.nil? ? '' : filtered_phone[1] + ' '
+ filtered_phone + '[FILTERED]'
+ end
+end
  • Можеш да интерполираш регулярни изрази в регулярни изрази
  • Ползвай константи за тези регулярни изрази, а не методи
  • Validations_regexps е супер невалидно име на константа :) ValidationsRegexps е най-малкото подобрение, което можеш да направиш с него :)
  • В кода ти като че ли има малко повторения, които биха могли да се избегнат

Милан обнови решението на 24.11.2012 20:35 (преди почти 12 години)

-module Validations_regexps
- def self.phone_regex
- '(0|((00|\+)[1-9]{1}\d{,2}))((( |\-){,2}\d){6,11})'
- end
-
- def self.international_phone_regex
- '((00|\+)[1-9]{1}\d{,2})((( |\-){,2}\d){6,11})'
- end
-
- def self.hostname_regex
- '((([a-zA-Z\d][a-zA-Z\d-]{,60}[a-zA-Z\d]|[a-zA-Z\d])\.)+([a-zA-Z]{2,3}(\.[a-zA-Z]{2})?))'
- end
-
- def self.email_username_regex
- '([a-zA-Z\d][\w\+\.-]{,200})'
- end
-
- def self.email_regex
- self.email_username_regex + '@' + self.hostname_regex
- end
-
- def self.date_regex
- '((\d{4})-(0[1-9]|1[0-2])-(0[1-9]|1\d|2\d|3[01]))'
- end
-
- def self.time_regex
- '((0\d|1\d|2[0-3]):(0\d|1\d|2\d|3\d|4\d|5\d):(0\d|1\d|2\d|3\d|4\d|5\d))'
- end
-
- def self.date_time_regex
- self.date_regex + '( |T)' + self.time_regex
- end
-
- def self.ip_address_regex
- '(([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])\.){3}([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])'
- end
-
- def self.number_regex
- '(-?([1-9][0-9]*|[1-9][0-9]*\.[1-9][0-9]*|0\.[1-9][0-9]*))'
- end
-
- def self.integer_regex
- '(-?([1-9][0-9]*|0))'
- end
-end
-
module Validations
- include Validations_regexps
+ PHONE = /(0|((00|\+)[1-9]{1}\d{,2}))((( |\-){,2}\d){6,11})/
+ INTERNATIONAL_PHONE = /((00|\+)[1-9]{1}\d{,2})((( |\-){,2}\d){6,11})/
+ HOSTNAME = /((([a-zA-Z\d][a-zA-Z\d-]{,60}[a-zA-Z\d]|[a-zA-Z\d])\.)+([a-zA-Z]{2,3}(\.[a-zA-Z]{2})?))/
+ EMAIL_USERNAME = /([a-zA-Z\d][\w\+\.-]{,200})/
+ EMAIL = /#{EMAIL_USERNAME}@#{HOSTNAME}/
+ DATE = /((\d{4})-(0[1-9]|1[0-2])-(0[1-9]|1\d|2\d|3[01]))/
+ TIME = /((0\d|1\d|2[0-3]):(0\d|1\d|2\d|3\d|4\d|5\d):(0\d|1\d|2\d|3\d|4\d|5\d))/
+ DATE_TIME = /#{DATE}( |T)#{TIME}/
+ IP_ADDRESS = /(([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])\.){3}([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])/
+ NUMBER = /(-?([1-9][0-9]*|[1-9][0-9]*\.[1-9][0-9]*|0\.[1-9][0-9]*))/
+ INTEGER = /(-?([1-9][0-9]*|0))/
- def self.email?(value)
- self.test(/^#{Validations_regexps.email_regex}$/, value)
+ def self.test(regex, value)
+ regex.match(value).nil? ? false : true
end
- def self.phone?(value)
- self.test(/^#{Validations_regexps.phone_regex}$/, value)
+ def self.validations(*methods)
+ methods.each do |validation_method|
+ define_singleton_method("#{validation_method}?") do |value|
+ test(/^#{const_get(validation_method.upcase)}$/, value)
+ end
+ end
end
- def self.hostname?(value)
- self.test(/^#{Validations_regexps.hostname_regex}$/, value)
- end
-
- def self.ip_address?(value)
- self.test(/^#{Validations_regexps.ip_address_regex}$/, value)
- end
-
- def self.number?(value)
- self.test(/^#{Validations_regexps.number_regex}$/, value)
- end
-
- def self.integer?(value)
- self.test(/^#{Validations_regexps.integer_regex}$/, value)
- end
-
- def self.date?(value)
- self.test(/^#{Validations_regexps.date_regex}$/, value)
- end
-
- def self.time?(value)
- self.test(/^#{Validations_regexps.time_regex}$/, value)
- end
-
- def self.date_time?(value)
- self.test(/^#{Validations_regexps.date_time_regex}$/, value)
- end
-
- def self.test(regex, value)
- (regex =~ value).nil? ? false : true
- end
+ validations :email, :phone, :hostname, :ip_address, :number, :integer, :date, :time, :date_time
end
class PrivacyFilter
- include Validations_regexps
+ include Validations
attr_writer :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@text = text
@preserve_phone_country_code, @preserve_email_hostname, @partially_preserve_email_username = false, false, false
end
def filtered
filter_phones(filter_emails(@text))
end
private
- def filter_phones(text)
- text.gsub(/#{Validations_regexps.phone_regex}/) do |phone|
- @preserve_phone_country_code ? preserve_country_code(phone) : '[PHONE]'
- end
- end
-
def filter_emails(text)
- text.gsub(/#{Validations_regexps.email_regex}/) do |email|
+ text.gsub(Validations::EMAIL) do |email|
if @preserve_email_hostname then preserve_hostname(email)
elsif @partially_preserve_email_username then preserve_username(email)
else '[EMAIL]'
end
end
end
def preserve_hostname(email)
- email.sub(/^#{Validations_regexps.email_username_regex}/, '[FILTERED]')
+ email.sub(/^.+?@/, '[FILTERED]@')
end
def preserve_username(email)
- email_parts = /^#{Validations_regexps.email_regex}/.match(email)
- username, hostname = email_parts[1], email_parts[2]
- username = (username.size < 6 ? '' : username[0, username.size / 2]) + '[FILTERED]'
- email.replace(username + '@' + hostname)
+ username = /^(.+?)@/.match(email)[1]
+ username = (username.size < 6 ? '' : username[0..2]) + '[FILTERED]@'
+ email.sub(/^.+?@/, username)
end
+ def filter_phones(text)
+ text.gsub(Validations::PHONE) do |phone|
+ @preserve_phone_country_code ? preserve_country_code(phone) : '[PHONE]'
+ end
+ end
+
def preserve_country_code(phone)
- filtered_phone = /^#{Validations_regexps.international_phone_regex}$/.match(phone)
- filtered_phone = filtered_phone.nil? ? '' : filtered_phone[1] + ' '
- filtered_phone + '[FILTERED]'
+ phone = /^#{Validations::INTERNATIONAL_PHONE}$/.match(phone)
+ phone = phone.nil? ? '' : phone[1] + ' '
+ phone + '[FILTERED]'
end
end

Сега кодът ти е по-добре.

Аз бих го написал така, ако искам да ползвам метапрограмиране, тъй като не очаквам отвън някой да вика Validations.validations:

[:email, :phone, :hostname, :ip_address, :number, :integer, :date, :time, :date_time].each do |validation_method|
  define_singleton_method("#{validation_method}?") do |value|
     test(/^#{const_get(validation_method.upcase)}$/, value)
  end
end

И в моето решение на тази задача, умишлено не съм използвал метапрограмиране, защото не сме говорили за него, но знам, че човек има нужда да си начеше крастата от време на време... :)

Милан обнови решението на 26.11.2012 14:31 (преди почти 12 години)

module Validations
PHONE = /(0|((00|\+)[1-9]{1}\d{,2}))((( |\-){,2}\d){6,11})/
INTERNATIONAL_PHONE = /((00|\+)[1-9]{1}\d{,2})((( |\-){,2}\d){6,11})/
HOSTNAME = /((([a-zA-Z\d][a-zA-Z\d-]{,60}[a-zA-Z\d]|[a-zA-Z\d])\.)+([a-zA-Z]{2,3}(\.[a-zA-Z]{2})?))/
EMAIL_USERNAME = /([a-zA-Z\d][\w\+\.-]{,200})/
EMAIL = /#{EMAIL_USERNAME}@#{HOSTNAME}/
DATE = /((\d{4})-(0[1-9]|1[0-2])-(0[1-9]|1\d|2\d|3[01]))/
TIME = /((0\d|1\d|2[0-3]):(0\d|1\d|2\d|3\d|4\d|5\d):(0\d|1\d|2\d|3\d|4\d|5\d))/
DATE_TIME = /#{DATE}( |T)#{TIME}/
IP_ADDRESS = /(([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])\.){3}([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])/
NUMBER = /(-?([1-9][0-9]*|[1-9][0-9]*\.[1-9][0-9]*|0\.[1-9][0-9]*))/
INTEGER = /(-?([1-9][0-9]*|0))/
def self.test(regex, value)
regex.match(value).nil? ? false : true
end
- def self.validations(*methods)
- methods.each do |validation_method|
- define_singleton_method("#{validation_method}?") do |value|
- test(/^#{const_get(validation_method.upcase)}$/, value)
- end
- end
+ [:email, :phone, :hostname, :ip_address, :number, :integer, :date, :time, :date_time].each do |validation_method|
+ define_singleton_method("#{validation_method}?") do |value|
+ test(/^#{const_get(validation_method.upcase)}$/, value)
+ end
end
-
- validations :email, :phone, :hostname, :ip_address, :number, :integer, :date, :time, :date_time
end
class PrivacyFilter
include Validations
attr_writer :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@text = text
@preserve_phone_country_code, @preserve_email_hostname, @partially_preserve_email_username = false, false, false
end
def filtered
filter_phones(filter_emails(@text))
end
private
def filter_emails(text)
text.gsub(Validations::EMAIL) do |email|
if @preserve_email_hostname then preserve_hostname(email)
elsif @partially_preserve_email_username then preserve_username(email)
else '[EMAIL]'
end
end
end
def preserve_hostname(email)
email.sub(/^.+?@/, '[FILTERED]@')
end
def preserve_username(email)
username = /^(.+?)@/.match(email)[1]
username = (username.size < 6 ? '' : username[0..2]) + '[FILTERED]@'
email.sub(/^.+?@/, username)
end
def filter_phones(text)
text.gsub(Validations::PHONE) do |phone|
@preserve_phone_country_code ? preserve_country_code(phone) : '[PHONE]'
end
end
def preserve_country_code(phone)
phone = /^#{Validations::INTERNATIONAL_PHONE}$/.match(phone)
phone = phone.nil? ? '' : phone[1] + ' '
phone + '[FILTERED]'
end
end

Милан обнови решението на 26.11.2012 14:38 (преди почти 12 години)

module Validations
PHONE = /(0|((00|\+)[1-9]{1}\d{,2}))((( |\-){,2}\d){6,11})/
INTERNATIONAL_PHONE = /((00|\+)[1-9]{1}\d{,2})((( |\-){,2}\d){6,11})/
HOSTNAME = /((([a-zA-Z\d][a-zA-Z\d-]{,60}[a-zA-Z\d]|[a-zA-Z\d])\.)+([a-zA-Z]{2,3}(\.[a-zA-Z]{2})?))/
EMAIL_USERNAME = /([a-zA-Z\d][\w\+\.-]{,200})/
EMAIL = /#{EMAIL_USERNAME}@#{HOSTNAME}/
DATE = /((\d{4})-(0[1-9]|1[0-2])-(0[1-9]|1\d|2\d|3[01]))/
TIME = /((0\d|1\d|2[0-3]):(0\d|1\d|2\d|3\d|4\d|5\d):(0\d|1\d|2\d|3\d|4\d|5\d))/
DATE_TIME = /#{DATE}( |T)#{TIME}/
IP_ADDRESS = /(([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])\.){3}([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])/
NUMBER = /(-?([1-9][0-9]*|[1-9][0-9]*\.[1-9][0-9]*|0\.[1-9][0-9]*))/
INTEGER = /(-?([1-9][0-9]*|0))/
def self.test(regex, value)
regex.match(value).nil? ? false : true
end
[:email, :phone, :hostname, :ip_address, :number, :integer, :date, :time, :date_time].each do |validation_method|
define_singleton_method("#{validation_method}?") do |value|
test(/^#{const_get(validation_method.upcase)}$/, value)
end
end
end
class PrivacyFilter
include Validations
attr_writer :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@text = text
@preserve_phone_country_code, @preserve_email_hostname, @partially_preserve_email_username = false, false, false
end
def filtered
filter_phones(filter_emails(@text))
end
private
def filter_emails(text)
text.gsub(Validations::EMAIL) do |email|
- if @preserve_email_hostname then preserve_hostname(email)
+ if @preserve_email_hostname then email.sub(/^.+?@/, '[FILTERED]@')
elsif @partially_preserve_email_username then preserve_username(email)
else '[EMAIL]'
end
end
- end
-
- def preserve_hostname(email)
- email.sub(/^.+?@/, '[FILTERED]@')
end
def preserve_username(email)
username = /^(.+?)@/.match(email)[1]
username = (username.size < 6 ? '' : username[0..2]) + '[FILTERED]@'
email.sub(/^.+?@/, username)
end
def filter_phones(text)
text.gsub(Validations::PHONE) do |phone|
@preserve_phone_country_code ? preserve_country_code(phone) : '[PHONE]'
end
end
def preserve_country_code(phone)
phone = /^#{Validations::INTERNATIONAL_PHONE}$/.match(phone)
phone = phone.nil? ? '' : phone[1] + ' '
phone + '[FILTERED]'
end
end

Милан обнови решението на 26.11.2012 14:40 (преди почти 12 години)

module Validations
PHONE = /(0|((00|\+)[1-9]{1}\d{,2}))((( |\-){,2}\d){6,11})/
INTERNATIONAL_PHONE = /((00|\+)[1-9]{1}\d{,2})((( |\-){,2}\d){6,11})/
HOSTNAME = /((([a-zA-Z\d][a-zA-Z\d-]{,60}[a-zA-Z\d]|[a-zA-Z\d])\.)+([a-zA-Z]{2,3}(\.[a-zA-Z]{2})?))/
EMAIL_USERNAME = /([a-zA-Z\d][\w\+\.-]{,200})/
EMAIL = /#{EMAIL_USERNAME}@#{HOSTNAME}/
DATE = /((\d{4})-(0[1-9]|1[0-2])-(0[1-9]|1\d|2\d|3[01]))/
TIME = /((0\d|1\d|2[0-3]):(0\d|1\d|2\d|3\d|4\d|5\d):(0\d|1\d|2\d|3\d|4\d|5\d))/
DATE_TIME = /#{DATE}( |T)#{TIME}/
IP_ADDRESS = /(([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])\.){3}([\d]|[1-9][\d]|1[\d]{2}|2[0-4][\d]|25[0-5])/
NUMBER = /(-?([1-9][0-9]*|[1-9][0-9]*\.[1-9][0-9]*|0\.[1-9][0-9]*))/
INTEGER = /(-?([1-9][0-9]*|0))/
def self.test(regex, value)
regex.match(value).nil? ? false : true
end
[:email, :phone, :hostname, :ip_address, :number, :integer, :date, :time, :date_time].each do |validation_method|
define_singleton_method("#{validation_method}?") do |value|
test(/^#{const_get(validation_method.upcase)}$/, value)
end
end
end
class PrivacyFilter
include Validations
attr_writer :preserve_phone_country_code, :preserve_email_hostname, :partially_preserve_email_username
def initialize(text)
@text = text
@preserve_phone_country_code, @preserve_email_hostname, @partially_preserve_email_username = false, false, false
end
def filtered
- filter_phones(filter_emails(@text))
+ filter_phones(filter_emails @text)
end
private
def filter_emails(text)
text.gsub(Validations::EMAIL) do |email|
if @preserve_email_hostname then email.sub(/^.+?@/, '[FILTERED]@')
- elsif @partially_preserve_email_username then preserve_username(email)
+ elsif @partially_preserve_email_username then preserve_username email
else '[EMAIL]'
end
end
end
def preserve_username(email)
username = /^(.+?)@/.match(email)[1]
username = (username.size < 6 ? '' : username[0..2]) + '[FILTERED]@'
email.sub(/^.+?@/, username)
end
def filter_phones(text)
text.gsub(Validations::PHONE) do |phone|
@preserve_phone_country_code ? preserve_country_code(phone) : '[PHONE]'
end
end
def preserve_country_code(phone)
- phone = /^#{Validations::INTERNATIONAL_PHONE}$/.match(phone)
+ phone = /^#{Validations::INTERNATIONAL_PHONE}$/.match phone
phone = phone.nil? ? '' : phone[1] + ' '
phone + '[FILTERED]'
end
end