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, :link_attribute_name, :description,
    :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.



190
191
192
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 190

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

Instance Method Details

#format_row(row) ⇒ Object



194
195
196
197
198
199
200
201
202
203
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 194

def format_row(row)
  replace_row_column(row, :attribute_type) { |s| AttributeType.new(s) }
  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



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 214

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)
  raise e
end

#process_row(row_processor, changeset) ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 205

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