lib/mayaml/mail_account/port_validator.rb in mayaml-3.0.1 vs lib/mayaml/mail_account/port_validator.rb in mayaml-4.0.0

- old
+ new

@@ -1,9 +1,8 @@ -# encoding: utf-8 # frozen_string_literal: true -# Copyright (C) 2016 Szymon Kopciewski +# Copyright (C) 2017 Szymon Kopciewski # # This file is part of Mayaml. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -19,20 +18,27 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. module Mayaml class MailAccount class PortValidator - attr_reader :errors - def initialize(port) - @errors = [] - port = port.respond_to?(:to_i) ? port.to_i : 0 - @errors << "Mail account port is invalid." if port == 0 - @errors << "Mail account port could not be negative." if port < 0 + port = conver_port(port) + errors << "Mail account port is invalid." if port.zero? + errors << "Mail account port could not be negative." if port.negative? end def valid? - @errors.empty? + errors.empty? + end + + def errors + @errors ||= [] + end + + private + + def conver_port(port) + port.respond_to?(:to_i) ? port.to_i : 0 end end end end