Class: Etna::Clients::Magma::ModelsCsv::Importer

Inherits:
Etna::CsvImporter show all
Includes:
Prettify
Defined in:
lib/etna/clients/magma/formatting/models_csv.rb

Constant Summary collapse

ATTRIBUTE_ROW_ENTRIES =

Columns of the row, _after format_row is called_, that should be applied to an attribute. This should line up with the attribute names _on the model itself_.

[
    :attribute_type,
    :link_model_name, :desc,
    :display_name, :format_hint,
    :restricted, :read_only,
    :validation, :attribute_group,
    :hidden, :unique,
]

Constants inherited from Etna::CsvImporter

Etna::CsvImporter::COLUMN_AS_BOOLEAN

Instance Method Summary collapse

Methods included from Prettify

#prettify

Methods inherited from Etna::CsvImporter

#each_csv_row, #replace_row_column

Constructor Details

#initializeImporter

Returns a new instance of Importer.



185
186
187
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 185

def initialize
  super(&method(:format_row))
end

Instance Method Details

#format_row(row) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 189

def format_row(row)
  replace_row_column(row, :attribute_type) { |s| AttributeType.new(s) }
  replace_row_column(row, :desc) { row.delete(:description) }
  replace_row_column(row, :restricted, &COLUMN_AS_BOOLEAN)
  replace_row_column(row, :read_only, &COLUMN_AS_BOOLEAN)
  replace_row_column(row, :options) { |s| {"type" => "Array", "value" => s.split(',').map(&:strip)} }
  replace_row_column(row, :match) { |s| {"type" => "Regexp", "value" => Regexp.new(s).source} }
  replace_row_column(row, :validation) { row[:options] || row[:match] }
  replace_row_column(row, :hidden, &COLUMN_AS_BOOLEAN)
  replace_row_column(row, :unique, &COLUMN_AS_BOOLEAN)
end

#prepare_changeset(filename: nil, input_io: nil, &validation_err_block) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 210

def prepare_changeset(filename: nil, input_io: nil, &validation_err_block)
  ModelsChangeset.new.tap do |changeset|
    context = {}
    each_csv_row(filename: filename, input_io: input_io) do |row, lineno|
      p = NestedRowProcessor.new(row, lineno, context)
      process_row(p, changeset)
    end

    # After configuring attributes, set defaults for certain attribute properties.
    changeset.models.all.each do |model|
      model.template.attributes.all.each do |attribute|
        attribute.set_field_defaults!
      end
    end
  end
rescue ImportError => e
  validation_err_block.call(e.message)
end

#process_row(row_processor, changeset) ⇒ Object



201
202
203
204
205
206
207
208
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 201

def process_row(row_processor, changeset)
  models = changeset.models

  process_matrix_constants(changeset, row_processor)
  process_model_config(models, row_processor)
  process_parent_config(models, row_processor)
  process_attribute_properties(changeset, row_processor)
end