Class: ArcWeld::Cli::RelationReaders::Csv

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/arc_weld/cli/readers/relation_csv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#constantize, #uri_join

Constructor Details

#initialize(*args) ⇒ Csv

Returns a new instance of Csv.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 12

def initialize(*args)
  Hash[*args].each {|k,v| self.send(format('%s=',k),v)}
  @csv = CSV.open(path)
  @keys = csv.readline
  @src_type, @src_select, @dst_type, @dst_select = keys.flat_map do |k|
     k.split(':')
  end
  @src_class=ArcWeld.const_get(constantize(src_type))
  @dst_class=ArcWeld.const_get(constantize(dst_type))
  self
end

Instance Attribute Details

#csvObject (readonly)

Returns the value of attribute csv.



10
11
12
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 10

def csv
  @csv
end

#dst_classObject

Returns the value of attribute dst_class.



6
7
8
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 6

def dst_class
  @dst_class
end

#dst_selectObject

Returns the value of attribute dst_select.



6
7
8
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 6

def dst_select
  @dst_select
end

#dst_typeObject (readonly)

Returns the value of attribute dst_type.



10
11
12
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 10

def dst_type
  @dst_type
end

#keysObject (readonly)

Returns the value of attribute keys.



10
11
12
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 10

def keys
  @keys
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 6

def path
  @path
end

#relationship_typeObject

Returns the value of attribute relationship_type.



6
7
8
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 6

def relationship_type
  @relationship_type
end

#src_classObject

Returns the value of attribute src_class.



6
7
8
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 6

def src_class
  @src_class
end

#src_selectObject

Returns the value of attribute src_select.



6
7
8
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 6

def src_select
  @src_select
end

#src_typeObject (readonly)

Returns the value of attribute src_type.



10
11
12
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 10

def src_type
  @src_type
end

Instance Method Details

#destinations(inputs) ⇒ Object



28
29
30
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 28

def destinations(inputs)
  inputs[dst_type]
end

#relate(inputs) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 32

def relate(inputs)
  csv.each do |selected_src, selected_dst|
    src_instance=sources(inputs).find {|s| s.send(src_select) == selected_src }
    dst_instance=destinations(inputs).find {|s| s.send(dst_select) == selected_dst}
    
    if (src_instance.nil? || dst_instance.nil?)
      STDERR.puts format('could not %s %s/%s',
                         relationship_type,
                         selected_src,
                         selected_dst)
    else
       src_instance.send(relationship_type,dst_instance)
    end
  end
end

#sources(inputs) ⇒ Object



24
25
26
# File 'lib/arc_weld/cli/readers/relation_csv.rb', line 24

def sources(inputs)
  inputs[src_type]
end