lib/csv_pirate.rb in csv_pirate-2.0.1 vs lib/csv_pirate.rb in csv_pirate-2.1.1

- old
+ new

@@ -156,11 +156,16 @@ end def prize(spoils) gold_doubloons = [] self.booty.each do |plunder| - gold_doubloons << spoils.send(plunder.to_sym) + # Check for nestedness + if plunder.is_a?(Hash) + gold_doubloons << CsvPirate.marlinespike(spoils, plunder) + else + gold_doubloons << spoils.send(plunder.to_sym) + end end gold_doubloons end def scrivener(msg) @@ -283,9 +288,29 @@ end ######################################## ############ CLASS METHODS ############# ######################################## + + # if this is your booty: + # {:booty => [ + # :id, + # {:region => {:country => :name }, :state => :name }, + # :name + # ]} + # so nested_hash = {:region => {:country => :name }, :state => :name } + def self.marlinespike(spoils, navigation) + navigation.map do |east,west| + spoils = spoils.send(east.to_sym) + if west.is_a?(Hash) + # Recursive nadness is here! + spoils = CsvPirate.marlinespike(spoils, west) + else + spoils = spoils.send(west.to_sym) + end + spoils + end.join(' - ') + end # Used to read any loot found by any pirate def self.rinse(quarterdeck) File.open(File.expand_path(quarterdeck), "r") do |bucket_line| bucket_line.each_line do |bucket|