Class: DBF::Column

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

Parameters:

  • table (String)
  • name (String)
  • type (String)
  • length (Integer)
  • decimal (Integer)


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dbf/column.rb', line 36

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.



11
12
13
# File 'lib/dbf/column.rb', line 11

def decimal
  @decimal
end

#lengthObject (readonly)

Returns the value of attribute length.



11
12
13
# File 'lib/dbf/column.rb', line 11

def length
  @length
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/dbf/column.rb', line 11

def name
  @name
end

#tableObject (readonly)

Returns the value of attribute table.



11
12
13
# File 'lib/dbf/column.rb', line 11

def table
  @table
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/dbf/column.rb', line 11

def type
  @type
end

Instance Method Details

#memo?Boolean

Returns true if the column is a memo

Returns:

  • (Boolean)


52
53
54
# File 'lib/dbf/column.rb', line 52

def memo?
  @memo ||= type == 'M'
end

#to_hashHash

Returns a Hash with :name, :type, :length, and :decimal keys

Returns:

  • (Hash)


59
60
61
# File 'lib/dbf/column.rb', line 59

def to_hash
  {name: name, type: type, length: length, decimal: decimal}
end

#underscored_nameString

Underscored name

This is the column name converted to underscore format. For example, MyColumn will be returned as my_column.

Returns:

  • (String)


69
70
71
72
73
74
75
76
77
# File 'lib/dbf/column.rb', line 69

def underscored_name
  @underscored_name ||= begin
    name.gsub(/::/, '/')
      .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
      .gsub(/([a-z\d])([A-Z])/, '\1_\2')
      .tr('-', '_')
      .downcase
  end
end