Class: Cpr::NumericField

Inherits:
Field
  • Object
show all
Defined in:
lib/cpr/numeric_field.rb

Instance Attribute Summary

Attributes inherited from Field

#default, #name, #range

Instance Method Summary collapse

Methods inherited from Field

create_type, #fill, #initialize, #length, #pattern, #read, #write

Constructor Details

This class inherits a constructor from Cpr::Field

Instance Method Details

#cast_for_read(val) ⇒ Object



4
5
6
# File 'lib/cpr/numeric_field.rb', line 4

def cast_for_read(val)
  val.to_i if val =~ /^\d+$/
end

#cast_for_write(val) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/cpr/numeric_field.rb', line 8

def cast_for_write(val)
  if val.nil?
    fill
  else
    str = "%0#{length}d" % val
    raise "Value #{val} overflows length #{length}" if str.length > length
    str
  end
end