Sha256: 8a0f4f78f09a5b54c2c786cb8f3c6e85e306f38b2f01863a012298f980d1f24b

Contents?: true

Size: 941 Bytes

Versions: 3

Compression:

Stored size: 941 Bytes

Contents

require 'stringio'

module Packable
  module Extensions #:nodoc:
    module String #:nodoc:

      def self.included(base)
        base.class_eval do
          include Packable
          extend ClassMethods
          alias_method :unpack_without_long_form, :unpack
          alias_method :unpack, :unpack_with_long_form
          packers.set :merge_all, :fill => " "
        end
      end

      def write_packed(io, options)
        return io.write_without_packing(self) unless options[:bytes]
        io.write_without_packing(self[0...options[:bytes]].ljust(options[:bytes], options[:fill] || "\000"))
      end

      def unpack_with_long_form(*arg)
        return unpack_without_long_form(*arg) if arg.first.is_a? String
        StringIO.new(self).packed.read(*arg)
      rescue EOFError
        nil
      end

      module ClassMethods #:nodoc:
        def unpack_string(s, options)
          s
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
packable-1.3.18 lib/packable/extensions/string.rb
packable-1.3.17 lib/packable/extensions/string.rb
packable-1.3.16 lib/packable/extensions/string.rb