Sha256: 9073319dab076ab5430913673c852f1a313a6d086670f3f9407cf2d112db0bb5

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

require 'slither'
class RemoteTable
  class Format
    class FixedWidth < Format
      include Textual
      def each(&blk)
        remove_useless_characters!
        crop_rows!
        skip_rows!
        cut_columns!
        parser.parse[:rows].each do |hash|
          hash.reject! { |k, v| k.blank? }
          hash.each do |k, v|
            hash[k] = utf8 v
          end
          yield hash if t.properties.keep_blank_rows or hash.any? { |k, v| v.present? }
        end
      ensure
        t.local_file.delete
      end
      private
      def parser
        @parser ||= ::Slither::Parser.new definition, t.local_file.path
      end
      def definition
        @definition ||= if t.properties.schema_name.is_a?(::String) or t.properties.schema_name.is_a?(::Symbol)
          ::Slither.send :definition, t.properties.schema_name
        elsif t.properties.schema.is_a?(::Array)
          everything = lambda { |_| true }
          srand # in case this was forked by resque
          ::Slither.define(rand.to_s) do |d|
            d.rows do |row|
              row.trap(&everything)
              t.properties.schema.each do |name, width, options|
                if name == 'spacer'
                  row.spacer width
                else
                  row.column name, width, options
                end
              end
            end
          end
        else
          raise "expecting schema_name to be a String or Symbol, or schema to be an Array"
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
remote_table-1.1.10 lib/remote_table/format/fixed_width.rb
remote_table-1.1.9 lib/remote_table/format/fixed_width.rb
remote_table-1.1.8 lib/remote_table/format/fixed_width.rb
remote_table-1.1.7 lib/remote_table/format/fixed_width.rb