Class: Etna::Clients::Magma::UpdateAttributesFromCsvWorkflowMultiModel::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, workflow) ⇒ Row

Returns a new instance of Row.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 77

def initialize(raw, workflow)
  # Assumes rows are in pairs, where
  #   [0] = model_name
  #   [1] = record_name / identifier
  #   [2], [4], etc. = attribute_name
  #   [3], [5], etc. = attribute value
  # So not every row needs the same number of columns
  @raw = raw
  @workflow = workflow

  raise "Invalid revision row #{@raw}. Must include at least 4 column values (model,record_name,attribute_name,attribute_value)." if @raw.length < 4
  raise "Invalid revision row #{@raw}. Must have an even number of columns." if @raw.length.odd?

  @model_name = raw[0]

  raise "Invalid model name: \"#{@model_name}\"." if nil_or_empty?(@model_name)

  @model_name.strip!

  @record_name = raw[1]

  raise "Invalid record name: \"#{@record_name}\"." if nil_or_empty?(@record_name)

  @record_name.strip!
end

Instance Attribute Details

#model_nameObject (readonly)

Returns the value of attribute model_name.



76
77
78
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 76

def model_name
  @model_name
end

#record_nameObject (readonly)

Returns the value of attribute record_name.



76
77
78
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 76

def record_name
  @record_name
end

Instance Method Details

#to_hObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 103

def to_h
  # Take attribute index values (even) and put them into a hash.
  # The assigned value will be the subsequent odd index.
  # {attribute_name: attribute_value}
  {}.tap do |attributes|
    (2..(@raw.length - 1)).to_a.each do |index|
      if index.even?
        attribute_name = @raw[index]

        raise "Invalid attribute name: \"#{attribute_name}\"." if nil_or_empty?(attribute_name)
        attribute_name.strip!

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

        attributes[attribute_name] = stripped_value(@raw[index + 1])
      end
    end
  end
end