lib/loaders/loader_base.rb in datashift-0.6.0 vs lib/loaders/loader_base.rb in datashift-0.6.1

- old
+ new

@@ -98,10 +98,11 @@ # populate or integrate an object of type @load_object_class DataShift::MethodDictionary.build_method_details(@load_object_class) @method_mapper = DataShift::MethodMapper.new @options = options.clone + @verbose_logging = @options[:verbose_logging] @headers = [] @default_data_objects ||= {} @default_values = {} @@ -214,11 +215,15 @@ end # Default values can be provided in YAML config file # Format : - # Load Class + # + # LoaderClass: + # option: value + # + # Load Class: (e.g Spree:Product) # atttribute: value def configure_from( yaml_file ) data = YAML::load( File.open(yaml_file) ) @@ -249,17 +254,24 @@ logger.info("Read Datashift loading config: #{data.inspect}") if(data[load_object_class.name]) + logger.info("Assigning defaults and over rides from config") + deflts = data[load_object_class.name]['datashift_defaults'] @default_values.merge!(deflts) if deflts ovrides = data[load_object_class.name]['datashift_overrides'] @override_values.merge!(ovrides) if ovrides end + if(data['LoaderBase']) + @options.merge!(data['LoaderBase']) + end + + logger.info("Loader Options : #{@options.inspect}") end # Set member variables to hold details and value. # # Check supplied value, validate it, and if required : @@ -283,18 +295,33 @@ @current_value = "#{@current_value}#{postfixes(operator)}" if(postfixes(operator)) @current_value end - + # return the find_by operator and the values to find + def get_find_operator_and_rest( column_data) + + find_operator, col_values = "",nil + + if(@current_method_detail.find_by_operator) + find_operator, col_values = @current_method_detail.find_by_operator, column_data + else + find_operator, col_values = column_data.split(LoaderBase::name_value_delim) + end + + return find_operator, col_values + end + # Process a value string from a column. # Assigning value(s) to correct association on @load_object. # Method detail represents a column from a file and it's correlated AR associations. # Value string which may contain multiple values for a collection association. # - def process() + def process() + logger.info("Current value to assign : #{@current_value}") #if @options['verboose_logging'] + if(@current_method_detail.operator_for(:has_many)) if(@current_method_detail.operator_class && @current_value) # there are times when we need to save early, for example before assigning to @@ -308,20 +335,21 @@ # Size:large|Colour:red,green,blue => generates find_by_size( 'large' ) and find_all_by_colour( ['red','green','blue'] ) columns.each do |col_str| - find_operator, col_values = "","" - - if(@current_method_detail.find_by_operator) - find_operator, col_values = @current_method_detail.find_by_operator, col_str - else - find_operator, col_values = col_str.split(LoaderBase::name_value_delim) - raise "No key to find #{@current_method_detail.operator} in DB. Expected format key:value" unless(col_values) - end + find_operator, col_values = get_find_operator_and_rest( col_str ) + #if(@current_method_detail.find_by_operator) + # find_operator, col_values = @current_method_detail.find_by_operator, col_str + # else + # find_operator, col_values = col_str.split(LoaderBase::name_value_delim) + # end + + raise "Cannot perform DB find by #{find_operator}. Expected format key:value" unless(find_operator && col_values) + find_by_values = col_values.split(LoaderBase::multi_value_delim) - + if(find_by_values.size > 1) @current_value = @current_method_detail.operator_class.send("find_all_by_#{find_operator}", find_by_values ) unless(find_by_values.size == @current_value.size) \ No newline at end of file