Sha256: 97f70a1f38b5421ee764849ce15e18f4577ef9b2992de11974dcd15a3c543d3d

Contents?: true

Size: 1.21 KB

Versions: 7

Compression:

Stored size: 1.21 KB

Contents

class RemoteTable
  module FixedWidth
    def each_row(&block)
      crop_rows!
      skip_rows!
      cut_columns!
      a = Slither.parse(path, schema_name)
      a[:rows].each { |row| yield HashWithIndifferentAccess.new(row) }
    ensure
      uncut_columns!
      unskip_rows!
      uncrop_rows!
    end
    
    private
    
    def cut_columns!
      return unless cut
      original = "#{path}.uncut"
      FileUtils.cp(path, original)
      `cat #{original} | cut -c #{cut} > #{path}`
    end
    
    def uncut_columns!
      return unless cut
      FileUtils.mv "#{path}.uncut", path
    end
    
    def skip_rows!
      return unless skip
      original = "#{path}.unskipped"
      FileUtils.cp(path, original)
      `cat #{original} | tail -n +#{skip + 1} > #{path}`
    end
    
    def unskip_rows!
      return unless skip
      FileUtils.mv "#{path}.unskipped", path
    end
    
    def crop_rows!
      return unless crop
      original = "#{path}.uncropped"
      FileUtils.cp(path, original)
      `cat #{original} | tail -n +#{crop.first} | head -n #{crop.last - crop.first + 1} > #{path}`
    end
    
    def uncrop_rows!
      return unless crop
      FileUtils.mv "#{path}.uncropped", path
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
seamusabshere-remote_table-0.1.0 lib/remote_table/file/fixed_width.rb
seamusabshere-remote_table-0.1.1 lib/remote_table/file/fixed_width.rb
seamusabshere-remote_table-0.1.2 lib/remote_table/file/fixed_width.rb
seamusabshere-remote_table-0.1.3 lib/remote_table/file/fixed_width.rb
seamusabshere-remote_table-0.1.4 lib/remote_table/file/fixed_width.rb
remote_table-0.1.6 lib/remote_table/file/fixed_width.rb
remote_table-0.1.5 lib/remote_table/file/fixed_width.rb