Sha256: 7eb71ee459f01954273c5b3bf0de9cb7d37125239ed3908c62366e45c248c873

Contents?: true

Size: 1020 Bytes

Versions: 1

Compression:

Stored size: 1020 Bytes

Contents

module Packable
  module Extensions #:nodoc:
    module Float #:nodoc:
      def self.included(base)
        base.class_eval do
          include Packable
          extend ClassMethods
          packers do |p|
            p.set :merge_all, :precision => :single, :endian => :big
            p.set :double   , :precision => :double
            p.set :float    , {}
            p.set :default  , :float
          end
        end
      end
      
      def write_packed(io, options)
        io << pack(self.class.pack_option_to_format(options))
      end

      module ClassMethods #:nodoc:
        def pack_option_to_format(options)
          format = {:big => "G", :small => "E"}[options[:endian]]
          format.downcase! if options[:precision] == :single
          format
        end

        def read_packed(io, options)
          io.read({:single => 4, :double => 8}[options[:precision]])   \
            .unpack(pack_option_to_format(options))   \
            .first
        end
      end
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
marcandre-packable-1.1.1 lib/packable/extensions/float.rb