Class: Mingle::BinCodec::MingleBinCodec

Inherits:
BitGirder::Core::BitGirderClass show all
Includes:
BitGirder::Core, Mingle, Codec::MingleCodecImpl
Defined in:
lib/mingle/bincodec.rb

Constant Summary collapse

BYTE_ORDER =
Io::ORDER_LITTLE_ENDIAN

Constants included from Mingle

COMPARABLE_TYPES, ID_STYLES, ID_STYLE_LC_CAMEL_CAPPED, ID_STYLE_LC_HYPHENATED, ID_STYLE_LC_UNDERSCORE, INT_TYPES, NUM_TYPES, PARSED_TYPES, QNAME_RESOLV_MAP, USE_ICONV

Constants included from BitGirder::Core

BitGirder::Core::ENV_BITGIRDER_DEBUG, BitGirder::Core::EXIT_FAILURE, BitGirder::Core::EXIT_SUCCESS

Constants included from BitGirder::Core::BitGirderMethods

BitGirder::Core::BitGirderMethods::PARAM_TYPE_ARG, BitGirder::Core::BitGirderMethods::PARAM_TYPE_ENVVAR, BitGirder::Core::BitGirderMethods::PARAM_TYPE_KEY

Instance Method Summary collapse

Methods included from Mingle

cast_value, quote_value

Methods included from BitGirder::Core::BitGirderMethods

argv_to_argh, check_fail_prefix, class_name_to_sym, code, compares_to, console, ext_to_class_name, ext_to_sym, has_env, has_key, has_keys, nonnegative, not_nil, positive, raisef, set_from_key, set_var, split_argv, sym_to_cli_switch, sym_to_ext_id, to_bool, unpack_argv_array, unpack_argv_hash, warn

Methods included from BitGirder::Core::BitGirderStructure

#==, included

Constructor Details

#initializeMingleBinCodec

Returns a new instance of MingleBinCodec.



37
38
39
# File 'lib/mingle/bincodec.rb', line 37

def initialize
    @conv = Io::BinaryConverter.new( :order => BYTE_ORDER )
end

Instance Method Details

#as_buffer(obj) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/mingle/bincodec.rb', line 201

def as_buffer( obj )
    
    not_nil( obj, :obj )
    obj.is_a?( MingleStruct ) or codec_raise( "Not a struct: #{obj}" )

    buf = RubyVersions.when_19x( StringIO.new ) do |io|
        io.set_encoding( "binary" )
    end

    wr = Io::BinaryWriter.new( :order => BYTE_ORDER, :io => buf )

    append_struct( wr, obj )

    buf.string
end

#from_buffer(buf) ⇒ Object



427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/mingle/bincodec.rb', line 427

def from_buffer( buf )
 
    validate_from_buffer_args( buf )

    scanner = Io::BinaryReader.new( 
        :order => BYTE_ORDER, :io => StringIO.new( buf ) )

    if ( res = read_value( scanner ) ).is_a?( MingleStruct )
        res
    else
        raise "Decode res wasn't a struct; got #{res.class}" 
    end
end