Module: ActiveSupport::MessagePack::Extensions
- Extended by:
- Extensions
- Included in:
- Extensions
- Defined in:
- lib/active_support/message_pack/extensions.rb
Overview
:nodoc:
Constant Summary collapse
- LOAD_WITH_MSGPACK_EXT =
0
- LOAD_WITH_JSON_CREATE =
1
Instance Method Summary collapse
- #dump_class(klass) ⇒ Object
- #dump_time_zone(time_zone) ⇒ Object
- #install(registry) ⇒ Object
- #install_unregistered_type_error(registry) ⇒ Object
- #install_unregistered_type_fallback(registry) ⇒ Object
- #load_class(name) ⇒ Object
- #load_time_zone(name) ⇒ Object
- #raise_invalid_format ⇒ Object
- #raise_unserializable(object) ⇒ Object
- #read_class(unpacker) ⇒ Object
- #read_complex(unpacker) ⇒ Object
- #read_date(unpacker) ⇒ Object
- #read_datetime(unpacker) ⇒ Object
- #read_duration(unpacker) ⇒ Object
- #read_hash_with_indifferent_access(unpacker) ⇒ Object
- #read_object(unpacker) ⇒ Object
- #read_range(unpacker) ⇒ Object
- #read_rational(unpacker) ⇒ Object
- #read_set(unpacker) ⇒ Object
- #read_time(unpacker) ⇒ Object
- #read_time_with_zone(unpacker) ⇒ Object
- #read_time_zone(unpacker) ⇒ Object
- #write_class(klass, packer) ⇒ Object
- #write_complex(complex, packer) ⇒ Object
- #write_date(date, packer) ⇒ Object
- #write_datetime(datetime, packer) ⇒ Object
- #write_duration(duration, packer) ⇒ Object
- #write_hash_with_indifferent_access(hwia, packer) ⇒ Object
- #write_object(object, packer) ⇒ Object
- #write_range(range, packer) ⇒ Object
- #write_rational(rational, packer) ⇒ Object
- #write_set(set, packer) ⇒ Object
- #write_time(time, packer) ⇒ Object
- #write_time_with_zone(twz, packer) ⇒ Object
- #write_time_zone(time_zone, packer) ⇒ Object
Instance Method Details
#dump_class(klass) ⇒ Object
240 241 242 243 |
# File 'lib/active_support/message_pack/extensions.rb', line 240 def dump_class(klass) raise UnserializableObjectError, "Cannot serialize anonymous class" unless klass.name klass.name end |
#dump_time_zone(time_zone) ⇒ Object
178 179 180 |
# File 'lib/active_support/message_pack/extensions.rb', line 178 def dump_time_zone(time_zone) time_zone.name end |
#install(registry) ⇒ Object
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/active_support/message_pack/extensions.rb', line 20 def install(registry) registry.register_type 0, Symbol, packer: :to_msgpack_ext, unpacker: :from_msgpack_ext, optimized_symbols_parsing: true registry.register_type 1, Integer, packer: ::MessagePack::Bigint.method(:to_msgpack_ext), unpacker: ::MessagePack::Bigint.method(:from_msgpack_ext), oversized_integer_extension: true registry.register_type 2, BigDecimal, packer: :_dump, unpacker: :_load registry.register_type 3, Rational, packer: method(:write_rational), unpacker: method(:read_rational), recursive: true registry.register_type 4, Complex, packer: method(:write_complex), unpacker: method(:read_complex), recursive: true registry.register_type 5, DateTime, packer: method(:write_datetime), unpacker: method(:read_datetime), recursive: true registry.register_type 6, Date, packer: method(:write_date), unpacker: method(:read_date), recursive: true registry.register_type 7, Time, packer: method(:write_time), unpacker: method(:read_time), recursive: true registry.register_type 8, ActiveSupport::TimeWithZone, packer: method(:write_time_with_zone), unpacker: method(:read_time_with_zone), recursive: true registry.register_type 9, ActiveSupport::TimeZone, packer: method(:dump_time_zone), unpacker: method(:load_time_zone) registry.register_type 10, ActiveSupport::Duration, packer: method(:write_duration), unpacker: method(:read_duration), recursive: true registry.register_type 11, Range, packer: method(:write_range), unpacker: method(:read_range), recursive: true registry.register_type 12, Set, packer: method(:write_set), unpacker: method(:read_set), recursive: true registry.register_type 13, URI::Generic, packer: :to_s, unpacker: URI.method(:parse) registry.register_type 14, IPAddr, packer: :to_s, unpacker: :new registry.register_type 15, Pathname, packer: :to_s, unpacker: :new registry.register_type 16, Regexp, packer: :to_s, unpacker: :new registry.register_type 17, ActiveSupport::HashWithIndifferentAccess, packer: method(:write_hash_with_indifferent_access), unpacker: method(:read_hash_with_indifferent_access), recursive: true end |
#install_unregistered_type_error(registry) ⇒ Object
106 107 108 109 110 |
# File 'lib/active_support/message_pack/extensions.rb', line 106 def install_unregistered_type_error(registry) registry.register_type 127, Object, packer: method(:raise_unserializable), unpacker: method(:raise_invalid_format) end |
#install_unregistered_type_fallback(registry) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/active_support/message_pack/extensions.rb', line 112 def install_unregistered_type_fallback(registry) registry.register_type 127, Object, packer: method(:write_object), unpacker: method(:read_object), recursive: true end |
#load_class(name) ⇒ Object
245 246 247 248 249 250 251 252 253 |
# File 'lib/active_support/message_pack/extensions.rb', line 245 def load_class(name) Object.const_get(name) rescue NameError => error if error.name.to_s == name raise MissingClassError, "Missing class: #{name}" else raise end end |
#load_time_zone(name) ⇒ Object
182 183 184 |
# File 'lib/active_support/message_pack/extensions.rb', line 182 def load_time_zone(name) ActiveSupport::TimeZone[name] end |
#raise_invalid_format ⇒ Object
236 237 238 |
# File 'lib/active_support/message_pack/extensions.rb', line 236 def raise_invalid_format(*) raise "Invalid format" end |
#raise_unserializable(object) ⇒ Object
232 233 234 |
# File 'lib/active_support/message_pack/extensions.rb', line 232 def raise_unserializable(object, *) raise UnserializableObjectError, "Unsupported type #{object.class} for object #{object.inspect}" end |
#read_class(unpacker) ⇒ Object
259 260 261 |
# File 'lib/active_support/message_pack/extensions.rb', line 259 def read_class(unpacker) load_class(unpacker.read) end |
#read_complex(unpacker) ⇒ Object
134 135 136 |
# File 'lib/active_support/message_pack/extensions.rb', line 134 def read_complex(unpacker) Complex(unpacker.read, unpacker.read) end |
#read_date(unpacker) ⇒ Object
155 156 157 |
# File 'lib/active_support/message_pack/extensions.rb', line 155 def read_date(unpacker) Date.jd(unpacker.read) end |
#read_datetime(unpacker) ⇒ Object
147 148 149 |
# File 'lib/active_support/message_pack/extensions.rb', line 147 def read_datetime(unpacker) DateTime.jd(unpacker.read, unpacker.read, unpacker.read, unpacker.read + read_rational(unpacker), read_rational(unpacker)) end |
#read_duration(unpacker) ⇒ Object
199 200 201 202 203 204 |
# File 'lib/active_support/message_pack/extensions.rb', line 199 def read_duration(unpacker) value = unpacker.read parts = ActiveSupport::Duration::PARTS.zip(unpacker.read).to_h parts.compact! ActiveSupport::Duration.new(value, parts) end |
#read_hash_with_indifferent_access(unpacker) ⇒ Object
228 229 230 |
# File 'lib/active_support/message_pack/extensions.rb', line 228 def read_hash_with_indifferent_access(unpacker) ActiveSupport::HashWithIndifferentAccess.new(unpacker.read) end |
#read_object(unpacker) ⇒ Object
280 281 282 283 284 285 286 287 288 289 |
# File 'lib/active_support/message_pack/extensions.rb', line 280 def read_object(unpacker) case unpacker.read when LOAD_WITH_MSGPACK_EXT read_class(unpacker).from_msgpack_ext(unpacker.read) when LOAD_WITH_JSON_CREATE read_class(unpacker).json_create(unpacker.read) else raise_invalid_format end end |
#read_range(unpacker) ⇒ Object
212 213 214 |
# File 'lib/active_support/message_pack/extensions.rb', line 212 def read_range(unpacker) Range.new(unpacker.read, unpacker.read, unpacker.read) end |
#read_rational(unpacker) ⇒ Object
124 125 126 127 |
# File 'lib/active_support/message_pack/extensions.rb', line 124 def read_rational(unpacker) numerator = unpacker.read Rational(numerator, numerator.zero? ? 1 : unpacker.read) end |
#read_set(unpacker) ⇒ Object
220 221 222 |
# File 'lib/active_support/message_pack/extensions.rb', line 220 def read_set(unpacker) Set.new(unpacker.read) end |
#read_time(unpacker) ⇒ Object
165 166 167 |
# File 'lib/active_support/message_pack/extensions.rb', line 165 def read_time(unpacker) Time.at_without_coercion(unpacker.read, unpacker.read, :nanosecond, in: unpacker.read) end |
#read_time_with_zone(unpacker) ⇒ Object
174 175 176 |
# File 'lib/active_support/message_pack/extensions.rb', line 174 def read_time_with_zone(unpacker) ActiveSupport::TimeWithZone.new(read_time(unpacker), read_time_zone(unpacker)) end |
#read_time_zone(unpacker) ⇒ Object
190 191 192 |
# File 'lib/active_support/message_pack/extensions.rb', line 190 def read_time_zone(unpacker) load_time_zone(unpacker.read) end |
#write_class(klass, packer) ⇒ Object
255 256 257 |
# File 'lib/active_support/message_pack/extensions.rb', line 255 def write_class(klass, packer) packer.write(dump_class(klass)) end |
#write_complex(complex, packer) ⇒ Object
129 130 131 132 |
# File 'lib/active_support/message_pack/extensions.rb', line 129 def write_complex(complex, packer) packer.write(complex.real) packer.write(complex.imaginary) end |
#write_date(date, packer) ⇒ Object
151 152 153 |
# File 'lib/active_support/message_pack/extensions.rb', line 151 def write_date(date, packer) packer.write(date.jd) end |
#write_datetime(datetime, packer) ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/active_support/message_pack/extensions.rb', line 138 def write_datetime(datetime, packer) packer.write(datetime.jd) packer.write(datetime.hour) packer.write(datetime.min) packer.write(datetime.sec) write_rational(datetime.sec_fraction, packer) write_rational(datetime.offset, packer) end |
#write_duration(duration, packer) ⇒ Object
194 195 196 197 |
# File 'lib/active_support/message_pack/extensions.rb', line 194 def write_duration(duration, packer) packer.write(duration.value) packer.write(duration._parts.values_at(*ActiveSupport::Duration::PARTS)) end |
#write_hash_with_indifferent_access(hwia, packer) ⇒ Object
224 225 226 |
# File 'lib/active_support/message_pack/extensions.rb', line 224 def write_hash_with_indifferent_access(hwia, packer) packer.write(hwia.to_h) end |
#write_object(object, packer) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/active_support/message_pack/extensions.rb', line 266 def write_object(object, packer) if object.class.respond_to?(:from_msgpack_ext) packer.write(LOAD_WITH_MSGPACK_EXT) write_class(object.class, packer) packer.write(object.to_msgpack_ext) elsif object.class.respond_to?(:json_create) packer.write(LOAD_WITH_JSON_CREATE) write_class(object.class, packer) packer.write(object.as_json) else raise_unserializable(object) end end |
#write_range(range, packer) ⇒ Object
206 207 208 209 210 |
# File 'lib/active_support/message_pack/extensions.rb', line 206 def write_range(range, packer) packer.write(range.begin) packer.write(range.end) packer.write(range.exclude_end?) end |
#write_rational(rational, packer) ⇒ Object
119 120 121 122 |
# File 'lib/active_support/message_pack/extensions.rb', line 119 def write_rational(rational, packer) packer.write(rational.numerator) packer.write(rational.denominator) unless rational.numerator.zero? end |
#write_set(set, packer) ⇒ Object
216 217 218 |
# File 'lib/active_support/message_pack/extensions.rb', line 216 def write_set(set, packer) packer.write(set.to_a) end |
#write_time(time, packer) ⇒ Object
159 160 161 162 163 |
# File 'lib/active_support/message_pack/extensions.rb', line 159 def write_time(time, packer) packer.write(time.tv_sec) packer.write(time.tv_nsec) packer.write(time.utc_offset) end |
#write_time_with_zone(twz, packer) ⇒ Object
169 170 171 172 |
# File 'lib/active_support/message_pack/extensions.rb', line 169 def write_time_with_zone(twz, packer) write_time(twz.utc, packer) write_time_zone(twz.time_zone, packer) end |
#write_time_zone(time_zone, packer) ⇒ Object
186 187 188 |
# File 'lib/active_support/message_pack/extensions.rb', line 186 def write_time_zone(time_zone, packer) packer.write(dump_time_zone(time_zone)) end |