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.



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

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:



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

def formats
  @formats
end

Instance Method Details

#decode_data(**kwargs) ⇒ Object



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

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



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

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



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

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



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

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