Class: Bulkrax::CsvParser::CsvTemplateGeneration::TemplateContext
- Inherits:
-
Object
- Object
- Bulkrax::CsvParser::CsvTemplateGeneration::TemplateContext
- Defined in:
- app/parsers/concerns/bulkrax/csv_parser/csv_template_generation.rb
Overview
Holds state for a single template generation run.
- Provides the interface expected by CsvTemplate
-
components.
Instance Attribute Summary collapse
-
#admin_set_id ⇒ Object
readonly
Returns the value of attribute admin_set_id.
-
#all_models ⇒ Object
readonly
Returns the value of attribute all_models.
-
#field_analyzer ⇒ Object
readonly
Returns the value of attribute field_analyzer.
-
#mapping_manager ⇒ Object
readonly
Returns the value of attribute mapping_manager.
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
Instance Method Summary collapse
- #field_metadata_for_all_models ⇒ Object
-
#initialize(models: nil, admin_set_id: nil) ⇒ TemplateContext
constructor
A new instance of TemplateContext.
- #to_csv_string ⇒ Object
- #to_file(file_path: nil) ⇒ Object
- #valid_headers_for_models ⇒ Object
Constructor Details
#initialize(models: nil, admin_set_id: nil) ⇒ TemplateContext
Returns a new instance of TemplateContext.
28 29 30 31 32 33 34 35 |
# File 'app/parsers/concerns/bulkrax/csv_parser/csv_template_generation.rb', line 28 def initialize(models: nil, admin_set_id: nil) @admin_set_id = admin_set_id @mapping_manager = CsvTemplate::MappingManager.new @mappings = @mapping_manager.mappings @field_analyzer = CsvTemplate::FieldAnalyzer.new(@mappings, admin_set_id) @all_models = CsvTemplate::ModelLoader.new(Array.wrap(models)).models @csv_builder = CsvTemplate::CsvBuilder.new(self) end |
Instance Attribute Details
#admin_set_id ⇒ Object (readonly)
Returns the value of attribute admin_set_id.
26 27 28 |
# File 'app/parsers/concerns/bulkrax/csv_parser/csv_template_generation.rb', line 26 def admin_set_id @admin_set_id end |
#all_models ⇒ Object (readonly)
Returns the value of attribute all_models.
26 27 28 |
# File 'app/parsers/concerns/bulkrax/csv_parser/csv_template_generation.rb', line 26 def all_models @all_models end |
#field_analyzer ⇒ Object (readonly)
Returns the value of attribute field_analyzer.
26 27 28 |
# File 'app/parsers/concerns/bulkrax/csv_parser/csv_template_generation.rb', line 26 def field_analyzer @field_analyzer end |
#mapping_manager ⇒ Object (readonly)
Returns the value of attribute mapping_manager.
26 27 28 |
# File 'app/parsers/concerns/bulkrax/csv_parser/csv_template_generation.rb', line 26 def mapping_manager @mapping_manager end |
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
26 27 28 |
# File 'app/parsers/concerns/bulkrax/csv_parser/csv_template_generation.rb', line 26 def mappings @mappings end |
Instance Method Details
#field_metadata_for_all_models ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'app/parsers/concerns/bulkrax/csv_parser/csv_template_generation.rb', line 47 def @field_metadata ||= @all_models.each_with_object({}) do |model, hash| field_list = @field_analyzer.find_or_create_field_list_for(model_name: model) hash[model] = { properties: field_list.dig(model, "properties") || [], required_terms: field_list.dig(model, "required_terms") || [], controlled_vocab_terms: field_list.dig(model, "controlled_vocab_terms") || [] } end end |
#to_csv_string ⇒ Object
43 44 45 |
# File 'app/parsers/concerns/bulkrax/csv_parser/csv_template_generation.rb', line 43 def to_csv_string @csv_builder.generate_string end |
#to_file(file_path: nil) ⇒ Object
37 38 39 40 41 |
# File 'app/parsers/concerns/bulkrax/csv_parser/csv_template_generation.rb', line 37 def to_file(file_path: nil) file_path ||= CsvTemplate::FilePathGenerator.default_path(@admin_set_id) @csv_builder.write_to_file(file_path) file_path end |
#valid_headers_for_models ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/parsers/concerns/bulkrax/csv_parser/csv_template_generation.rb', line 58 def valid_headers_for_models @valid_headers ||= begin column_builder = CsvTemplate::ColumnBuilder.new(self) all_columns = column_builder.all_columns all_columns - CsvTemplate::CsvBuilder::IGNORED_PROPERTIES rescue StandardError => e Rails.logger.error("Error building valid headers: #{e.}") standard_fields = %w[model source_identifier parent parents file] model_fields = .values.flat_map { |m| m[:properties] } (standard_fields + model_fields).uniq end end |