Class: ProductLoader

Inherits:
LoaderBase show all
Defined in:
lib/loaders/spree/product_loader.rb

Instance Attribute Summary

Attributes inherited from LoaderBase

#load_object, #load_object_class, #value

Instance Method Summary collapse

Methods inherited from LoaderBase

#reset, set_multi_assoc_delim, set_multi_value_delim, set_name_value_delim

Constructor Details

#initialize(klass = Product, product = nil) ⇒ ProductLoader

Returns a new instance of ProductLoader.



12
13
14
15
# File 'lib/loaders/spree/product_loader.rb', line 12

def initialize(klass = Product, product = nil)
  super( klass, product )
  raise "Failed to create Product for loading" unless @load_object
end

Instance Method Details

#process(method_map, value) ⇒ Object

What process a value string from a column, assigning value(s) to correct association on Product. Method map represents a column from a file and it’s correlated Product association. Value string which may contain multiple values for a collection association. Product to assign that value to.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/loaders/spree/product_loader.rb', line 21

def process( method_map, value)
  #puts "INFO: PRODUCT LOADER processing #{@load_object}"
  @value = value

  #puts "DEBUG : process #{method_map.inspect} : #{value.inspect}"
  # Special case for OptionTypes as it's two stage process
  # First add the possible option_types to Product, then we are able
  # to define Variants on those options.
  
  if(method_map.name == 'option_types' && @value)

    option_types = @value.split(@@multi_assoc_delim)
    option_types.each do |ostr|
      oname, value_str = ostr.split(@@name_value_delim)
      option_type = OptionType.find_or_create_by_name(oname)
      unless option_type
        puts "WARNING: OptionType #{oname} NOT found - Not set Product"
        next
      end

      @load_object.option_types << option_type unless @load_object.option_types.include?(option_type)

      # Now get the value(s) for the option e.g red,blue,green for OptType 'colour'
      ovalues = value_str.split(',')
      ovalues.each_with_index do |ovname, i|
        ovname.strip!
        ov = OptionValue.find_by_name(ovname)
        if ov
          object = Variant.new( :sku => "#{@load_object.sku}_#{i}", :price => @load_object.price, :available_on => @load_object.available_on)
          #puts "DEBUG: Create New Variant: #{object.inspect}"
          object.option_values << ov
          @load_object.variants << object
        else
          puts "WARNING: Option #{ovname} NOT FOUND - No Variant created"
        end
      end
    end

  # Special case for ProductProperties since it can have additional value applied.
  # A list of Properties with a optional Value - supplied in form :
  #   Property:value|Property2:value|Property3:value
  #
  elsif(method_map.name == 'product_properties' && @value)

    property_list = @value.split(@@multi_assoc_delim)

    property_list.each do |pstr|
      pname, pvalue = pstr.split(@@name_value_delim)
      property = Property.find_by_name(pname)
      unless property
        puts "WARNING: Property #{pname} NOT found - Not set Product"
        next
      end
      @load_object.product_properties << ProductProperty.create( :property => property, :value => pvalue)
    end

  elsif(method_map.name == 'count_on_hand' && @load_object.variants.size > 0 &&
        @value.is_a?(String) && @value.include?(@@multi_assoc_delim))
    # Check if we processed Option Types and assign count per option
    values = @value.split(@@multi_assoc_delim)
    if(@load_object.variants.size == values.size)
      @load_object.variants.each_with_index {|v, i| v.count_on_hand == values[i] }
    else
      puts "WARNING: Count on hand entries does not match number of Variants"
    end

  else
    super(method_map, value)
  end

end