Class: ArcWeld::Cli::ResourceReaders::Csv

Inherits:
Object
  • Object
show all
Defined in:
lib/arc_weld/cli/readers/resource_csv.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{skip_lines: %r{\A[#;]}}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, target_class: nil, options: DEFAULT_OPTIONS) ⇒ Csv

Returns a new instance of Csv.



11
12
13
14
15
16
# File 'lib/arc_weld/cli/readers/resource_csv.rb', line 11

def initialize(path: nil, target_class: nil, options: DEFAULT_OPTIONS)
  @path, @target_class = path, target_class
  @csv = CSV.open(path, 'r', options)
  @key_proc ||= {}
  @keys = csv.readline.map { |k| k.to_sym }
end

Instance Attribute Details

#csvObject (readonly)

Returns the value of attribute csv.



8
9
10
# File 'lib/arc_weld/cli/readers/resource_csv.rb', line 8

def csv
  @csv
end

#key_procObject

Returns the value of attribute key_proc.



7
8
9
# File 'lib/arc_weld/cli/readers/resource_csv.rb', line 7

def key_proc
  @key_proc
end

#keysObject (readonly)

Returns the value of attribute keys.



8
9
10
# File 'lib/arc_weld/cli/readers/resource_csv.rb', line 8

def keys
  @keys
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/arc_weld/cli/readers/resource_csv.rb', line 7

def path
  @path
end

#target_classObject

Returns the value of attribute target_class.



7
8
9
# File 'lib/arc_weld/cli/readers/resource_csv.rb', line 7

def target_class
  @target_class
end

Instance Method Details

#each(rewind: not_at_start_record?) ) ⇒ Object



46
47
48
49
# File 'lib/arc_weld/cli/readers/resource_csv.rb', line 46

def each(rewind: not_at_start_record?)
  reset_csv if rewind
  csv.each {|arr| yield parse(arr) }
end

#not_at_start_record?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/arc_weld/cli/readers/resource_csv.rb', line 42

def not_at_start_record?
  (csv.lineno!=1)
end

#parse(array) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/arc_weld/cli/readers/resource_csv.rb', line 26

def parse(array)
  row_hash = Hash[keys.zip(array)]
  instance_hash = row_hash.select {|k,v| read_keys.include?(k)}
  new_instance = target_class.new(instance_hash)
  
  if processed_keys.empty?
    new_instance
  else
    processed_hash = row_hash.select {|k,v| read_keys.include?(k)==false }
    processed_hash.each do |processed_key, processed_value|
      key_proc.fetch(processed_key).call(processed_key,processed_value,new_instance)
    end
    new_instance
  end
end

#processed_keysObject



22
23
24
# File 'lib/arc_weld/cli/readers/resource_csv.rb', line 22

def processed_keys
  key_proc.keys
end

#read_keysObject



18
19
20
# File 'lib/arc_weld/cli/readers/resource_csv.rb', line 18

def read_keys
  keys - processed_keys
end

#reset_csvObject



56
57
58
59
60
# File 'lib/arc_weld/cli/readers/resource_csv.rb', line 56

def reset_csv
  csv.rewind
  *trash = csv.readline
  csv.lineno
end

#to_a(rewind: not_at_start_record?) ) ⇒ Object



51
52
53
54
# File 'lib/arc_weld/cli/readers/resource_csv.rb', line 51

def to_a(rewind: not_at_start_record?)
  reset_csv if rewind
  csv.readlines.map {|arr| parse(arr)}
end