lib/csv_pirate.rb in csv_pirate-3.1.1 vs lib/csv_pirate.rb in csv_pirate-3.3.0

- old
+ new

@@ -12,19 +12,19 @@ class CsvPirate BOOKIE = [:counter, :timestamp, :none] MOP_HEADS = [:clean, :dirty] - attr_accessor :waggoner #filename - attr_accessor :chart #directory, default is (['log','csv']) - attr_accessor :aft #extension, default is ('.csv') - attr_accessor :gibbet #part of the filename after waggoner and date, before swabbie and aft - attr_accessor :chronometer + attr_accessor :waggoner # First part of filename + attr_accessor :chart # directory, default is (['log','csv']) + attr_accessor :aft # extension, default is ('.csv') + attr_accessor :gibbet # part of the filename after waggoner and date, before swabbie and aft + attr_accessor :chronometer # Date object or false # Must provide swag or grub (not both) - attr_accessor :swag # ARrr array of objects - attr_accessor :grub # ARrr class + attr_accessor :swag # Array of objects + attr_accessor :grub # Class # spyglasses is only used with grub, not swag attr_accessor :spyglasses # named_scopes # These are the booty of the CSV # Should be methods/columns on the swag @@ -58,18 +58,18 @@ # :booty booty (columns/methods) on your model that you want printed in the CSV, also used to create the figurehead (CSV header) # :chart array of directory names (relative to rails root if using rails) which will be the filepath where you want to hide your loot # :wagonner name of document where you will give detailed descriptions of the loot # :aft filename extention ('.csv') # :shrouds CSV column separator, default is ','. For tsv, tab-delimited, "\t" - # :chronometer keeps track of when you hunt for treasure + # :chronometer keeps track of when you hunt for treasure, can be false if you don't want to keep track. # :gibbet filename spacer after the date, and before the iterative counter/timestamp. MuST contain a '.' # :swab can be :counter, :timestamp, or :none # :counter - default, each successive run will create a new file using a counter # :timestamp - each successive run will create a new file using a HHMMSS time stamp - # :none - no iterative file naming convention, just use waggoner and aft + # :none - no iterative file naming convention, just use waggoner, aft and gibbet # :mop can be :clean or :dirty (:overwrite or :append) (only has an effect if :swab is :none) since overwriting is irrelevant for a new file - # :clear - do not use :counter or :timestamp, and instead overwrite the file + # :clean - do not use :counter or :timestamp, and instead overwrite the file # :dirty - do not use :counter, or :timestamp, or :overwrite. Just keep adding on. # :bury_treasure should we store the csv data as it is collected in an array in Ruby form for later use (true), or just write the CSV (false)? # See README for examples def initialize(*args) @@ -77,37 +77,38 @@ @swag = args.first[:swag] @grub = args.first[:grub] # if they provide both - raise ArgumentError, "must provide either :swag or :grub" if !args.first[:swag].blank? && !args.first[:grub].blank? + raise ArgumentError, "must provide either :swag or :grub, not both" if !self.swag.blank? && !self.grub.blank? # if they provide neither - raise ArgumentError, "must provide either :swag or :grub, not both" if args.first[:swag].blank? && args.first[:grub].blank? + raise ArgumentError, "must provide either :swag or :grub" if self.swag.blank? && self.grub.blank? @swab = args.first[:swab] || :counter - raise ArgumentError, ":swab is #{args.first[:swab].inspect}, but must be one of #{CsvPirate::BOOKIE.inspect}" unless CsvPirate::BOOKIE.include?(args.first[:swab]) + raise ArgumentError, ":swab is #{self.swab.inspect}, but must be one of #{CsvPirate::BOOKIE.inspect}" unless CsvPirate::BOOKIE.include?(self.swab) @mop = args.first[:mop] || :clean - raise ArgumentError, ":mop is #{args.first[:mop].inspect}, but must be one of #{CsvPirate::MOP_HEADS.inspect}" unless CsvPirate::MOP_HEADS.include?(args.first[:mop]) + raise ArgumentError, ":mop is #{self.mop.inspect}, but must be one of #{CsvPirate::MOP_HEADS.inspect}" unless CsvPirate::MOP_HEADS.include?(self.mop) @gibbet = args.first[:gibbet] || '.export' - raise ArgumentError, ":gibbet is #{args.first[:gibbet].inspect}, and does not contain a '.' character, which is required for iterative filenames" if args.first[:gibbet].nil? || !args.first[:gibbet].include?('.') + raise ArgumentError, ":gibbet is #{self.gibbet.inspect}, and does not contain a '.' character, which is required when using iterative filenames (set :swab => :none to turn off iterative filenames)" if self.swab != :none && (self.gibbet.nil? || !self.gibbet.include?('.')) @waggoner = args.first[:waggoner] || "#{self.grub || self.swag}" - raise ArgumentError, ":waggoner is #{args.first[:waggoner].inspect}, and must be a string at least one character long" if args.first[:waggoner].nil? || args.first[:waggoner].length < 1 + raise ArgumentError, ":waggoner is #{self.waggoner.inspect}, and must be a string at least one character long" if self.waggoner.nil? || self.waggoner.length < 1 @booty = args.first[:booty] || [] - raise ArgumentError, ":booty is #{args.first[:booty].inspect}, and must be an array of methods to call on a class for CSV data" if args.first[:booty].nil? || !args.first[:booty].is_a?(Array) || args.first[:booty].empty? + raise ArgumentError, ":booty is #{self.booty.inspect}, and must be an array of methods to call on a class for CSV data" if self.booty.nil? || !self.booty.is_a?(Array) || self.booty.empty? @chart = args.first[:chart] || ['log','csv'] - raise ArgumentError, ":chart is #{args.first[:chart].inspect}, and must be an array of directory names, which will become the filepath for the csv file" if args.first[:chart].nil? || !args.first[:chart].is_a?(Array) || args.first[:booty].empty? + raise ArgumentError, ":chart is #{self.chart.inspect}, and must be an array of directory names, which will become the filepath for the csv file" if self.chart.nil? || !self.chart.is_a?(Array) || self.chart.empty? @aft = args.first[:aft] || '.csv' - @chronometer = args.first[:chronometer] || Date.today + @chronometer = args.first[:chronometer] == false ? false : args.first[:chronometer] || Date.today @spyglasses = (args.first[:spyglasses] || [:all]) if self.grub @shrouds = args.first[:shrouds] || ',' # for tsv, tab-delimited, "\t" + raise ArgumentError, ":shrouds is #{self.shrouds.inspect}, and must be a string (e.g. ',' or '\t'), which will be used as the delimeter for the csv file" if self.shrouds.nil? || !self.shrouds.is_a?(String) @astrolabe = args.first[:astrolabe] || false @bury_treasure = args.first[:astrolabe] || false @buried_treasure = [] @@ -196,11 +197,11 @@ self.dead_mans_chest self.rhumb_lines.close - self.jolly_roger if CsvPirate.parlay > 1 + self.jolly_roger if CsvPirate.parlay && CsvPirate.parlance(1) # returns the text of this CSV export return self.maroon end @@ -213,14 +214,14 @@ end def jolly_roger if self.bury_treasure if self.buried_treasure.is_a?(Array) - puts "Found #{self.buried_treasure.length} deniers buried here: '#{self.poop_deck}'" if CsvPirate.parlay > 1 - puts "You must weigh_anchor to review your plunder!" if CsvPirate.parlay > 1 + puts "Found #{self.buried_treasure.length} deniers buried here: '#{self.poop_deck}'" if CsvPirate.parlay && CsvPirate.parlance(1) + puts "You must weigh_anchor to review your plunder!" if CsvPirate.parlay && CsvPirate.parlance(1) else - puts "Failed to locate treasure" if CsvPirate.parlay > 1 + puts "Failed to locate treasure" if CsvPirate.parlay && CsvPirate.parlance(1) end end end def sounding(csv) @@ -369,11 +370,11 @@ # Sink other ships! Or run a block of code on each row of a CSV def self.broadside(galley, &block) return false unless block_given? count = 1 if report_kills FasterCSV.foreach(galley, {:headers => :first_row, :return_headers => false}) do |gun| - puts "Galleys sunk: #{count+=1}" if CsvPirate.parlay > 1 + puts "Galleys sunk: #{count+=1}" if CsvPirate.parlance(1) yield gun end end # During a mutiny things are a little different! @@ -419,45 +420,50 @@ cutthroat = CsvPirate.new(first_mate) cutthroat.figurehead carrack.scuttle do |cutlass| - puts "CUTLASS: #{cutlass.inspect}" if CsvPirate.parlay > 2 - puts "CARRACK.SWAG: #{carrack.swag.inspect}" if CsvPirate.parlay > 2 + puts "CUTLASS: #{cutlass.inspect}" if CsvPirate.parlance(2) + puts "CARRACK.SWAG: #{carrack.swag.inspect}" if CsvPirate.parlance(2) backstaff = cutlass[carrack.swag] || cutlass["#{carrack.spyglasses}"] - puts "BACKSTAFF: #{backstaff}" if CsvPirate.parlay > 2 - puts "CARRACK.SPYGLASSES: #{carrack.spyglasses.inspect}" if CsvPirate.parlay > 2 + puts "BACKSTAFF: #{backstaff}" if CsvPirate.parlance(2) + puts "CARRACK.SPYGLASSES: #{carrack.spyglasses.inspect}" if CsvPirate.parlance(2) gully = carrack.grub.send("find_by_#{carrack.spyglasses}".to_sym, backstaff) - puts "GULLY: #{gully.inspect}" if CsvPirate.parlay > 2 + puts "GULLY: #{gully.inspect}" if CsvPirate.parlance(2) if gully flotsam = cutthroat.grub.is_a?(String) ? gully.send(cutthroat.grub.to_sym) : cutthroat.grub.is_a?(Symbol) ? gully.send(cutthroat.grub) : cutthroat.grub.class == carrack.grub.class ? gully : cutthroat.grub.class == Class ? cutthroat.grub.send("find_by_#{cutthroat.swag}", gully.send(cutthroat.spyglasses)) : nil - puts "FLOTSAM: #{flotsam.inspect}" if CsvPirate.parlay > 2 + puts "FLOTSAM: #{flotsam.inspect}" if CsvPirate.parlance(2) if flotsam plunder = cutthroat.prize(flotsam) cutthroat.buried_treasure << plunder cutthroat.scrivener(plunder.map {|bulkhead| "#{bulkhead}"}.join(',')) else - puts "Unable to locate: #{cutthroat.grub} related to #{carrack.grub}.#{carrack.spyglasses} '#{gully.send(carrack.spyglasses)}'" if CsvPirate.parlay > 1 + puts "Unable to locate: #{cutthroat.grub} related to #{carrack.grub}.#{carrack.spyglasses} '#{gully.send(carrack.spyglasses)}'" if CsvPirate.parlance(1) end else - puts "Unable to locate: #{carrack.grub}.#{carrack.spyglasses} '#{gully.send(carrack.spyglasses)}'" if CsvPirate.parlay > 1 + puts "Unable to locate: #{carrack.grub}.#{carrack.spyglasses} '#{gully.send(carrack.spyglasses)}'" if CsvPirate.parlance(1) end end carrack.rhumb_lines.close cutthroat.rhumb_lines.close cutthroat.jolly_roger # returns the array that is created before exporting it to CSV return cutthroat + end + + # verbosity on a scale of 0 - 3 (0=:none, 1=:error, 2=:info, 3=:debug, 0 being no screen output, 1 is default + def self.parlance(level = 1) + self.parlay.is_a?(Numeric) && self.parlay >= level end end