Module: Packable::Extensions::Array

Defined in:
lib/packable/extensions/array.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/packable/extensions/array.rb', line 6

def self.included(base)
  base.class_eval do
    alias_method_chain :pack, :long_form
    include Packable
    extend ClassMethods
  end
end

Instance Method Details

#pack_with_long_form(*arg) ⇒ Object



14
15
16
17
18
19
# File 'lib/packable/extensions/array.rb', line 14

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

#write_packed(io, *how) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/packable/extensions/array.rb', line 21

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