module Gyunyu::Command::Export::Format::Csv

Public Class Methods

expand_fields( fields ) click to toggle source
# File lib/gyunyu/command/export/format/csv.rb, line 102
def expand_fields( fields )
  fields.map { |e|
    case e
    when 'tags', 'notes'
      num = instance_variable_get("@num_#{e}")
      if num and num > 1
        [e] + Array.new(num - 1, '')
      else
        e
      end
    else
      e
    end
  }.flatten
end
expand_tags( tags ) click to toggle source
# File lib/gyunyu/command/export/format/csv.rb, line 118
def expand_tags( tags )
  @tags.map { |e|
    tags.include?(e) ? e : ''
  }
end
export( tasks, fields ) click to toggle source
# File lib/gyunyu/command/export/format/csv.rb, line 22
def export( tasks, fields )
  sum_estimate( tasks, fields )

  FasterCSV.generate { |csv|
    tasks, fields = parse( tasks, fields )
    csv << fields
    fields.delete('')
    tasks.each { |t|
      csv << fields.map { |f|
        case f
        when 'tags'
          t[f] = expand_tags( t[f] )
        when 'notes'
          num = instance_variable_get( "@num_#{f}" )
          while t[f].size < num
            t[f] += ['']
          end
        end
        t[f]
      }.flatten
    }

    if @total_estimate
      csv << fields.map { |f|
        @total_estimate.has_key?(f) ? @total_estimate[f] : ''
      }
    end
  }
end
parse( tasks, fields ) click to toggle source
# File lib/gyunyu/command/export/format/csv.rb, line 52
def parse( tasks, fields )
  if !fields.include?( 'tags' ) and !fields.include?( 'notes' )
    [tasks, fields]
  else
    if fields.include?( 'tags' )
      tasks = tasks.map { |t| parse_tags( t ) }
    end
    if fields.include?( 'notes' )
      tasks = tasks.map { |t| parse_notes( t ) }
    end

    [tasks, expand_fields( fields )]
  end
end
parse_notes( t ) click to toggle source
# File lib/gyunyu/command/export/format/csv.rb, line 80
def parse_notes( t )
  notes = if t['notes'].first.has_key? 'note'
            t['notes'].first['note'].map { |n|
              n['content']
            }
          else
            []
          end
  if !@num_notes or notes.size > @num_notes
    @num_notes = notes.size
  end
  t['notes'] = notes

  t
end
parse_tags( t ) click to toggle source
# File lib/gyunyu/command/export/format/csv.rb, line 67
def parse_tags( t )
  tags = if t['tags'].first.has_key? 'tag'
           t['tags'].first['tag']
         else
           []
         end
  update_tags_field( tags )

  t['tags'] = tags.sort

  t
end
update_tags_field( tags ) click to toggle source
# File lib/gyunyu/command/export/format/csv.rb, line 96
def update_tags_field( tags )
  @tags = (@tags | tags).sort

  @num_tags = @tags.size
end