MyBankConverter Example

   [ConverterName("My good Bank")]
   public class MyBankConverter: BaseConverter, IConverter
   {
      public SimpleCsvConverter()
      {
         SourceCulture = CultureInfo.InvariantCulture;
      }

      /// <summary>
      /// Converts a source file to a YNAB CSV file.
      /// </summary>
      /// <returns>
      /// The Converter Result.
      /// </returns>
      public ConverterResult Convert(string source, string target, CreationMode mode)
      {
         // Read specific records from source file.
         IEnumerable<MyBankRecord> records= ReadRecords<MyBankRecord>(source, ',', false);

         // Convert records from source file to YNAB records.
         IEnumerable<YnabRecord> ynabRecords = ConvertRecords(records);

         // Write YNAB records to target file.
         WriteRecords(ynabRecords, target, mode);

         // Return a result
         return new ConverterResult {IsSuccess = true};
      }

      /// <summary>
      /// Converts to ynab records and calls the ConvertRecord method.
      /// </summary>
      public IEnumerable<YnabRecord> ConvertRecords(IEnumerable<MyBankRecord> records)
      {
         return records.Select(ConvertRecord).ToList();
      }

      /// <summary>
      /// Converts a single MyBankRecord to a YNAB record.
      /// </summary>
      public YnabRecord ConvertRecord(MyBankRecord record)
      {
         var ynabRecord = new YnabRecord();

         ynabRecord.SetDate(record.Date, SourceCulture);
         ynabRecord.SetInflowOutflow(record.Amount, SourceCulture);

         ynabRecord.Payee = record.Text;
         ynabRecord.Memo = string.Empty;
         ynabRecord.Category = string.Empty;

         return ynabRecord;
      }

Last edited Feb 5, 2013 at 8:57 PM by chris32, version 4

Comments

No comments yet.