Class: CloudEvents::Format::Multi

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_events/format.rb

Overview

A convenience formatter that checks multiple formats for one capable of handling the given input.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formats = [], &result_checker) ⇒ Multi

Create a multi format.

Parameters:

  • formats (Array<Format>) (defaults to: [])

    An array of formats to check in order

  • result_checker (Proc)

    An optional block that determines whether a result provided by a format is acceptable. The block takes the format's result as an argument, and returns either the result to indicate acceptability, or nil to indicate not.



216
217
218
219
# File 'lib/cloud_events/format.rb', line 216

def initialize formats = [], &result_checker
  @formats = formats
  @result_checker = result_checker
end

Instance Attribute Details

#formatsArray<Format> (readonly)

The formats to check, in order.

Returns:



226
227
228
# File 'lib/cloud_events/format.rb', line 226

def formats
  @formats
end

Instance Method Details

#decode_data(**kwargs) ⇒ Object



255
256
257
258
259
260
261
262
# File 'lib/cloud_events/format.rb', line 255

def decode_data **kwargs
  @formats.each do |elem|
    result = elem.decode_data(**kwargs)
    result = @result_checker.call result if @result_checker
    return result if result
  end
  nil
end

#decode_event(**kwargs) ⇒ Object



231
232
233
234
235
236
237
238
# File 'lib/cloud_events/format.rb', line 231

def decode_event **kwargs
  @formats.each do |elem|
    result = elem.decode_event(**kwargs)
    result = @result_checker.call result if @result_checker
    return result if result
  end
  nil
end

#encode_data(**kwargs) ⇒ Object



267
268
269
270
271
272
273
274
# File 'lib/cloud_events/format.rb', line 267

def encode_data **kwargs
  @formats.each do |elem|
    result = elem.encode_data(**kwargs)
    result = @result_checker.call result if @result_checker
    return result if result
  end
  nil
end

#encode_event(**kwargs) ⇒ Object



243
244
245
246
247
248
249
250
# File 'lib/cloud_events/format.rb', line 243

def encode_event **kwargs
  @formats.each do |elem|
    result = elem.encode_event(**kwargs)
    result = @result_checker.call result if @result_checker
    return result if result
  end
  nil
end