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

#attribute_is_json?, #nil_or_empty?, #stripped_value

Constructor Details

#initialize(raw, workflow) ⇒ Row

Returns a new instance of Row.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 91

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.



89
90
91
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 89

def model_name
  @model_name
end

#record_nameObject (readonly)

Returns the value of attribute record_name.



89
90
91
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 89

def record_name
  @record_name
end

Instance Method Details

#to_hObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 117

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!

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

        stripped = stripped_value(attribute, @raw[index + 1])
        unless @workflow.hole_value.nil?
          next if stripped == @workflow.hole_value
        end

        if attribute.is_project_name_reference?(model_name)
          stripped&.downcase!
        end

        attributes[attribute_name] = stripped
      end
    end
  end
end