Class: DataShift::ConfigGenerator

Inherits:
GeneratorBase show all
Defined in:
lib/generators/config_generator.rb

Instance Attribute Summary collapse

Attributes inherited from GeneratorBase

#configuration

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GeneratorBase

#generate_with_associations

Methods included from Logging

#logdir, #logdir=, #logger, #verbose

Constructor Details

#initializeConfigGenerator

Returns a new instance of ConfigGenerator.



23
24
25
# File 'lib/generators/config_generator.rb', line 23

def initialize
  super
end

Instance Attribute Details

#export_templateObject



31
32
33
# File 'lib/generators/config_generator.rb', line 31

def export_template
  @export_template ||= File.join(DataShift.library_path, 'datashift/templates/import_export_config.erb')
end

#headersObject

Returns the value of attribute headers.



19
20
21
# File 'lib/generators/config_generator.rb', line 19

def headers
  @headers
end

#import_templateObject



27
28
29
# File 'lib/generators/config_generator.rb', line 27

def import_template
  @import_template ||= File.join(DataShift.library_path, 'datashift/templates/import_export_config.erb')
end

#output_filenameObject

Returns the value of attribute output_filename.



19
20
21
# File 'lib/generators/config_generator.rb', line 19

def output_filename
  @output_filename
end

Class Method Details

.titleObject



15
16
17
# File 'lib/generators/config_generator.rb', line 15

def self.title
  @mappings_title ||= "datashift_mappings:\n"
end

Instance Method Details

#create_import_config(klass_or_name, options = {}) ⇒ Object



75
76
77
78
79
80
81
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
109
110
111
112
113
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
# File 'lib/generators/config_generator.rb', line 75

def create_import_config(klass_or_name, options = {})

  klass = MapperUtils.ensure_class(klass_or_name)

  @key = 'data_flow_schema'
  @klass = klass.name.to_s

  defaults_section = create_section(options[:defaults] )
  overrides_section = create_section(options[:overrides] )
  prefixes_section = create_section(options[:prefixes] )
  postfixes_section = create_section(options[:postfixes] )

  @substitutions = options[:substitutions] || {}

  # operator => [rule , replacement]
  substitutions_section = ''

  @substitutions.each do |o, v|
    raise BadConfig, 'Substitutions need be in format {operator: [rule, replacement]}' unless(v.is_a? Array)
    substitutions_section += <<-EOS
    #{o}:
      - #{v.first}
      - #{v.last}
    EOS
  end

  @headers = Headers.klass_to_headers(klass)

  nodes_section = <<-EOS
# Mappings between inbound column names and internal names
# are only required when datashift cannot guess the mapping itself
# It will automatically map headings like :
#  'Product properties' or 'Product_Properties', 'product Properties' etc to product_properties
nodes:
EOS

  @headers.each_with_index do |s, _i|
    nodes_section += <<-EOS
    - #{s}:
        heading:
           source: #{s}
           presentation: #{s}
    EOS
  end

  x = <<-EOS
# YAML Configuration file for Datashift Import/Export
#
#{@key}:
  #{@klass}:
defaults:
#{defaults_section}
overrides:
 #{overrides_section}
# Expects a tuple (list with 2 entries), the rule and the replacement
substitutions:
#{substitutions_section}
prefixes:
#{prefixes_section}
postfixes:
#{postfixes_section}

#{nodes_section}

  EOS

  # This was a nightmare to get proeprly formatted YAML
  # Erubis::Eruby.new( File.read(import_template)).result(binding)

  x
end

#create_section(hash) ⇒ Object

Create an YAML ERB Configuration template for Importing. Includes available transformations and column mapping

For other options See DataShift::Loaders::Configuration



69
70
71
72
73
# File 'lib/generators/config_generator.rb', line 69

def create_section(hash)
  section = ''
  (hash || { "#name": 'value' } ).each { |n, v| section += "        #{n}: #{v}\n" }
  section
end

#export(klass_or_name, options = {}) ⇒ Object

Create an YAML Configuration template for Exporting includes available transformations and column mapping

For other options See DataShift::Exporters::Configuration



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/generators/config_generator.rb', line 162

def export(klass_or_name, options = {})

  @klass = MapperUtils.ensure_class(klass_or_name)

  @title = @klass.name.to_s

  @defaults = options[:defaults] || []

  @overrides = options[:overrides] || []
  @substitutions = options[:substitutions] || []
  @prefixs = options[:prefixs] || []
  @postfixs = options[:postfixs] || []

  klass_to_headers(@klass)

  Erubis::Eruby.new( File.read(export_template)).result(binding)
end

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

Create an YAML template BAASED on an Excel spreadsheet for mapping headers

  • :title - Top level YAML node -defaults to ConfigGenerator.title

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

  • :file - Write mappings direct to file name provided



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/generators/config_generator.rb', line 188

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 )

  @headers = excel.parse_headers(sheet, options[:header_row] || 0)

  mappings = options[:title] || ConfigGenerator.title

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

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

  mappings

end

#write_export(file_name, klass_or_name, options = {}) ⇒ Object

FOR EXPORTERS



149
150
151
152
153
154
155
# File 'lib/generators/config_generator.rb', line 149

def write_export(file_name, klass_or_name, options = {})
  result = export(klass_or_name, options)

  logger.info("Writing Export Config File [#{config[:file]}]")

  File.open(file_name, 'w') { |f| f << result }
end

#write_import(file_name, klass_or_name, options = {}) ⇒ Object

You can pass Transformations into the options

options[:defaults]
options[:overrides]
options[:substitutions]
options[:prefixes]
options[:postfixes]

For example :

options = {
  defaults: {'value_as_string': 'some default text', 'value_as_double': 45.467 }
}


49
50
51
52
53
54
55
56
57
# File 'lib/generators/config_generator.rb', line 49

def write_import(file_name, klass_or_name, options = {})
  result = create_import_config(klass_or_name, options)

  logger.info("Writing Import Config File [#{file_name}]")

  File.open(file_name, 'w') { |f| f << result }

  result
end