Class: DataTypes::Char

Inherits:
BaseType show all
Defined in:
lib/active_model_serializers_binary/data_types.rb

Constant Summary collapse

EXTENDED_CHARACTERS =
["Ç","ü","é","â","ä","à","å","ç","ê","ë","è","ï","î","ì","Ä","Å","É","æ","Æ","ô","ö","ò","û","ù","ÿ","Ö","Ü","ø","£","Ø","×","ƒ","á","í","ó","ú","ñ","Ñ","ª","º","¿","®","¬","½","¼","¡","«","»","░","▒","▓","│","┤","Á","Â","À","©","╣","║","╗","╝","¢","¥","┐","└","┴","┬","├","─","┼","ã","Ã","╚","╔","╩","╦","╠","═","╬","¤","ð","Ð","Ê","Ë","È","ı","Í","Î","Ï","┘","┌","█","▄","¦","Ì","▀","Ó","ß","Ô","Ò","õ","Õ","µ","þ","Þ","Ú","Û","Ù","ý","Ý","¯","´","≡","±","‗","¾","¶","§","÷","¸","°","¨","·","¹","³","²","■"," "]
ASCII_TO_UTF8 =
["",('?'*31).split(''),(32..126).to_a.pack("U*").split(''),"?", EXTENDED_CHARACTERS].flatten

Instance Attribute Summary

Attributes inherited from BaseType

#bit_length, #count, #endianess, #length, #name, #parent, #raw_value, #sign, #type, #value

Instance Method Summary collapse

Methods inherited from BaseType

#after_load, #before_dump, #check, #check_raw_value, #check_value, #size, #to_s, #trim

Constructor Details

#initialize(options = {}) ⇒ Char

Returns a new instance of Char.



173
174
175
# File 'lib/active_model_serializers_binary/data_types.rb', line 173

def initialize(options = {})
  super options.merge :bit_length => 8, :sign => nil, :default_value => "\x0"
end

Instance Method Details

#dump(value = nil) ⇒ Object

to bytes



180
181
182
183
184
# File 'lib/active_model_serializers_binary/data_types.rb', line 180

def dump(value=nil) # to bytes
  before_dump( value )
  @raw_value = @value.map{|v| v.ljust(@length, @default_value).slice(0,@length)}.first.split("").map{|x| x=="\u0000" ? 0 : ASCII_TO_UTF8.index(x)}
  @raw_value
end

#load(raw_value) ⇒ Object

from bytes



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/active_model_serializers_binary/data_types.rb', line 186

def load(raw_value) # from bytes       
  if !value.nil?
    characters = []
    check_raw_value(raw_value).each do |x|
      break if x == 0
      characters << ASCII_TO_UTF8[x]
    end
    self.value = characters.empty? ? "" : characters.join[0...@length]
  end
  after_load
end