module ActsAsBytefield::InstanceMethods

Public Instance Methods

bytefield_value(column, field) click to toggle source
# File lib/acts_as_bytefield.rb, line 50
def bytefield_value(column, field)
  self[column][byte_index(column, field)]
end
set_bytefield_value(column, field, value) click to toggle source
# File lib/acts_as_bytefield.rb, line 54
def set_bytefield_value(column, field, value)
  self[column] ||= ''
  null_pad(column)
  self[column][byte_index(column, field)] = guarded_value(value)
end

Private Instance Methods

byte_index(column, field) click to toggle source
# File lib/acts_as_bytefield.rb, line 70
def byte_index(column, field)
  bytefields[column][field]
end
column_length(column) click to toggle source
# File lib/acts_as_bytefield.rb, line 66
def column_length(column)
  bytefields[column].keys.count
end
guarded_value(value) click to toggle source
# File lib/acts_as_bytefield.rb, line 74
def guarded_value(value)
  if value.is_a?(String)
    value.first
  elsif value.is_a?(Fixnum)
    value.abs.chr
  else
    fail "#{value} must be a Fixnum or String"
  end
end
null_pad(column) click to toggle source
# File lib/acts_as_bytefield.rb, line 62
def null_pad(column)
  self[column] << "\0" * (column_length(column) - self[column].length)
end