Module: Qwirk::MarshalStrategy

Defined in:
lib/qwirk/marshal_strategy.rb,
lib/qwirk/marshal_strategy/bson.rb,
lib/qwirk/marshal_strategy/json.rb,
lib/qwirk/marshal_strategy/none.rb,
lib/qwirk/marshal_strategy/ruby.rb,
lib/qwirk/marshal_strategy/yaml.rb,
lib/qwirk/marshal_strategy/string.rb

Overview

Defines some default marshaling strategies for use in marshaling/unmarshaling objects written and read via jms. Implementing classes must define the following methods:

# Return symbol
#  :text  if session.create_text_message should be used to generate the JMS message
#  :bytes if session.create_bytes_message should be used to generate the JMS message
def marshal_type
  # Return either :text or :bytes
  :text
end

# Defines the conversion to wire format by the publisher of the message
def marshal(object)
  # Operate on object and convert to message format
end

# Defines the conversion from wire format by the consumer of the message
def unmarshal(msg)
  # Operate on message to convert it from wire protocol back to object format
end

Defined Under Namespace

Modules: BSON, JSON, None, Ruby, String, YAML

Class Method Summary collapse

Class Method Details

.find(marshaler) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/qwirk/marshal_strategy.rb', line 27

def self.find(marshaler)
  if marshaler.nil?
    return Ruby
  else
    val = @options[marshaler.to_sym]
    return val if val
  end
  raise "Invalid marshal strategy: #{marshaler}"
end

.register(*marshalers) ⇒ Object

Allow user-defined marshal strategies. It can either be passed a hash of sym => marshaler or a list of marshalers that define the to_sym method which will be used as the key.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/qwirk/marshal_strategy.rb', line 43

def self.register(*marshalers)
  if marshalers.size == 1 && marshalers[0].kind_of?(Hash)
    marshalers[0].each do |key, marshaler|
      raise "Invalid marshal strategy: #{marshaler}" unless valid?(marshaler)
      @options[key] = marshaler
    end
  else
    marshalers.each do |marshaler|
      raise "Invalid marshal strategy: #{marshaler}" unless valid?(marshaler) && marshaler.respond_to?(:to_sym)
      @options[marshaler.to_sym] = marshaler
    end
  end
end

.registered?(marshaler) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/qwirk/marshal_strategy.rb', line 37

def self.registered?(marshaler)
  @options.has_key?(marshaler.to_sym)
end

.unregister(sym) ⇒ Object



57
58
59
# File 'lib/qwirk/marshal_strategy.rb', line 57

def self.unregister(sym)
  @options.delete(sym)
end

.valid?(marshaler) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/qwirk/marshal_strategy.rb', line 61

def self.valid?(marshaler)
  return marshaler.respond_to?(:marshal_type) &&
      marshaler.respond_to?(:marshal) &&
      marshaler.respond_to?(:unmarshal)
end