Module: SnakyHash::Serializer
- Defined in:
- lib/snaky_hash/serializer.rb
Overview
Provides JSON serialization and deserialization capabilities with extensible value transformation
Defined Under Namespace
Modules: BackportedInstanceMethods, ConvenienceInstanceMethods, Modulizer
Class Method Summary collapse
-
.extended(base) ⇒ void
Extends the base class with serialization capabilities.
Instance Method Summary collapse
-
#dump(obj) ⇒ String
Serializes a hash object to JSON.
-
#load(raw_hash) ⇒ Hash
Deserializes a JSON string into a hash object.
Class Method Details
.extended(base) ⇒ void
This method returns an undefined value.
Extends the base class with serialization capabilities
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/snaky_hash/serializer.rb', line 21 def extended(base) extended_module = Modulizer.to_extended_mod base.extend(extended_module) base.include(ConvenienceInstanceMethods) # :nocov: # This will be run in CI on Ruby 2.3, but we only collect coverage from current Ruby unless base.instance_methods.include?(:transform_values) base.include(BackportedInstanceMethods) end # :nocov: end |
Instance Method Details
#dump(obj) ⇒ String
Serializes a hash object to JSON
38 39 40 41 |
# File 'lib/snaky_hash/serializer.rb', line 38 def dump(obj) hash = dump_hash(obj) hash.to_json end |
#load(raw_hash) ⇒ Hash
Deserializes a JSON string into a hash object
47 48 49 50 |
# File 'lib/snaky_hash/serializer.rb', line 47 def load(raw_hash) hash = JSON.parse(presence(raw_hash) || "{}") load_hash(new(hash)) end |