Class: Qpid::Proton::Codec::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/codec/mapping.rb

Overview

Maps between Proton types and their Ruby native language counterparts.

Constant Summary collapse

@@by_code =
{}
@@by_class =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, name, klasses = nil, getter = nil) ⇒ Mapping

Creates a new mapping.

Arguments

  • code - the AMQP code for this type

  • name - the AMQP name for this type

  • klasses - native Ruby classes that are mapped to this AMQP type

  • getter - overrides the get method for the type



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/codec/mapping.rb', line 42

def initialize(code, name, klasses = nil, getter = nil)

  @code = code
  @name = name

  @@by_code[code] = self

  unless klasses.nil?
    klasses.each do |klass|
      raise "entry exists for #{klass}" if @@by_class.keys.include? klass
      @@by_class[klass] = self unless klass.nil?
    end
  end

  @put_method = (name + "=").intern

  if getter.nil?
    @get_method = name.intern
  else
    @get_method = getter.intern
  end
end

Instance Attribute Details

#codeObject (readonly)



27
28
29
# File 'lib/codec/mapping.rb', line 27

def code
  @code
end

#get_methodObject (readonly)



29
30
31
# File 'lib/codec/mapping.rb', line 29

def get_method
  @get_method
end

#put_methodObject (readonly)



28
29
30
# File 'lib/codec/mapping.rb', line 28

def put_method
  @put_method
end

Class Method Details

.[](x) ⇒ Object

Convert x to a Mapping



87
88
89
90
91
92
93
# File 'lib/codec/mapping.rb', line 87

def self.[](x)
  case x
  when Mapping then x
  when Integer then @@by_code[x]
  when Types::Type then @@by_code[x.code]
  end
end

.for_class(klass) ⇒ Object

Raises:

  • (TypeError)


75
76
77
78
79
80
# File 'lib/codec/mapping.rb', line 75

def self.for_class(klass)
  c = klass
  c = c.superclass while c && (x = @@by_class[c]).nil?
  raise TypeError, "#{klass} cannot be converted to AMQP" unless x
  x
end

.for_code(code) ⇒ Object



82
83
84
# File 'lib/codec/mapping.rb', line 82

def self.for_code(code)
  @@by_code[code]
end

Instance Method Details

#get(data) ⇒ Object



71
72
73
# File 'lib/codec/mapping.rb', line 71

def get(data)
  data.__send__(@get_method)
end

#put(data, value) ⇒ Object



67
68
69
# File 'lib/codec/mapping.rb', line 67

def put(data, value)
  data.__send__(@put_method, value)
end

#to_sObject



65
# File 'lib/codec/mapping.rb', line 65

def to_s; @name; end