Class: Etna::Cwl::RecordLoader

Inherits:
Loader
  • Object
show all
Defined in:
lib/etna/cwl.rb

Instance Method Summary collapse

Methods inherited from Loader

#as_array, #as_mapped_array, #map, #optional, #or

Constructor Details

#initialize(klass, field_loaders = nil) ⇒ RecordLoader

Returns a new instance of RecordLoader.



261
262
263
264
# File 'lib/etna/cwl.rb', line 261

def initialize(klass, field_loaders = nil)
  @klass = klass
  @field_loaders = field_loaders
end

Instance Method Details

#field_loadersObject



266
267
268
# File 'lib/etna/cwl.rb', line 266

def field_loaders
  @field_loaders || @klass::FIELD_LOADERS
end

#load(val) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/etna/cwl.rb', line 270

def load(val)
  unless val.is_a?(Hash)
    raise "Unexpected value #{val.inspect} for type #{@klass.name}"
  end

  errors = {}
  @klass.new({}.tap do |result|
    field_loaders.each do |field_sym, loader|
      field_str = field_sym.to_s
      begin
        result[field_str] = loader.load(val[field_str])
      rescue => e
        errors[field_str] = e.to_s
      end
    end

    unless errors.empty?
      raise errors.map { |k, e| "#{k}: #{e}" }.join(',')
    end
  end)
end