Class: Etna::Clients::Magma::UpdateAttributesFromCsvWorkflowSingleModel::Row

Inherits:
RowBase
  • Object
show all
Defined in:
lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RowBase

#nil_or_empty?, #stripped_value

Constructor Details

#initialize(raw, model_name, workflow) ⇒ Row

Returns a new instance of Row.



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 211

def initialize(raw, model_name, workflow)
  # Assumes CSV includes a column header to identify the attribute_name
  # Assumes index 0 is the record_name
  @raw = raw
  @model_name = model_name
  @workflow = workflow

  @record_name = @raw[0]
  raise "Invalid record name: \"#{record_name}\"." if nil_or_empty?(record_name)

  @record_name.strip!
end

Instance Attribute Details

#record_nameObject (readonly)

Returns the value of attribute record_name.



209
210
211
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 209

def record_name
  @record_name
end

Instance Method Details

#to_hObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 224

def to_h
  # Row can be converted to a hash, where keys are attribute_names and the
  #   values come from the CSV
  # {attribute_name: attribute_value}
  {}.tap do |attributes|
    row_hash = @raw.to_h
    row_keys = row_hash.keys
    row_keys[1..row_keys.length - 1].each do |attribute_name|
      raise "Invalid attribute name: \"#{attribute_name}\"." if nil_or_empty?(attribute_name)

      attribute_name_clean = attribute_name.strip
      raise "Invalid attribute \"#{attribute_name_clean}\" for model #{@model_name}." unless attribute = @workflow.find_attribute(@model_name, attribute_name_clean)

      attributes[attribute_name_clean] = stripped_value(@raw[attribute_name])
    end
  end
end