Class: Etna::Clients::Magma::ModelsCsv::Exporter

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

Instance Attribute Summary

Attributes inherited from Etna::CsvExporter

#column_headers

Instance Method Summary collapse

Methods included from Prettify

#prettify

Methods inherited from Etna::CsvExporter

#header_row, #map_column_value, #row_from_columns, #with_row_writeable

Constructor Details

#initializeExporter

Returns a new instance of Exporter.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 17

def initialize
  super([
      :comments,
      :model_name, :identifier, :parent_model_name, :parent_link_type,
      :attribute_name,
      :new_attribute_name,
      :attribute_type,
      :link_model_name,
      :link_attribute_name,
      :description,
      :display_name,
      :format_hint,
      :restricted,
      :read_only,
      :options,
      :match,
      :attribute_group,
      :hidden,
      :unique,
      :matrix_constant,
  ])
end

Instance Method Details

#each_attribute_row(models, model, attribute, matrix_constants, row_writeable) ⇒ Object



114
115
116
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
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 114

def each_attribute_row(models, model, attribute, matrix_constants, row_writeable)
  if attribute.attribute_type == Etna::Clients::Magma::AttributeType::IDENTIFIER
    # Identifiers for models whose parent link type ends up being a table are non configurable, so we don't
    # want to include them in the CSV.
    if models.find_reciprocal(
      model: model,
      link_attribute_name: model.name,
      link_model: models.model(model.template.parent))&.attribute_type == Etna::Clients::Magma::AttributeType::TABLE
      return
    end
  else
    return unless Etna::Clients::Magma::AttributeValidator.valid_add_row_attribute_types.include?(attribute.attribute_type)
  end

  options = attribute.options&.join(', ')
  if attribute.attribute_type == Etna::Clients::Magma::AttributeType::MATRIX
    # Matrix attribute validations are massive, and functional.  For now, we don't support showing and editing
    # them inline with this spreadsheet.  In the future, I think we should possibly introduce the concept of
    # CONSTANTS or Matrix Types that are managed separately.
    options = options || ''
    digest = Digest::MD5.hexdigest(options)
    matrix_constants[digest] ||= options.split(',').map(&:strip)

    options = COPY_OPTIONS_SENTINEL + digest
  end

  row_writeable.write(
      attribute_name: attribute.name,
      attribute_type: attribute.attribute_type,
      link_model_name: attribute.link_model_name,
      link_attribute_name: attribute.link_attribute_name,
      reciprocal_link_type: models.find_reciprocal(model: model, attribute: attribute)&.attribute_type,
      description: attribute.description,
      display_name: attribute.display_name,
      match: attribute.match,
      format_hint: attribute.format_hint,
      restricted: attribute.restricted,
      read_only: attribute.read_only,
      options: options,
      attribute_group: attribute.attribute_group,
      hidden: attribute.hidden,
      unique: attribute.unique,
  )
end

#each_model_row(models, model_name, matrix_constants, row_writeable) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 105

def each_model_row(models, model_name, matrix_constants, row_writeable)
  return unless (model = models.model(model_name))

  each_model_trunk_row(models, model_name, row_writeable)
  model.template.attributes.all.each do |attribute|
    each_attribute_row(models, model, attribute, matrix_constants, row_writeable)
  end
end

#each_model_trunk_row(models, model_name, row_writeable) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 82

def each_model_trunk_row(models, model_name, row_writeable)
  return unless (model = models.model(model_name))

  # Empty link for better visual separation
  row_writeable.write
  row_writeable.write(model_name: model_name)

  if !model.template.parent.nil?
    parent_model = models.model(model.template.parent)
    reciprocal = models.find_reciprocal(model: model, link_model: parent_model)

    row_writeable.write(
        identifier: model.template.identifier,
        parent_model_name: model.template.parent,
        parent_link_type: reciprocal.attribute_type.to_s
    )
  else
    row_writeable.write(
        identifier: model.template.identifier,
    )
  end
end

#ensure_parents(models, model_keys, row_writeable) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 61

def ensure_parents(models, model_keys, row_writeable)
  q = model_keys.dup
  seen = Set.new

  until q.empty?
    model_key = q.shift
    next if model_key.nil?
    next if seen.include?(model_key)
    seen.add(model_key)

    # For models that are only part of the trunk, but not of the tree of model_keys,
    # we still need their basic information (identifier / parent) for validation and for
    # potentially creating the required tree dependencies to connect it to a remote tree.
    unless model_keys.include?(model_key)
      each_model_trunk_row(models, model_key, row_writeable)
    end

    q.push(*models.model(model_key).template.all_linked_model_names)
  end
end

#write_model_rows(models, model_keys, row_writeable) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 46

def write_model_rows(models, model_keys, row_writeable)
  ensure_parents(models, model_keys, row_writeable)

  matrix_constants = {}
  model_keys.each { |model_name| each_model_row(models, model_name, matrix_constants, row_writeable) }

  matrix_constants.each do |digest, options|
    row_writeable.write
    row_writeable.write(matrix_constant: COPY_OPTIONS_SENTINEL + digest)
    options.each do |option|
      row_writeable.write(matrix_constant: option)
    end
  end
end

#write_models(models, model_keys = models.model_keys.sort, output_io: nil, filename: nil) ⇒ Object



40
41
42
43
44
# File 'lib/etna/clients/magma/formatting/models_csv.rb', line 40

def write_models(models, model_keys = models.model_keys.sort, output_io: nil, filename: nil)
  with_row_writeable(filename: filename, output_io: output_io) do |row_writeable|
    write_model_rows(models, model_keys, row_writeable)
  end
end