Sha256: 94801ed9a291523953b5a2052101211eee175b704b396fa39fa18ed4e85f226e

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

require 'csv'
require "twstats/tw_log"

module Twstats
  class CSVReader
    attr_reader :logs
    attr_reader :tags
    attr_reader :people
    attr_reader :tasks
    attr_reader :projects
    # Create an object structure from the csv file which allows to easily read the content
    def initialize(file)
      @logs = Set.new([])
      @tags = Set.new([])
      @people = Set.new([])
      @tasks = Set.new([])
      @projects = Set.new([])
      # Initialize the reader with a csv file and fill the times as an array of columns
      CSV.foreach(file, DEFAULT_OPTIONS) do |row|
        update_info TwLog.new(row)
      end
    end

    def update_info(log)
      @projects << log.project
      @people << log.who
      log.tags.each{|x| @tags << x}
      @tasks << log.task
      @logs << log
    end

    def get_total_time(object, element)
      case object
        when :projects
          logs.select{|log| log.project == element}.inject(0){ |sum, l| sum + l.decimal_time}
        when :tags
          logs.select{|log| log.tags.include?(element)}.inject(0){ |sum, l| sum + l.decimal_time}
        when :people
          logs.select{|log| log.who == element}.inject(0){ |sum, l| sum + l.decimal_time}
        when :tasks
          logs.select{|log| log.task == element}.inject(0){ |sum, l| sum + l.decimal_time}
        else
          -99999
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twstats-0.1.4 lib/twstats/csv_reader.rb