Sha256: f398b4ffffd7fcc8560c5cc071284677cda8a48ff63f3b233f4c4981fa87ca58

Contents?: true

Size: 661 Bytes

Versions: 7

Compression:

Stored size: 661 Bytes

Contents

module Datev
  class StringField < Field
    def limit
      options[:limit]
    end

    def validate!(value)
      super

      if value
        raise ArgumentError.new("Value given for field '#{name}' is not a String") unless value.is_a?(String)
        raise ArgumentError.new("Value '#{value}' for field '#{name}' is too long") if limit && value.length > limit
      end
    end

    def output(value, _context=nil)
      value = value.slice(0, limit || 255) if value

      quote(value)
    end

  private

    def quote(value)
      # Existing quotes have to be doubled
      value = value.gsub('"','""') if value

      "\"#{value}\""
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
datev-0.8.2 lib/datev/field/string_field.rb
datev-0.8.1 lib/datev/field/string_field.rb
datev-0.8.0 lib/datev/field/string_field.rb
datev-0.7.0 lib/datev/field/string_field.rb
datev-0.6.0 lib/datev/field/string_field.rb
datev-0.5.1 lib/datev/field/string_field.rb
datev-0.5.0 lib/datev/field/string_field.rb