Class: DataShift::MappingGenerator

Inherits:
GeneratorBase show all
Includes:
Logging, ExcelBase
Defined in:
lib/generators/mapping_generator.rb

Instance Attribute Summary

Attributes included from ExcelBase

#excel_headers, #header_row_index

Attributes inherited from GeneratorBase

#filename, #headers, #remove_list

Instance Method Summary collapse

Methods included from ExcelBase

#ar_to_headers, #ar_to_xls, #ar_to_xls_cell, #ar_to_xls_row, #parse_headers, #sanitize_sheet_name

Methods included from Logging

#logdir, #logger

Methods inherited from GeneratorBase

#prep_remove_list, #prepare_model_headers, rails_columns, #remove_headers

Constructor Details

#initialize(filename) ⇒ MappingGenerator

Returns a new instance of MappingGenerator.



17
18
19
# File 'lib/generators/mapping_generator.rb', line 17

def initialize(filename)
  super(filename)
end

Instance Method Details

#generate(model = nil, options = {}) ⇒ Object

Create an YAML template for mapping headers

Options:

  • :model_as_dest - Override default treatment of using model as the SOURCE

  • :remove - Array of header names to remove

Rails columns like id, created_at etc are added to the remove list by default

  • :include_rails - Specify to keep Rails columns in mappings

  • :associations - Additionally include all Associations

  • :exclude - Association TYPE(s) to exclude.

    Possible association_type values are given by MethodDetail::supported_types_enum
    ... [:assignment, :belongs_to, :has_one, :has_many]
    
  • :file - Write mappings direct to file name provided



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/mapping_generator.rb', line 42

def generate(model = nil, options = {})

  mappings = "mappings:\n"

  if(model)

    klass = DataShift::ModelMapper.class_from_string_or_raise( model )

    MethodDictionary.find_operators( klass )

    MethodDictionary.build_method_details( klass )

    prepare_model_headers(MethodDictionary.method_details_mgrs[klass], options)

    if(options[:model_as_dest])
      headers.each_with_index do |s, i|  mappings += "       #srcs_column_heading_#{i}: #{s}\n" end
    else
      headers.each_with_index do |s, i|  mappings += "       #{s}: #dest_column_heading_#{i}\n" end
    end
  else
    mappings += "##source_column_heading_0: #dest_column_heading_0\n##source_column_heading_1: #dest_column_heading_1\n##source_column_heading_2: #dest_column_heading_2\n\n"
  end

  File.open(options[:file], 'w')  do |f| f << mappings  end if(options[:file])

  mappings

end

#generate_from_excel(excel_file_name, options = {}) ⇒ Object

Create an YAML template from a Excel spreadsheet for mapping headers

  • :model_as_dest - Override default treatment of using model as the SOURCE

  • :file - Write mappings direct to file name provided



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/generators/mapping_generator.rb', line 82

def generate_from_excel(excel_file_name, options = {})

  excel = Excel.new

  puts "\n\n\nGenerating mapping from Excel file: #{excel_file_name}"

  excel.open(excel_file_name)

  sheet_number = options[:sheet_number] || 0

  sheet = excel.worksheet( sheet_number )

  parse_headers(sheet, options[:header_row])

  mappings = "mappings:\n"

  if(options[:model_as_dest])
    excel_headers.each_with_index do |s, i|  mappings += "       #srcs_column_heading_#{i}: #{s}\n" end
  else
    excel_headers.each_with_index do |s, i|  mappings += "       #{s}: #dest_column_heading_#{i}\n" end
  end

  File.open(options[:file], 'w')  do |f| f << mappings  end if(options[:file])

  mappings

end