Class: RBase::Columns::CharacterColumn

Inherits:
Column
  • Object
show all
Defined in:
lib/rbase/columns.rb

Instance Attribute Summary

Attributes inherited from Column

#decimal, #name, #offset, #size

Instance Method Summary collapse

Methods inherited from Column

#attach_to, column_for, column_type, #type, type

Constructor Details

#initialize(name, options = {}) ⇒ CharacterColumn

Returns a new instance of CharacterColumn.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rbase/columns.rb', line 78

def initialize(name, options = {})
  if options[:size] && options[:decimal]
    size = options[:decimal]*256 + options[:size] 
  else
    size = options[:size] || 254
  end
  
  super name, options.merge(:size => size)

  if options[:encoding]
    @unpack_converter = Iconv.new('utf-8', options[:encoding])
    @pack_converter = Iconv.new(options[:encoding], 'utf-8')
  end
end

Instance Method Details

#inspectObject



105
106
107
# File 'lib/rbase/columns.rb', line 105

def inspect
  "#{name}(string #{size})"
end

#pack(value) ⇒ Object



93
94
95
96
97
# File 'lib/rbase/columns.rb', line 93

def pack(value)
	value = value.to_s
  value = @pack_converter.iconv(value) if @pack_converter
  [value].pack("A#{size}")
end

#unpack(data) ⇒ Object



99
100
101
102
103
# File 'lib/rbase/columns.rb', line 99

def unpack(data)
  value = data.rstrip
  value = @unpack_converter.iconv(value) if @unpack_converter
  value
end