Class: BBMB::Util::ProductImporter

Inherits:
CsvImporter show all
Defined in:
lib/bbmb/util/csv_importer.rb

Constant Summary collapse

PRODUCT_MAP =
{
  1   =>  :description_de,
  2   =>  :description_fr,
  3   =>  :ean13,
  4   =>  :pcode,    
  5   =>  :price,
}

Instance Method Summary collapse

Methods inherited from CsvImporter

#import, #string

Constructor Details

#initializeProductImporter

Returns a new instance of ProductImporter.



80
81
82
83
# File 'lib/bbmb/util/csv_importer.rb', line 80

def initialize
  super
  @active_products = {}
end

Instance Method Details

#import_record(record) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bbmb/util/csv_importer.rb', line 84

def import_record(record)
  article_number = string(record[0])
  return unless(/^\d+$/.match(article_number))
  @active_products.store article_number, true
  product = Model::Product.find_by_article_number(article_number) \
    || Model::Product.new(article_number)
  PRODUCT_MAP.each { |idx, name|
    value = string(record[idx])
    writer = "#{name}="
    case name
    when :description_de
      product.description.de = value
    when :description_fr
      product.description.fr = value
    else
      product.send(writer, value)
    end
  }
  product
end

#postprocess(persistence) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bbmb/util/csv_importer.rb', line 104

def postprocess(persistence)
  return if(@active_products.empty?)
  deletables = []
  persistence.all(BBMB::Model::Product) { |product|
    unless(@active_products.include?(product.article_number))
      deletables.push product
    end
  }
  persistence.all(BBMB::Model::Customer) { |customer|
    [customer.current_order, customer.favorites].each { |order|
      deletables.each { |product|
        order.add(0, product)
      }
    }
  }
  persistence.delete(*deletables) unless(deletables.empty?)
end