Class: DBF::Column
- Inherits:
-
Object
- Object
- DBF::Column
- Defined in:
- lib/dbf/column.rb
Instance Attribute Summary collapse
-
#decimal ⇒ Object
readonly
Returns the value of attribute decimal.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #decode_datetime(value) ⇒ Object
-
#initialize(name, type, length, decimal) ⇒ Column
constructor
A new instance of Column.
- #schema_definition ⇒ Object
- #type_cast(value) ⇒ Object
- #unpack_integer(value) ⇒ Object
Constructor Details
#initialize(name, type, length, decimal) ⇒ Column
Returns a new instance of Column.
8 9 10 11 |
# File 'lib/dbf/column.rb', line 8 def initialize(name, type, length, decimal) raise ColumnLengthError, "field length must be greater than 0" unless length > 0 @name, @type, @length, @decimal = strip_non_ascii_chars(name), type, length, decimal end |
Instance Attribute Details
#decimal ⇒ Object (readonly)
Returns the value of attribute decimal.
6 7 8 |
# File 'lib/dbf/column.rb', line 6 def decimal @decimal end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
6 7 8 |
# File 'lib/dbf/column.rb', line 6 def length @length end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/dbf/column.rb', line 6 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/dbf/column.rb', line 6 def type @type end |
Instance Method Details
#decode_datetime(value) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/dbf/column.rb', line 32 def decode_datetime(value) days, milliseconds = value.unpack('l2') hours = (milliseconds / MS_PER_HOUR).to_i minutes = ((milliseconds - (hours * MS_PER_HOUR)) / MS_PER_MINUTE).to_i seconds = ((milliseconds - (hours * MS_PER_HOUR) - (minutes * MS_PER_MINUTE)) / MS_PER_SECOND).to_i DateTime.jd(days, hours, minutes, seconds) end |
#schema_definition ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/dbf/column.rb', line 44 def schema_definition data_type = case type when "N" # number if decimal > 0 ":float" else ":integer" end when "I" # integer ":integer" when "D" # date ":date" when "T" # datetime ":datetime" when "L" # boolean ":boolean" when "M" # memo ":text" else ":string, :limit => #{length}" end "\"#{name.underscore}\", #{data_type}\n" end |
#type_cast(value) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/dbf/column.rb', line 13 def type_cast(value) value = value.is_a?(Array) ? value.first : value case type when 'N' # number decimal.zero? ? unpack_integer(value) : value.to_f when 'D' # date value.to_date unless value.blank? when 'L' # logical value.strip =~ /^(y|t)$/i ? true : false when 'I' # integer unpack_integer(value) when 'T' # datetime decode_datetime(value) else value.to_s.strip end end |
#unpack_integer(value) ⇒ Object
40 41 42 |
# File 'lib/dbf/column.rb', line 40 def unpack_integer(value) value.unpack('v').first.to_i end |