Sha256: d93e1f2bd03028a4ac80ebba43838528f7f0056d645844469de848033fe9c176

Contents?: true

Size: 826 Bytes

Versions: 3

Compression:

Stored size: 826 Bytes

Contents

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

    def regex
      options[:regex]
    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
        raise ArgumentError.new("Value '#{value}' for field '#{name}' does not match regex") if regex && value !~ regex
      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

3 entries across 3 versions & 1 rubygems

Version Path
datev-0.11.0 lib/datev/field/string_field.rb
datev-0.10.0 lib/datev/field/string_field.rb
datev-0.9.0 lib/datev/field/string_field.rb