From e3444542db6e3aeb72c7abe441ede77f352d4bce Mon Sep 17 00:00:00 2001 From: nicholas evans Date: Tue, 7 May 2024 16:59:53 -0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Extract=20superclass=20for?= =?UTF-8?q?=20(internal)=20command=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new `CommandData` superclass uses `Data` to add the pattern matching and equality methods while also simplifying the implementation. Specifically, I wanted RawData#deconstruct for the basic `ESEARCH` support branch. It seemed reasonable to apply the same change to all of the internal command data classes. Please note: this does change these objects to be frozen. However, these classes are explicitly undocumented and considered "internal", so this will _not_ be treated as a "breaking change". --- lib/net/imap/command_data.rb | 72 +++++++++++------------------------- 1 file changed, 21 insertions(+), 51 deletions(-) diff --git a/lib/net/imap/command_data.rb b/lib/net/imap/command_data.rb index 71fd4d65..2b3a05b4 100644 --- a/lib/net/imap/command_data.rb +++ b/lib/net/imap/command_data.rb @@ -3,6 +3,7 @@ require "date" require_relative "errors" +require_relative "data_lite" module Net class IMAP < Protocol @@ -119,80 +120,53 @@ def send_symbol_data(symbol) put_string("\\" + symbol.to_s) end - class RawData # :nodoc: + CommandData = Data.define(:data) do # :nodoc: def send_data(imap, tag) - imap.__send__(:put_string, @data) + raise NoMethodError, "#{self.class} must implement #{__method__}" end def validate end - - private - - def initialize(data) - @data = data - end end - class Atom # :nodoc: + class RawData < CommandData # :nodoc: def send_data(imap, tag) - imap.__send__(:put_string, @data) - end - - def validate - end - - private - - def initialize(data) - @data = data + imap.__send__(:put_string, data) end end - class QuotedString # :nodoc: + class Atom < CommandData # :nodoc: def send_data(imap, tag) - imap.__send__(:send_quoted_string, @data) - end - - def validate - end - - private - - def initialize(data) - @data = data + imap.__send__(:put_string, data) end end - class Literal # :nodoc: + class QuotedString < CommandData # :nodoc: def send_data(imap, tag) - imap.__send__(:send_literal, @data, tag) - end - - def validate + imap.__send__(:send_quoted_string, data) end + end - private - - def initialize(data) - @data = data + class Literal < CommandData # :nodoc: + def send_data(imap, tag) + imap.__send__(:send_literal, data, tag) end end # *DEPRECATED*. Replaced by SequenceSet. - class MessageSet # :nodoc: + class MessageSet < CommandData # :nodoc: def send_data(imap, tag) - imap.__send__(:put_string, format_internal(@data)) + imap.__send__(:put_string, format_internal(data)) end def validate - validate_internal(@data) + validate_internal(data) end private - def initialize(data) - @data = data + def initialize(data:) + super warn("DEPRECATED: #{MessageSet} should be replaced with #{SequenceSet}.", uplevel: 1, category: :deprecated) begin @@ -246,22 +220,18 @@ def validate_internal(data) end end - class ClientID # :nodoc: + class ClientID < CommandData # :nodoc: def send_data(imap, tag) - imap.__send__(:send_data, format_internal(@data), tag) + imap.__send__(:send_data, format_internal(data), tag) end def validate - validate_internal(@data) + validate_internal(data) end private - def initialize(data) - @data = data - end - def validate_internal(client_id) client_id.to_h.each do |k,v| unless StringFormatter.valid_string?(k)