Class: Whoosh::Serialization::Msgpack
- Inherits:
-
Object
- Object
- Whoosh::Serialization::Msgpack
- Defined in:
- lib/whoosh/serialization/msgpack.rb
Class Method Summary collapse
- .available? ⇒ Boolean
- .content_type ⇒ Object
- .decode(raw) ⇒ Object
- .encode(data) ⇒ Object
- .ensure_available! ⇒ Object
- .prepare(obj) ⇒ Object
Class Method Details
.available? ⇒ Boolean
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/whoosh/serialization/msgpack.rb', line 8 def self.available? if @available.nil? @available = begin require "msgpack" true rescue LoadError false end end @available end |
.content_type ⇒ Object
20 21 22 |
# File 'lib/whoosh/serialization/msgpack.rb', line 20 def self.content_type "application/msgpack" end |
.decode(raw) ⇒ Object
29 30 31 32 33 |
# File 'lib/whoosh/serialization/msgpack.rb', line 29 def self.decode(raw) return nil if raw.nil? || raw.empty? ensure_available! MessagePack.unpack(raw) end |
.encode(data) ⇒ Object
24 25 26 27 |
# File 'lib/whoosh/serialization/msgpack.rb', line 24 def self.encode(data) ensure_available! MessagePack.pack(prepare(data)) end |
.ensure_available! ⇒ Object
46 47 48 |
# File 'lib/whoosh/serialization/msgpack.rb', line 46 def self.ensure_available! raise Errors::DependencyError, "MessagePack requires the 'msgpack' gem" unless available? end |
.prepare(obj) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/whoosh/serialization/msgpack.rb', line 35 def self.prepare(obj) case obj when Hash then obj.transform_keys(&:to_s).transform_values { |v| prepare(v) } when Array then obj.map { |v| prepare(v) } when Symbol then obj.to_s when Time, DateTime then obj.iso8601 when BigDecimal then obj.to_s("F") else obj end end |