Class: CodeInventory::CSVFile

Inherits:
Object
  • Object
show all
Defined in:
lib/codeinventory/csv_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csv_file) ⇒ CSVFile



7
8
9
10
# File 'lib/codeinventory/csv_file.rb', line 7

def initialize(csv_file)
  @csv = CSV.read(csv_file, { headers: true, converters: [ :integer ] })
  validate_headers
end

Instance Attribute Details

#csvObject (readonly)

Returns the value of attribute csv.



5
6
7
# File 'lib/codeinventory/csv_file.rb', line 5

def csv
  @csv
end

Instance Method Details

#projectsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/codeinventory/csv_file.rb', line 12

def projects
  @csv.collect do |row|
    csv_data = row.to_hash
    project = csv_data.inject({}) do |memo, pair|
      csv_header, csv_value = pair
      case
      when csv_header == "tags"
        new_pair = { "tags" => csv_value.split(",").collect { |tag| tag.strip } }
      when csv_header.include?(".")
        new_pair = dotted_to_nested(csv_header, csv_value)
      else
        new_pair = { csv_header => csv_value }
      end
      memo.merge(new_pair)
    end
    yield project if block_given?
    project
  end
end