Sha256: c08cd191a6e552484e54d0e5f54fd186389192bafeb917e32b46ae6fd9db1363

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

require 'stringio'

module Packable
  module Extensions #:nodoc:
    module Array #:nodoc:
      def self.included(base)
        base.class_eval do
          alias_method :pack_without_long_form, :pack
          alias_method :pack, :pack_with_long_form
          include Packable
          extend ClassMethods
        end
      end

      def pack_with_long_form(*arg)
        return pack_without_long_form(*arg) if arg.first.is_a? String
        pio = StringIO.new.packed
        write_packed(pio, *arg)
        pio.string
      end

      def write_packed(io, *how)
        return io << self.original_pack(*how) if how.first.is_a? String
        how = [:repeat => :all] if how.empty?
        current = -1
        how.each do |options|
          repeat = options.is_a?(Hash) ? options.delete(:repeat) || 1 : 1
          repeat = length - 1 - current if repeat == :all
          repeat.times do
            io.write(self[current+=1],options)
          end
        end
      end

      module ClassMethods #:nodoc:
        def read_packed(io, *how)
          raise "Can't support builtin format for arrays" if (how.length == 1) && (how.first.is_a? String)
          how.inject [] do |r, options|
            repeat = options.is_a? Hash ? options.delete(:repeat) || 1 : 1
            (0...repeat).inject r do
              r << io.read(options)
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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