Skip to content

Commit ca72ac4

Browse files
committed
♻️ Extract superclass for (internal) command data
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".
1 parent 525577c commit ca72ac4

File tree

1 file changed

+21
-51
lines changed

1 file changed

+21
-51
lines changed

lib/net/imap/command_data.rb

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "date"
44

55
require_relative "errors"
6+
require_relative "data_lite"
67

78
module Net
89
class IMAP < Protocol
@@ -119,80 +120,53 @@ def send_symbol_data(symbol)
119120
put_string("\\" + symbol.to_s)
120121
end
121122

122-
class RawData # :nodoc:
123+
CommandData = Data.define(:data) do # :nodoc:
123124
def send_data(imap, tag)
124-
imap.__send__(:put_string, @data)
125+
raise NoMethodError, "#{self.class} must implement #{__method__}"
125126
end
126127

127128
def validate
128129
end
129-
130-
private
131-
132-
def initialize(data)
133-
@data = data
134-
end
135130
end
136131

137-
class Atom # :nodoc:
132+
class RawData < CommandData # :nodoc:
138133
def send_data(imap, tag)
139-
imap.__send__(:put_string, @data)
140-
end
141-
142-
def validate
143-
end
144-
145-
private
146-
147-
def initialize(data)
148-
@data = data
134+
imap.__send__(:put_string, data)
149135
end
150136
end
151137

152-
class QuotedString # :nodoc:
138+
class Atom < CommandData # :nodoc:
153139
def send_data(imap, tag)
154-
imap.__send__(:send_quoted_string, @data)
155-
end
156-
157-
def validate
158-
end
159-
160-
private
161-
162-
def initialize(data)
163-
@data = data
140+
imap.__send__(:put_string, data)
164141
end
165142
end
166143

167-
class Literal # :nodoc:
144+
class QuotedString < CommandData # :nodoc:
168145
def send_data(imap, tag)
169-
imap.__send__(:send_literal, @data, tag)
170-
end
171-
172-
def validate
146+
imap.__send__(:send_quoted_string, data)
173147
end
148+
end
174149

175-
private
176-
177-
def initialize(data)
178-
@data = data
150+
class Literal < CommandData # :nodoc:
151+
def send_data(imap, tag)
152+
imap.__send__(:send_literal, data, tag)
179153
end
180154
end
181155

182156
# *DEPRECATED*. Replaced by SequenceSet.
183-
class MessageSet # :nodoc:
157+
class MessageSet < CommandData # :nodoc:
184158
def send_data(imap, tag)
185-
imap.__send__(:put_string, format_internal(@data))
159+
imap.__send__(:put_string, format_internal(data))
186160
end
187161

188162
def validate
189-
validate_internal(@data)
163+
validate_internal(data)
190164
end
191165

192166
private
193167

194-
def initialize(data)
195-
@data = data
168+
def initialize(data:)
169+
super
196170
warn("DEPRECATED: #{MessageSet} should be replaced with #{SequenceSet}.",
197171
uplevel: 1, category: :deprecated)
198172
begin
@@ -246,22 +220,18 @@ def validate_internal(data)
246220
end
247221
end
248222

249-
class ClientID # :nodoc:
223+
class ClientID < CommandData # :nodoc:
250224

251225
def send_data(imap, tag)
252-
imap.__send__(:send_data, format_internal(@data), tag)
226+
imap.__send__(:send_data, format_internal(data), tag)
253227
end
254228

255229
def validate
256-
validate_internal(@data)
230+
validate_internal(data)
257231
end
258232

259233
private
260234

261-
def initialize(data)
262-
@data = data
263-
end
264-
265235
def validate_internal(client_id)
266236
client_id.to_h.each do |k,v|
267237
unless StringFormatter.valid_string?(k)

0 commit comments

Comments
 (0)