Class: DataShift::ConfigGenerator

Inherits:
GeneratorBase show all
Defined in:
lib/datashift/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/datashift/generators/config_generator.rb', line 23

def initialize
  super
end

Instance Attribute Details

#export_templateObject



31
32
33
# File 'lib/datashift/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/datashift/generators/config_generator.rb', line 19

def headers
  @headers
end

#import_templateObject



27
28
29
# File 'lib/datashift/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/datashift/generators/config_generator.rb', line 19

def output_filename
  @output_filename
end

Class Method Details

.titleObject



15
16
17
# File 'lib/datashift/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

Includes a column mapping section, generated from klass_to_headers.

The column set to be exported to the config file can be manipulated via configuration options such as

:remove_columns - List of columns to remove from files

:remove_rails - Remove standard Rails cols like :id, created_at etc


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
146
147
148
149
150
151
152
153
# File 'lib/datashift/generators/config_generator.rb', line 84

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 += "    \#{o}:\n      - \#{v.first}\n      - \#{v.last}\n    EOS\n  end\n\n  @headers = Headers.klass_to_operators(klass)\n\n  nodes_section = <<-EOS\n# Mappings between inbound column names and internal names\n# are only required when datashift cannot guess the mapping itself\n# It will automatically map headings like :\n#  'Product properties' or 'Product_Properties', 'product Properties' etc to product_properties\nnodes:\n"

  @headers.each_with_index do |s, _i|
    nodes_section += "    - \#{s}:\n        heading:\n           source: \#{s}\n    EOS\n  end\n\n  x = <<-EOS\n# YAML Configuration file for Datashift Import/Export\n#\n\#{@key}:\n  \#{@klass}:\ndefaults:\n\#{defaults_section}\noverrides:\n \#{overrides_section}\n# Expects a tuple (list with 2 entries), the rule and the replacement\nsubstitutions:\n\#{substitutions_section}\nprefixes:\n\#{prefixes_section}\npostfixes:\n\#{postfixes_section}\n\n\#{nodes_section}\n\n  EOS\n\n  # This was a nightmare to get proeprly formatted YAML\n  # Erubis::Eruby.new( File.read(import_template)).result(binding)\n\n  x\nend\n"

#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/datashift/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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/datashift/generators/config_generator.rb', line 170

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



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/datashift/generators/config_generator.rb', line 196

def generate_from_excel(excel_file_name, options = {})

  excel = Excel.new

  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



157
158
159
160
161
162
163
# File 'lib/datashift/generators/config_generator.rb', line 157

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/datashift/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