Class: DBF::Column

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#decimalObject (readonly)

Returns the value of attribute decimal.



9
10
11
# File 'lib/dbf/column.rb', line 9

def decimal
  @decimal
end

#lengthObject (readonly)

Returns the value of attribute length.



9
10
11
# File 'lib/dbf/column.rb', line 9

def length
  @length
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/dbf/column.rb', line 9

def name
  @name
end

#tableObject (readonly)

Returns the value of attribute table.



9
10
11
# File 'lib/dbf/column.rb', line 9

def table
  @table
end

#typeObject (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_definitionString

Schema definition



62
63
64
# File 'lib/dbf/column.rb', line 62

def schema_definition
  "\"#{underscored_name}\", #{schema_data_type}\n"
end

#sequel_schema_definitionString

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_hashObject



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_nameString

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