Module: Gyunyu::Command::Export::Format::Csv

Extended by:
TotalEstimate
Defined in:
lib/gyunyu/command/export/format/csv.rb

Class Method Summary collapse

Methods included from TotalEstimate

estimate_fields, have_estimate_fields?, moveup_minute, sum_estimate

Class Method Details

.expand_fields(fields) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gyunyu/command/export/format/csv.rb', line 103

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) ⇒ Object



119
120
121
122
123
# File 'lib/gyunyu/command/export/format/csv.rb', line 119

def expand_tags( tags )
  @tags.map { |e|
    tags.include?(e) ? e : ''
  }
end

.export(tasks, fields) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gyunyu/command/export/format/csv.rb', line 23

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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gyunyu/command/export/format/csv.rb', line 53

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) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gyunyu/command/export/format/csv.rb', line 81

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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gyunyu/command/export/format/csv.rb', line 68

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) ⇒ Object



97
98
99
100
101
# File 'lib/gyunyu/command/export/format/csv.rb', line 97

def update_tags_field( tags )
  @tags = (@tags | tags).sort

  @num_tags = @tags.size
end