lib/spreadsheet_architect/utils.rb in spreadsheet_architect-3.0.0 vs lib/spreadsheet_architect/utils.rb in spreadsheet_architect-3.1.0

- old
+ new

@@ -30,47 +30,61 @@ else raise SpreadsheetArchitect::Exceptions::NoDataError end end - if !options[:spreadsheet_columns] && klass != SpreadsheetArchitect && !klass.instance_methods.include?(:spreadsheet_columns) - if is_ar_model?(klass) - the_column_names = klass.column_names - headers = the_column_names.map{|x| str_titleize(x)} if needs_headers - columns = the_column_names.map{|x| x.to_sym} - else - raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(klass) + if options[:spreadsheet_columns] + if [String, Symbol].any?{|x| options[:spreadsheet_columns].is_a?(x)} + cols_method_name = options[:spreadsheet_columns] + + if klass != SpreadsheetArchitect && !klass.instance_methods.include?(cols_method_name) + raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(klass, cols_method_name) + end end + else + if klass != SpreadsheetArchitect && !klass.instance_methods.include?(:spreadsheet_columns) + if is_ar_model?(klass) + the_column_names = klass.column_names + headers = the_column_names.map{|x| str_titleize(x)} if needs_headers + columns = the_column_names.map{|x| x.to_sym} + else + raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(klass) + end + end end data = [] options[:instances].each do |instance| if columns - data.push columns.map{|col| col.is_a?(Symbol) ? instance.instance_eval(col.to_s) : col} + data.push columns.map{|col| col.is_a?(Symbol) ? instance.send(col) : col} else row_data = [] - if !options[:spreadsheet_columns] + if options[:spreadsheet_columns] + if cols_method_name + instance_cols = instance.send(cols_method_name) + else + instance_cols = options[:spreadsheet_columns].call(instance) + end + else if klass == SpreadsheetArchitect && !instance.respond_to?(:spreadsheet_columns) raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(instance.class) else instance_cols = instance.spreadsheet_columns end - else - instance_cols = options[:spreadsheet_columns].call(instance) end instance_cols.each_with_index do |x,i| if x.is_a?(Array) headers.push(x[0].to_s) if needs_headers - row_data.push(x[1].is_a?(Symbol) ? instance.instance_eval(x[1].to_s) : x[1]) + row_data.push(x[1].is_a?(Symbol) ? instance.send(x[1]) : x[1]) if needs_column_types column_types[i] = x[2] end else headers.push(str_titleize(x.to_s)) if needs_headers - row_data.push(x.is_a?(Symbol) ? instance.instance_eval(x.to_s) : x) + row_data.push(x.is_a?(Symbol) ? instance.send(x) : x) end end data.push row_data @@ -99,11 +113,11 @@ if klass::SPREADSHEET_OPTIONS.is_a?(Hash) options = SpreadsheetArchitect.default_options.merge( klass::SPREADSHEET_OPTIONS.merge(options) ) else - raise SpreadsheetArchitect::Exceptions::InvalidTypeError.new("#{klass}::SPREADSHEET_OPTIONS constant") + raise SpreadsheetArchitect::Exceptions::OptionTypeError.new("#{klass}::SPREADSHEET_OPTIONS constant") end else options = SpreadsheetArchitect.default_options.merge(options) end @@ -186,23 +200,24 @@ elsif !val.is_a?(type) invalid = true end if invalid - raise SpreadsheetArchitect::Exceptions::InvalidTypeError.new(":#{option_name} option") + raise SpreadsheetArchitect::Exceptions::OptionTypeError.new(":#{option_name} option") end end end def self.verify_option_types(options) - check_option_type(options, :spreadsheet_columns, Proc) + check_option_type(options, :spreadsheet_columns, [Proc, Symbol, String]) check_option_type(options, :data, Array) check_option_type(options, :instances, Array) check_option_type(options, :headers, [TrueClass, Array]) check_option_type(options, :header_style, Hash) check_option_type(options, :row_style, Hash) check_option_type(options, :column_styles, Array) check_option_type(options, :range_styles, Array) + check_option_type(options, :conditional_row_styles, Array) check_option_type(options, :merges, Array) check_option_type(options, :borders, Array) check_option_type(options, :column_widths, Array) end