module CsvPirate::PirateShip::ActMethods
Attributes
piratey_options[RW]
Public Instance Methods
has_csv_pirate_ship(options = {})
click to toggle source
coding style is adopted from attachment_fu
# File lib/csv_pirate/pirate_ship.rb, line 6 def has_csv_pirate_ship(options = {}) check_booty = prevent_from_failing_pre_migration options[:chart] ||= ['log','csv'] options[:aft] ||= '.csv' options[:gibbet] ||= '.export' #Needs to be defined at runtime, doesn't make sense here unless set to false, except to be overridden at runtime # Default is nil so that the CsvPirate default will kick-in. options[:chronometer] ||= nil options[:waggoner] ||= "#{self}" options[:swag] ||= nil options[:swab] ||= :counter options[:mop] ||= :clean options[:shrouds] ||= ',' options[:grub] ||= self if options[:swag].nil? options[:spyglasses] ||= (self.respond_to?(:all) ? [:all] : nil) if options[:grub] # Have to protect against model definitions pre-migration, since column names will fail if options[:booty] ||= check_booty ? self.column_names : [] options[:bury_treasure] ||= true options[:blackjack] ||= {:humanize => '_'} options[:parlay] ||= 1 # if they provide both raise ArgumentError, "must provide either :swag or :grub, not both" if !options[:swag].nil? && !options[:grub].nil? # if they provide neither raise ArgumentError, "must provide either :swag or :grub" if options[:swag].nil? && options[:grub].nil? raise ArgumentError, ":swab is #{options[:swab].inspect}, but must be one of #{TheCapn::BOOKIE.inspect}" unless TheCapn::BOOKIE.include?(options[:swab]) raise ArgumentError, ":mop is #{options[:mop].inspect}, but must be one of #{TheCapn::MOP_HEADS.inspect}" unless TheCapn::MOP_HEADS.include?(options[:mop]) raise ArgumentError, ":gibbet is #{options[:gibbet].inspect}, and does not contain a '.' character, which is required when using iterative filenames (set :swab => :none to turn off iterative filenames)" if options[:swab] != :none && (options[:gibbet].nil? || !options[:gibbet].include?('.')) raise ArgumentError, ":waggoner is #{options[:waggoner].inspect}, and must be a string at least one character long" if options[:waggoner].nil? || options[:waggoner].length < 1 raise ArgumentError, ":booty is #{options[:booty].inspect}, and must be an array of methods to call on a class for CSV data" if check_booty && (options[:booty].nil? || !options[:booty].is_a?(Array) || options[:booty].empty?) raise ArgumentError, ":chart is #{options[:chart].inspect}, and must be an array of directory names, which will become the filepath for the csv file" if options[:chart].nil? || !options[:chart].is_a?(Array) || options[:chart].empty? raise ArgumentError, ":shrouds is #{options[:shrouds].inspect}, and must be a string (e.g. ',' or '\t'), which will be used as the delimeter for the csv file" if options[:shrouds].nil? || !options[:shrouds].is_a?(String) raise ArgumentError, ":blackjack is #{options[:blackjack].inspect}, and must be a hash (e.g. {:humanize => '_'}), which defines how the header of the CSV will be created" if options[:blackjack].nil? || !options[:blackjack].is_a?(Hash) extend ClassMethods unless (class << self; included_modules; end).include?(ClassMethods) class << self attr_accessor :piratey_options end self.piratey_options = options end
prevent_from_failing_pre_migration()
click to toggle source
return true if we can access the tables, return false if we’d better not.
# File lib/csv_pirate/pirate_ship.rb, line 52 def prevent_from_failing_pre_migration # If you aren't using ActiveRecord (you are outside rails) then you must declare your :booty # If you are using ActiveRecord then you only want ot check for booty if the table exists so it won't fail pre-migration begin defined?(ActiveRecord) && ActiveRecord::Base.connected? ? self.respond_to?(:table_name) && ActiveRecord::Base.connection.tables.include?(self.table_name) : self.respond_to?(:column_names) #The only error that occurs here is when ActiveRecord checks for the table but the database isn't setup yet. #It was preventing rake db:create from working. rescue puts "csv_pirate: failed to connect to database, or table missing" return false end end