class RemoteTable class File attr_accessor :filename, :format, :delimiter, :skip, :cut, :crop, :sheet, :headers, :schema, :schema_name, :trap attr_accessor :path def initialize(bus) @filename = bus[:filename] @format = bus[:format] || format_from_filename @delimiter = bus[:delimiter] @sheet = bus[:sheet] || 0 @skip = bus[:skip] # rows @crop = bus[:crop] # rows @cut = bus[:cut] # columns @headers = bus[:headers] @schema = bus[:schema] @schema_name = bus[:schema_name] @trap = bus[:trap] extend "RemoteTable::#{format.to_s.camelcase}".constantize end def tabulate(path) define_fixed_width_schema! if format == :fixed_width and schema.is_a?(Array) # TODO move to generic subclass callback self.path = path self end private # doesn't support trap or spacer def define_fixed_width_schema! raise "can't define both schema_name and schema" if !schema_name.blank? self.schema_name = "autogenerated_#{filename.gsub(/[^a-z0-9_]/i, '')}".to_sym self.trap ||= lambda { true } Slither.define schema_name do |d| d.rows do |row| row.trap(&trap) schema.each do |name, width, options| if name == :spacer row.spacer width else row.column name, width, options end end end end end def format_from_filename extname = ::File.extname(filename).gsub('.', '') return :csv if extname.blank? format = [ :xls, :ods ].detect { |i| i == extname.to_sym } format = :csv if format.blank? format end end end