Class: ADT::Column
Instance Attribute Summary collapse
-
#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
- #data_type(id) ⇒ Object
-
#decode_datetime(value) ⇒ DateTime
Decode a DateTime value.
- #flag(type, length = 0) ⇒ Object
-
#initialize(name, type, length) ⇒ Column
constructor
Initialize a new ADT::Column.
-
#schema_data_type ⇒ String
Column type for schema definition.
-
#schema_definition ⇒ String
Schema definition.
-
#strip_non_ascii_chars(s) ⇒ String
Strip all non-ascii and non-printable characters.
Constructor Details
#initialize(name, type, length) ⇒ Column
Initialize a new ADT::Column
17 18 19 20 21 22 |
# File 'lib/adt/column.rb', line 17 def initialize(name, type, length) @name, @type, @length = strip_non_ascii_chars(name), type, length raise ColumnLengthError, "field length must be greater than 0" unless length > 0 raise ColumnNameError, "column name cannot be empty" if @name.length == 0 end |
Instance Attribute Details
#length ⇒ Object (readonly)
Returns the value of attribute length.
10 11 12 |
# File 'lib/adt/column.rb', line 10 def length @length end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/adt/column.rb', line 10 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
10 11 12 |
# File 'lib/adt/column.rb', line 10 def type @type end |
Instance Method Details
#data_type(id) ⇒ Object
24 25 26 |
# File 'lib/adt/column.rb', line 24 def data_type(id) TYPES[id] end |
#decode_datetime(value) ⇒ DateTime
Decode a DateTime value
41 42 43 44 45 |
# File 'lib/adt/column.rb', line 41 def decode_datetime(value) days, milliseconds = value.unpack('l2') seconds = milliseconds / 1000 DateTime.jd(days, seconds/3600, seconds/60 % 60, seconds % 60) rescue nil end |
#flag(type, length = 0) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/adt/column.rb', line 28 def flag(type, length = 0) data_type = data_type(type) flag = FLAGS[data_type] if flag.eql? 'A' return flag + length.to_s end return flag end |
#schema_data_type ⇒ String
Column type for schema definition
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/adt/column.rb', line 57 def schema_data_type case data_type(type) when "character" ":string, :limit => #{length}" when "cicharacter" ":string, :limit => #{length}" when "double" ":float" when "date" ":date" when "time" ":timestamp" when "timestamp" ":timestamp" when "integer" ":integer" when "autoinc" ":integer" else ":string, :limit => #{length}" end end |
#schema_definition ⇒ String
Schema definition
50 51 52 |
# File 'lib/adt/column.rb', line 50 def schema_definition "\"#{name.underscore}\", #{schema_data_type}\n" end |
#strip_non_ascii_chars(s) ⇒ String
Strip all non-ascii and non-printable characters
84 85 86 87 88 89 |
# File 'lib/adt/column.rb', line 84 def strip_non_ascii_chars(s) # truncate the string at the first null character s = s[0, s.index("\x00")] if s.index("\x00") s.gsub(/[^\x20-\x7E]/,"") end |