Class: RBase::Columns::NumberColumn

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 = {}) ⇒ NumberColumn

Returns a new instance of NumberColumn.



112
113
114
115
116
117
# File 'lib/rbase/columns.rb', line 112

def initialize(name, options = {})
  size = options[:size] || 18
  size = 18 if size > 18
  
  super name, options.merge(:size => size)
end

Instance Method Details

#float?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/rbase/columns.rb', line 144

def float?
  decimal && decimal != 0
end

#inspectObject



136
137
138
139
140
141
142
# File 'lib/rbase/columns.rb', line 136

def inspect
  if float?
    "#{name}(decimal)"
  else
    "#{name}(integer)"
  end
end

#pack(value) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rbase/columns.rb', line 119

def pack(value)
  if value
    if float?
      [format("%#{size-decimal-1}.#{decimal}f", value)].pack("A#{size}")
    else
      [format("%#{size}d", value)].pack("A#{size}")
    end
  else
    " "*size
  end
end

#unpack(data) ⇒ Object



131
132
133
134
# File 'lib/rbase/columns.rb', line 131

def unpack(data)
  return nil if data.strip == ''
  data.rstrip.to_i
end