Module: EasyType::Helpers

Defined in:
lib/easy_type/helper.rb

Overview

Contains a set of helpe methods and classed tha can be used throughtout EasyType

Defined Under Namespace

Classes: InstancesResults

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(parent)



6
7
8
# File 'lib/easy_type/helper.rb', line 6

def self.included(parent)
	parent.extend(Helpers)
end

Instance Method Details

- (Array) convert_csv_data_to_hash(csv_data, headers = [])

Convert a comma separated string into an Array of Hashes

Parameters:

  • csv_data (String) —

    comma separated string

  • headers (Array) (defaults to: []) —

    of [Symbols] specifying the key’s of the Hash

Returns:

  • (Array) —

    of [InstancesResults] a special Hash



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/easy_type/helper.rb', line 31

def convert_csv_data_to_hash(csv_data, headers = [])
	data = []

	csv_data.split("\n").each do | row |
		columnized = row.split(',')
		columnized.map!{|column| column.strip}
		if headers.empty?
			headers = columnized
		elsif row.include?('----')
			#do nothing
		else
			values = headers.zip(columnized)
			data << InstancesResults[values]
		end
	end
	data
end