Class: DBF::Column
- Inherits:
-
Object
- Object
- DBF::Column
- Defined in:
- lib/dbf/column.rb
Defined Under Namespace
Classes: LengthError, NameError
Constant Summary collapse
- TYPE_CAST_CLASS =
{ N: ColumnType::Number, I: ColumnType::SignedLong, F: ColumnType::Float, Y: ColumnType::Currency, D: ColumnType::Date, T: ColumnType::DateTime, L: ColumnType::Boolean, M: ColumnType::Memo, B: ColumnType::Double, G: ColumnType::General }
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.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(table, name, type, length, decimal) ⇒ Column
constructor
Initialize a new DBF::Column.
-
#memo? ⇒ Boolean
Returns true if the column is a memo.
-
#schema_definition ⇒ String
Schema definition.
-
#sequel_schema_definition ⇒ String
Sequel Schema definition.
- #to_hash ⇒ Object
-
#type_cast(value) ⇒ Fixnum, ...
Cast value to native type.
-
#underscored_name ⇒ String
Underscored name.
Constructor Details
#initialize(table, name, type, length, decimal) ⇒ Column
Initialize a new DBF::Column
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/dbf/column.rb', line 31 def initialize(table, name, type, length, decimal) @table = table @name = clean(name) @type = type @length = length @decimal = decimal @version = table.version @encoding = table.encoding validate_length validate_name end |
Instance Attribute Details
#decimal ⇒ Object (readonly)
Returns the value of attribute decimal.
9 10 11 |
# File 'lib/dbf/column.rb', line 9 def decimal @decimal end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
9 10 11 |
# File 'lib/dbf/column.rb', line 9 def length @length end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/dbf/column.rb', line 9 def name @name end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
9 10 11 |
# File 'lib/dbf/column.rb', line 9 def table @table end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
9 10 11 |
# File 'lib/dbf/column.rb', line 9 def type @type end |
Instance Method Details
#memo? ⇒ Boolean
Returns true if the column is a memo
55 56 57 |
# File 'lib/dbf/column.rb', line 55 def memo? @memo ||= type == 'M' end |
#schema_definition ⇒ String
Schema definition
62 63 64 |
# File 'lib/dbf/column.rb', line 62 def schema_definition "\"#{underscored_name}\", #{schema_data_type}\n" end |
#sequel_schema_definition ⇒ String
Sequel Schema definition
69 70 71 |
# File 'lib/dbf/column.rb', line 69 def sequel_schema_definition ":#{underscored_name}, #{schema_data_type(:sequel)}\n" end |
#to_hash ⇒ Object
91 92 93 |
# File 'lib/dbf/column.rb', line 91 def to_hash {name: name, type: type, length: length, decimal: decimal} end |
#type_cast(value) ⇒ Fixnum, ...
Cast value to native type
48 49 50 |
# File 'lib/dbf/column.rb', line 48 def type_cast(value) type_cast_class.type_cast(value) end |
#underscored_name ⇒ String
Underscored name
This is the column name converted to underscore format. For example, MyColumn will be returned as my_column.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dbf/column.rb', line 79 def underscored_name @underscored_name ||= begin un = name.dup un.gsub!(/::/, '/') un.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2') un.gsub!(/([a-z\d])([A-Z])/, '\1_\2') un.tr!('-', '_') un.downcase! un end end |