lib/rbbt/util/tsv/index.rb in rbbt-util-2.0.1 vs lib/rbbt/util/tsv/index.rb in rbbt-util-2.1.0
- old
+ new
@@ -137,18 +137,18 @@
# Determine new fields
# both have fields => list of names
# not both have fields => nil
-# fields2add = case
-# when (fields2add.nil? and (other.fields.nil? or self.fields.nil?))
-# nil
-# when fields2add.nil?
-# other.all_fields
-# else
-# fields2add
-# end
+ # fields2add = case
+ # when (fields2add.nil? and (other.fields.nil? or self.fields.nil?))
+ # nil
+ # when fields2add.nil?
+ # other.all_fields
+ # else
+ # fields2add
+ # end
# Determine common fields
common_fields, new_fields = case
when fields2add != nil
@@ -169,10 +169,12 @@
when TSV === match
raise "No field info in match TSV" if match.fields.nil?
match_source = (all_fields & match.all_fields).first
index = match.index :target => other.key_field, :fields => match_source
[match_source, index]
+ when (String === match and match == key_field)
+ [:key, other.index]
when String === match
[match, other.index]
when Array === match
[match.first, other.index(:fields => match.last)]
end
@@ -180,36 +182,46 @@
match_source_position = identify_field match_source
# through
new = {}
each do |key,values|
- source_keys = match_source == :key ? key : values[match_source]
+ source_keys = match_source == :key ? key : values[match_source_position]
source_keys = [source_keys] unless Array === source_keys
other_keys = case
- when index.nil?
- source_keys
- else
- index.values_at(*source_keys).flatten.compact
- end
+ when index.nil?
+ source_keys
+ else
+ index.values_at(*source_keys).flatten.compact
+ end
+
other_keys = other_keys.collect do |other_key| match_index[other_key] end.flatten unless match_index.nil?
+
other_values = other_keys.collect do |other_key|
next unless other.include? other_key
new_fields.collect do |field|
if field == other.key_field
- other_key
+ if type == :double
+ [other_key]
+ else
+ other_key
+ end
else
other[other_key][field]
end
end
end.compact
- if type == :double
- new_values = values + TSV.zip_fields(other_values)
- else
- new_values = values + TSV.zip_fields(other_values).collect{|v| v.first}
- end
+ other_values = case
+ when type == :double
+ TSV.zip_fields(other_values).collect{|v| v.flatten.uniq}
+ else
+ TSV.zip_fields(other_values).collect{|v| v.flatten.first}
+ end
+
+ new_values = values + other_values
+
new[key] = new_values
end
new = TSV.new new
new.fields = fields + new_fields if fields
@@ -230,10 +242,10 @@
field_values = {}
fields.each{|field|
field_values[field] = []
}
- if type == :double
+ if tsv.type == :double
tsv.through do |key,entry_values|
fields.zip(entry_values).each do |field,entry_field_values|
field_values[field].concat entry_field_values
end
end