lib/dm-serializer/to_csv.rb in dm-serializer-1.1.0.rc2 vs lib/dm-serializer/to_csv.rb in dm-serializer-1.1.0.rc3

- old
+ new

@@ -11,27 +11,35 @@ # do nothing end end module DataMapper - module Serialize + module Serializer # Serialize a Resource to comma-separated values (CSV). # # @return <String> a CSV representation of the Resource - def to_csv(writer = '') - CSV.generate(writer) do |csv| - row = model.properties(repository.name).map do |property| + def to_csv(*args) + options = args.first || {} + options = options.to_h if options.respond_to?(:to_h) + options[:writer] = '' unless options.has_key? :writer + + CSV.generate(options[:writer]) do |csv| + row = properties_to_serialize(options).map do |property| __send__(property.name).to_s end csv << row end end module ValidationErrors module ToCsv - def to_csv(writer = '') - CSV.generate(writer) do |csv| + def to_csv(*args) + options = args.first || {} + options = options.to_h if options.respond_to?(:to_h) + options[:writer] = '' unless options.has_key? :writer + + CSV.generate(options[:writer]) do |csv| errors.each do |key, value| value.each do |error| row = [] row << key.to_s row << error.to_s @@ -43,23 +51,23 @@ end end end class Collection - def to_csv + def to_csv(*args) result = '' each do |item| - result << item.to_csv + "\n" + result << item.to_csv(args.first) + "\n" end result end end - if Serialize.dm_validations_loaded? + if Serializer.dm_validations_loaded? module Validations class ValidationErrors - include DataMapper::Serialize::ValidationErrors::ToCsv + include DataMapper::Serializer::ValidationErrors::ToCsv end end end