Class: LoaderBase

Inherits:
Object show all
Defined in:
lib/loaders/loader_base.rb

Overview

Copyright

© Autotelik Media Ltd 2011

Author

Tom Statter

Date

Aug 2010

License

MIT

Details

Base class for loaders, providing a process hook which populates a model, based on a method map and supplied value from a file - i.e a single column/row’s string value. Note that although a single column, the string can be formatted to contain multiple values.

Tightly coupled with MethodMapper classes (in lib/engine) which contains full details of a file’s column and it’s correlated AR associations.

Direct Known Subclasses

ImageLoader, ProductLoader

Constant Summary collapse

@@name_value_delim =

Enable single column (association) to contain multiple name/value sets in default form :

Name1:value1, value2|Name2:value1, value2, value3|Name3:value1, value2

E.G.

Row for association could have a name (Size/Colour/Sex) with a set of values,
and this combination can be expressed multiple times :
Size:small,medium,large|Colour:red, green|Sex:Female
':'
@@multi_value_delim =
','
@@multi_assoc_delim =
'|'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_class, object = nil) ⇒ LoaderBase

Returns a new instance of LoaderBase.



33
34
35
36
# File 'lib/loaders/loader_base.rb', line 33

def initialize(object_class, object = nil)
  @load_object_class = object_class
  @load_object = object || @load_object_class.new
end

Instance Attribute Details

#load_objectObject

Returns the value of attribute load_object.



15
16
17
# File 'lib/loaders/loader_base.rb', line 15

def load_object
  @load_object
end

#load_object_classObject

Returns the value of attribute load_object_class.



15
16
17
# File 'lib/loaders/loader_base.rb', line 15

def load_object_class
  @load_object_class
end

#valueObject

Returns the value of attribute value.



15
16
17
# File 'lib/loaders/loader_base.rb', line 15

def value
  @value
end

Class Method Details

.set_multi_assoc_delim(x) ⇒ Object



31
# File 'lib/loaders/loader_base.rb', line 31

def self.set_multi_assoc_delim(x) @@multi_assoc_delim = x; end

.set_multi_value_delim(x) ⇒ Object



30
# File 'lib/loaders/loader_base.rb', line 30

def self.set_multi_value_delim(x) @@multi_value_delim = x; end

.set_name_value_delim(x) ⇒ Object



29
# File 'lib/loaders/loader_base.rb', line 29

def self.set_name_value_delim(x)  @@name_value_delim = x; end

Instance Method Details

#process(method_map, value) ⇒ Object

What process a value string from a column. Assigning value(s) to correct association on @load_object. Method map represents a column from a file and it’s correlated AR associations. Value string which may contain multiple values for a collection association.



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
# File 'lib/loaders/loader_base.rb', line 47

def process(method_map, value)
  #puts "INFO: LOADER BASE processing #{@load_object}"
  @value = value
  
  if(method_map.has_many && method_map.has_many_class && @value)
    # The Generic handler for Associations
    # The actual class of the association so we can find_or_create on it
    assoc_class = method_map.has_many_class

    puts "Processing Association: #{assoc_class} : #{@value}"

    @value.split(@@multi_assoc_delim).collect do |lookup|
      # TODO - Don't just rely on 'name' but try different finds as per MethodMappe::insistent_belongs_to ..
      x = assoc_class.find(:first, :conditions => ['lower(name) LIKE ?', "%#{lookup.downcase}%"])
      unless x
        puts "WARNING: #{lookup} in #{assoc_class} NOT found - Not added to #{@load_object.class}"
        next
      end
      @load_object.send( method_map.has_many ) << x
      @load_object.save
    end
  else
    # Nice n simple straight assignment to a column variable
    method_map.assign(@load_object, @value) unless method_map.has_many
  end
end

#resetObject



38
39
40
# File 'lib/loaders/loader_base.rb', line 38

def reset()
  @load_object = @load_object_class.new
end