Module: Protojson

Defined in:
lib/protojson.rb,
lib/protojson/version.rb,
lib/protojson/codec/hash.rb,
lib/protojson/codec/json.rb,
lib/protojson/codec/binary.rb,
lib/protojson/codec/json_indexed.rb,
lib/protojson/codec/json_tag_map.rb,
lib/protojson/codec/codec_interface.rb

Defined Under Namespace

Modules: Codec

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.[](codec = nil) ⇒ Object

Returns the codec specified or the default one if no attribute is defined



32
33
34
35
36
# File 'lib/protojson.rb', line 32

def [](codec = nil)
  codec.is_a?(Protojson::Codec::CodecInterface) and return codec
  codec.is_a?(Symbol) or codec.nil? and return codecs[codec]
  raise "Invalid codec #{codec}. It should either implement Protojson::Codec::CodecInterface or be a symbol"
end

.[]=(codec_name = nil, codec = nil) ⇒ Object

Key name - Codec value



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/protojson.rb', line 10

def []=(codec_name = nil, codec = nil)
  codec_name.is_a?(String) and codec_name.downcase!

  unless codec_name.nil? # default codec
    unless codec_name.is_a?(Symbol)
      if codec_name.respond_to?(:to_sym)
        codec_name = codec_name.to_sym
      else
        raise Exception, "Only symbols allowed as codec names"
      end
    end
  end

  if codec.nil? # unregister codec
    codecs.delete(codec_name)
  else # register codec
    !codec.is_a? Protojson::Codec::CodecInterface and raise Exception, "codec should include Protojson::Codec::CodecInterface"
    codecs[codec_name] = codec
  end
end

.codecsObject

Initializes the codecs Hash



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/protojson.rb', line 39

def codecs
  @codecs ||= (
      # While accessing the Hash, if the key does not exist, throws an exception
  h = Hash.new { |hash, key|
    hash.has_key?(key) and return hash[key]
    !key.nil? and raise Exception, "Undefined codec #{key}"
    h[nil] = Protojson::Codec::Binary
  }
  # default value is Binary codec
  h[nil] = Protojson::Codec::Binary
  h[:hash] = Protojson::Codec::Hash
  h[:indexed] = Protojson::Codec::JsonIndexed
  h[:json] = Protojson::Codec::Json
  h[:tag_map] = Protojson::Codec::JsonTagMap
  h
  )
end

.decode(message, data, codec = nil) ⇒ Object



77
78
79
80
# File 'lib/protojson.rb', line 77

def decode(message, data, codec = nil)
  codec = send("[]".to_sym, codec) # fetch default codec if none given
  codec.decode(message, data)
end

.encode(message, codec = nil) ⇒ Object



72
73
74
75
# File 'lib/protojson.rb', line 72

def encode(message, codec = nil)
  codec = send("[]".to_sym, codec)
  codec.encode(message)
end

.set_default_codec(codec) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/protojson.rb', line 57

def set_default_codec(codec)
  # fetch codec from Hash if codec name received
  if codec.is_a?(Symbol) or codec.is_a?(String)
    codec = Protojson[codec]
  end

  # set default codec
  puts codec.is_a?(Protojson::Codec::CodecInterface)
  if codec.is_a?(Protojson::Codec::CodecInterface)
    codecs[nil] = codec
  else
    raise Exception, 'Codec must implement Protojson::Codec::CodecInterface'
  end
end