Sha256: 7736ec8194f8ec1acf4987a94892656a44c3b1cf5636e81e0624b2e40fcb9189

Contents?: true

Size: 1.48 KB

Versions: 13

Compression:

Stored size: 1.48 KB

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:
        ENDIAN_TO_FORMAT = Hash.new{|h, endian| raise ArgumentError, "Endian #{endian} is not valid. It must be one of #{h.keys.join(', ')}."}.
          merge!(:big => "G", :network => "G", :little => "E", :native => "D").freeze

        FORMAT_TO_SINGLE_PRECISION = {'G' => 'g', 'E' => 'e', 'D' => 'f'}.freeze

        PRECISION = Hash.new{|h, precision| raise ArgumentError, "Precision #{precision} is not valid. It must be one of #{h.keys.join(', ')}."}.
          merge!(:single => 4, :double => 8).freeze

        def pack_option_to_format(options)
          format = ENDIAN_TO_FORMAT[options[:endian]]
          format = FORMAT_TO_SINGLE_PRECISION[format] if options[:precision] == :single
          format
        end

        def read_packed(io, options)
          s = io.read_exactly(PRECISION[options[:precision]])
          s && s.unpack(pack_option_to_format(options)).first
        end
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
packable-1.3.18 lib/packable/extensions/float.rb
packable-1.3.17 lib/packable/extensions/float.rb
packable-1.3.16 lib/packable/extensions/float.rb
packable-1.3.15 lib/packable/extensions/float.rb
packable-1.3.14 lib/packable/extensions/float.rb
packable-1.3.13 lib/packable/extensions/float.rb
packable-1.3.12 lib/packable/extensions/float.rb
packable-1.3.10 lib/packable/extensions/float.rb
packable-1.3.9 lib/packable/extensions/float.rb
packable-1.3.8 lib/packable/extensions/float.rb
packable-1.3.7 lib/packable/extensions/float.rb
packable-1.3.6 lib/packable/extensions/float.rb
packable-1.3.5 lib/packable/extensions/float.rb