Class: ActiveImport::ModelConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_import/model_converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModelConverter

Returns a new instance of ModelConverter.



7
8
9
10
# File 'lib/active_import/model_converter.rb', line 7

def initialize
  @columns = {}
  setup
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



3
4
5
# File 'lib/active_import/model_converter.rb', line 3

def columns
  @columns
end

#converted_valuesObject (readonly)

TODO: columns should be a reader only once old code has been fixed up



5
6
7
# File 'lib/active_import/model_converter.rb', line 5

def converted_values
  @converted_values
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/active_import/model_converter.rb', line 3

def options
  @options
end

#raw_valuesObject (readonly)

TODO: columns should be a reader only once old code has been fixed up



5
6
7
# File 'lib/active_import/model_converter.rb', line 5

def raw_values
  @raw_values
end

Instance Method Details

#add_column(column_name, options) ⇒ Object



15
16
17
# File 'lib/active_import/model_converter.rb', line 15

def add_column column_name, options
  @columns[column_name] = options
end

#afterObject



22
23
# File 'lib/active_import/model_converter.rb', line 22

def after
end

#beforeObject



19
20
# File 'lib/active_import/model_converter.rb', line 19

def before
end

#convert_attributes(values) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/active_import/model_converter.rb', line 64

def convert_attributes(values)
  cv = {}
  @columns.each_pair do |name, column|
    cv[name] = convert_attribute(name, values)
  end
  cv
end

#convert_boolean(value) ⇒ Object



85
86
87
# File 'lib/active_import/model_converter.rb', line 85

def convert_boolean value
  /^y|t/.match(value.strip.downcase) ? true : false
end

#convert_clean_string(value) ⇒ Object



78
79
80
81
82
83
# File 'lib/active_import/model_converter.rb', line 78

def convert_clean_string(value)
  value = value.to_i if (value.to_i == value.to_f) if /^\s*[\d]+(\.0+){0,1}\s*$/.match(value.to_s)
  value = value.gsub(/[^A-Za-z0-9 \.,\?'""!@#\$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~]/, '').strip if value.is_a?(String)
  return nil if value.to_s.blank? || value.to_s.nil?
  value.to_s
end

#convert_date(s) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/active_import/model_converter.rb', line 89

def convert_date s
  return nil if (s.nil? || s.blank?)
  return Date.strptime(s, "%d/%m/%y") if /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2}$/.match(s)
  return DateTime.new(1899,12,30) + s.to_f if s.to_f unless s !~ /^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$/
  begin
  result = Date.parse(s)
  rescue
    puts "Could not parse date ".red + "'#{s}'"
  end

  return result
end

#convert_string(value) ⇒ Object



72
73
74
75
76
# File 'lib/active_import/model_converter.rb', line 72

def convert_string(value)
  value = value.to_i if (value.to_i == value.to_f) if /^\s*[\d]+(\.0+){0,1}\s*$/.match(value.to_s)
  return nil if value.to_s.blank? || value.to_s.nil?
  value.to_s
end

#csv_headersObject



31
32
33
34
35
36
37
38
39
# File 'lib/active_import/model_converter.rb', line 31

def csv_headers()
  selected_columns = @columns

  [].tap do |o|
    selected_columns.each_value do |column|
      o << (column[:header] || column[:match])
    end
  end.to_csv.html_safe
end

#csv_values(values) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/active_import/model_converter.rb', line 41

def csv_values(values)
  selected_columns = @columns

  [].tap do |o|
    selected_columns.each_key do |column|
      o << values[column]
    end
  end.to_csv.html_safe
end


25
26
27
28
29
# File 'lib/active_import/model_converter.rb', line 25

def print_columns
  @columns.each_pair do |name, column|
    puts name
  end
end

#process_values(values) ⇒ Object



51
52
53
54
# File 'lib/active_import/model_converter.rb', line 51

def process_values(values)
  @raw_values = values
  @converted_values = convert_attributes(values)
end

#remove_blank_from_converted_valuesObject



60
61
62
# File 'lib/active_import/model_converter.rb', line 60

def remove_blank_from_converted_values
  @converted_values.delete_if { |k, v| v.to_s.blank? }
end

#remove_nil_from_converted_valuesObject



56
57
58
# File 'lib/active_import/model_converter.rb', line 56

def remove_nil_from_converted_values
  @converted_values.delete_if { |k, v| v.nil? }
end

#reportObject



102
103
104
# File 'lib/active_import/model_converter.rb', line 102

def report
  ""
end

#setupObject



12
13
# File 'lib/active_import/model_converter.rb', line 12

def setup
end