Class: Datev::StringField

Inherits:
Field
  • Object
show all
Defined in:
lib/datev/field/string_field.rb

Instance Attribute Summary

Attributes inherited from Field

#block, #name, #options

Instance Method Summary collapse

Methods inherited from Field

#initialize, #required?

Constructor Details

This class inherits a constructor from Datev::Field

Instance Method Details

#limitObject



3
4
5
# File 'lib/datev/field/string_field.rb', line 3

def limit
  options[:limit]
end

#output(value, _context = nil) ⇒ Object



21
22
23
24
25
# File 'lib/datev/field/string_field.rb', line 21

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

  quote(value)
end

#regexObject



7
8
9
# File 'lib/datev/field/string_field.rb', line 7

def regex
  options[:regex]
end

#validate!(value) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/datev/field/string_field.rb', line 11

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