Sha256: 2a18a56dc71458224794008cc821c5bd0327571c60868486902fb52a77234e74

Contents?: true

Size: 1.4 KB

Versions: 7

Compression:

Stored size: 1.4 KB

Contents

require 'slither'
class RemoteTable
  class Format
    class FixedWidth < Format
      include Textual
      def each(&blk)
        backup_file!
        convert_file_to_utf8!
        remove_useless_characters!
        crop_rows!
        skip_rows!
        cut_columns!
        parser.parse[:rows].each do |hash|
          hash.reject! { |k, v| k.blank? }
          yield hash if t.properties.keep_blank_rows or hash.any? { |k, v| v.present? }
        end
      ensure
        restore_file!
      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 }
          ::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

7 entries across 7 versions & 1 rubygems

Version Path
remote_table-1.1.2 lib/remote_table/format/fixed_width.rb
remote_table-1.1.1 lib/remote_table/format/fixed_width.rb
remote_table-1.1.0 lib/remote_table/format/fixed_width.rb
remote_table-1.0.3 lib/remote_table/format/fixed_width.rb
remote_table-1.0.2 lib/remote_table/format/fixed_width.rb
remote_table-1.0.1 lib/remote_table/format/fixed_width.rb
remote_table-1.0.0 lib/remote_table/format/fixed_width.rb