Class: MessagePack::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/msgpack/factory.rb

Instance Method Summary collapse

Instance Method Details

#dump(v, *rest) ⇒ Object Also known as: pack



74
75
76
77
78
# File 'lib/msgpack/factory.rb', line 74

def dump(v, *rest)
  packer = packer(*rest)
  packer.write(v)
  packer.full_pack
end

#load(src, param = nil) ⇒ Object Also known as: unpack



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/msgpack/factory.rb', line 60

def load(src, param = nil)
  unpacker = nil

  if src.is_a? String
    unpacker = unpacker(param)
    unpacker.feed(src)
  else
    unpacker = unpacker(src, param)
  end

  unpacker.full_unpack
end

#registered_types(selector = :both) ⇒ Object

id, class: Class(or nil), packer: arg, unpacker: arg, …


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/msgpack/factory.rb', line 6

def registered_types(selector=:both)
  packer, unpacker = registered_types_internal
  # packer: Class -> [tid, proc, arg]
  # unpacker: tid -> [klass, proc, arg]

  list = []

  case selector
  when :both
    packer.each_pair do |klass, ary|
      type = ary[0]
      packer_arg = ary[2]
      unpacker_arg = nil
      if unpacker.has_key?(type) && unpacker[type][0] == klass
        unpacker_arg = unpacker.delete(type)[2]
      end
      list << {type: type, class: klass, packer: packer_arg, unpacker: unpacker_arg}
    end

    # unpacker definition only
    unpacker.each_pair do |type, ary|
      list << {type: type, class: ary[0], packer: nil, unpacker: ary[2]}
    end

  when :packer
    packer.each_pair do |klass, ary|
      list << {type: ary[0], class: klass, packer: ary[2]}
    end

  when :unpacker
    unpacker.each_pair do |type, ary|
      list << {type: type, class: ary[0], unpacker: ary[2]}
    end

  else
    raise ArgumentError, "invalid selector #{selector}"
  end

  list.sort{|a, b| a[:type] <=> b[:type] }
end

#type_registered?(klass_or_type, selector = :both) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/msgpack/factory.rb', line 47

def type_registered?(klass_or_type, selector=:both)
  case klass_or_type
  when Class
    klass = klass_or_type
    registered_types(selector).any?{|entry| klass <= entry[:class] }
  when Integer
    type = klass_or_type
    registered_types(selector).any?{|entry| type == entry[:type] }
  else
    raise ArgumentError, "class or type id"
  end
end