Module: ZPNG::ReadableStruct

Defined in:
lib/zpng/readable_struct.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.new(fmt, *args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/zpng/readable_struct.rb', line 4

def self.new fmt, *args
  size = fmt.scan(/([a-z])(\d*)/i).map do |f,len|
    [len.to_i, 1].max *
      case f
      when /[aAC]/ then 1
      when 'v' then 2
      when 'V','l' then 4
      when 'Q' then 8
      else raise "unknown fmt #{f.inspect}"
      end
  end.inject(&:+)

  Struct.new( *args ).tap do |x|
    x.const_set 'FORMAT', fmt
    x.const_set 'SIZE',  size
    x.class_eval do
      include InstanceMethods
    end
    x.extend ClassMethods
  end
end