Class: SolidusImportProducts::Parser::Csv
- 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
-
#mappings ⇒ Object
Returns the value of attribute mappings.
-
#variant_option_fields ⇒ Object
Returns the value of attribute variant_option_fields.
Attributes inherited from Base
#data_file, #image_fields, #property_fields, #rows, #variant_image_fields
Instance Method Summary collapse
-
#column_mappings ⇒ Object
column_mappings This method attempts to automatically map headings in the CSV files with fields in the product and variant models.
-
#data_rows ⇒ Object
data_rows This method fetch the product rows.
-
#image_field?(field) ⇒ Boolean
image_field? Class method that check if a field is an image field.
-
#initialize(data_file, options) ⇒ Csv
constructor
A new instance of Csv.
-
#products_count ⇒ Object
products_count This method count the product rows.
-
#property_field?(field) ⇒ Boolean
property_field? Class method that check if a field is a product property field.
-
#variant_image_field?(field) ⇒ Boolean
variant_image_field? Class method that check if a field is a variant image field.
-
#variant_option_field?(field) ⇒ Boolean
variant_option_field? Class method that check if a field is a variant option field.
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, ) self.data_file = data_file self.mappings = {} self.variant_option_fields = [] self.image_fields = [] self.variant_image_fields = [] self.property_fields = [] encoding_csv = ([:encoding_csv] if ) || DEFAULT_CSV_ENCODING separator_char = ([:separator_char] if ) || 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
#mappings ⇒ Object
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_fields ⇒ Object
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_mappings ⇒ Object
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.)
30 31 32 |
# File 'app/models/solidus_import_products/parser/csv.rb', line 30 def column_mappings mappings end |
#data_rows ⇒ Object
data_rows This method fetch the product rows.
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
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_count ⇒ Object
products_count This method count the product rows.
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
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
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
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 |