Sha256: 35ceeffcc9f813bdb9a3aedb29edf79ea267aee566ff53e75f685648959bf9ec

Contents?: true

Size: 1.76 KB

Versions: 4

Compression:

Stored size: 1.76 KB

Contents

class RemoteTable
  class File
    attr_accessor :filename, :format, :delimiter, :skip, :cut, :crop, :sheet, :headers, :schema, :schema_name, :trap
    attr_accessor :path
    attr_accessor :keep_blank_rows
    
    def initialize(bus)
      @filename = bus[:filename]
      @format = bus[:format] || format_from_filename
      @delimiter = bus[:delimiter]
      @sheet = bus[:sheet] || 0
      @skip = bus[:skip] # rows
      @keep_blank_rows = bus[:keep_blank_rows] || false
      @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
    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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
remote_table-0.2.3 lib/remote_table/file.rb
remote_table-0.2.2 lib/remote_table/file.rb
remote_table-0.2.1 lib/remote_table/file.rb
remote_table-0.2.0 lib/remote_table/file.rb