Class: SolidusImportProducts::Parser::Csv

Inherits:
Base
  • Object
show all
Defined in:
app/models/solidus_import_products/parser/csv.rb

Constant Summary collapse

DEFAULT_CSV_ENCODING =
'utf-8'.freeze
DEFAULT_CSV_SEPARATOR =
','.freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#data_file, #image_fields, #property_fields, #rows, #variant_image_fields

Instance Method Summary collapse

Constructor Details

#initialize(data_file, options) ⇒ Csv

Returns a new instance of Csv.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/solidus_import_products/parser/csv.rb', line 11

def initialize(data_file, options)
  self.data_file = data_file
  self.mappings = {}
  self.variant_option_fields = []
  self.image_fields = []
  self.variant_image_fields = []
  self.property_fields = []
  encoding_csv = (options[:encoding_csv] if options) || DEFAULT_CSV_ENCODING
  separator_char = (options[:separator_char] if options) || DEFAULT_CSV_SEPARATOR
  csv_string = open(data_file, "r:#{encoding_csv}").read.encode('utf-8')
  self.rows = CSV.parse(csv_string, col_sep: separator_char)
  extract_column_mappings
end

Instance Attribute Details

#mappingsObject

Returns the value of attribute mappings.



9
10
11
# File 'app/models/solidus_import_products/parser/csv.rb', line 9

def mappings
  @mappings
end

#variant_option_fieldsObject

Returns the value of attribute variant_option_fields.



9
10
11
# File 'app/models/solidus_import_products/parser/csv.rb', line 9

def variant_option_fields
  @variant_option_fields
end

Instance Method Details

#column_mappingsObject

column_mappings This method attempts to automatically map headings in the CSV files with fields in the product and variant models. Rows is an array of headings for columns - SKU, Master Price, etc.)

Returns:

  • a hash of symbol heading => column index pairs



30
31
32
# File 'app/models/solidus_import_products/parser/csv.rb', line 30

def column_mappings
  mappings
end

#data_rowsObject

data_rows This method fetch the product rows.

Returns:

  • a array of columns with product information



65
66
67
# File 'app/models/solidus_import_products/parser/csv.rb', line 65

def data_rows
  rows[1..-1]
end

#image_field?(field) ⇒ Boolean

image_field? Class method that check if a field is an image field

Returns:

  • (Boolean)

    true or false



51
52
53
# File 'app/models/solidus_import_products/parser/csv.rb', line 51

def image_field?(field)
  image_fields.include?(field.to_s)
end

#products_countObject

products_count This method count the product rows.

Returns:

  • a integer



72
73
74
# File 'app/models/solidus_import_products/parser/csv.rb', line 72

def products_count
  data_rows.count
end

#property_field?(field) ⇒ Boolean

property_field? Class method that check if a field is a product property field

Returns:

  • (Boolean)

    true or false



44
45
46
# File 'app/models/solidus_import_products/parser/csv.rb', line 44

def property_field?(field)
  property_fields.include?(field.to_s)
end

#variant_image_field?(field) ⇒ Boolean

variant_image_field? Class method that check if a field is a variant image field

Returns:

  • (Boolean)

    true or false



58
59
60
# File 'app/models/solidus_import_products/parser/csv.rb', line 58

def variant_image_field?(field)
  variant_image_fields.include?(field.to_s)
end

#variant_option_field?(field) ⇒ Boolean

variant_option_field? Class method that check if a field is a variant option field

Returns:

  • (Boolean)

    true or false



37
38
39
# File 'app/models/solidus_import_products/parser/csv.rb', line 37

def variant_option_field?(field)
  variant_option_fields.include?(field.to_s)
end