Skip to content

Commit

Permalink
♻️ Extract superclass for (internal) command data
Browse files Browse the repository at this point in the history
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".
  • Loading branch information
nevans committed Dec 9, 2024
1 parent 7c1c6aa commit 839c1b7
Showing 1 changed file with 21 additions and 51 deletions.
72 changes: 21 additions & 51 deletions lib/net/imap/command_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "date"

require_relative "errors"
require_relative "data_lite"

module Net
class IMAP < Protocol
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 839c1b7

Please # to comment.