Sha256: 9cd10bcf890f18cdd52958ba3bbf9a7d533b42e0769fdd74241427ca36bd26cc

Contents?: true

Size: 842 Bytes

Versions: 1

Compression:

Stored size: 842 Bytes

Contents

module IMW
  module Recordizer
    class StringSliceRecordizer

      attr_reader :schema

      def initialize ranges
        @schema = ranges
      end

      def recordize line
        format = schema
        case format
        when Array then slice_by_array(line, format)
        when Hash  then slice_by_hash(line, format)
        end
      end

      def slice_range string, range
        string.slice(range).strip
      end

      def slice_by_array string, format
        format.map { |range| slice_range(string, range) }
      end

      def slice_by_hash string, format
        format.inject({}) do |hsh, (key, val)|
          case val
          when Range then hsh[key] = slice_range(string, val)
          when Hash  then hsh[key] = slice_by_hash(string, val)
          end
          hsh
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
imw-0.3.0 lib/imw/recordizer/string_slice_recordizer.rb