Class: Mutant::Transform::JSON Private
- Inherits:
-
Object
- Object
- Mutant::Transform::JSON
- Defined in:
- lib/mutant/transform/json.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Bidirectional JSON transform
Wraps a pair of dump/load transforms and optionally adds JSON string serialization via .build
Class Method Summary collapse
-
.build(dump:, load:) ⇒ JSON
private
Build a JSON transform that wraps raw transforms with JSON.parse/generate.
-
.for_anima(klass) ⇒ JSON
private
Build a JSON transform for simple Anima objects with JSON-primitive fields.
Instance Method Summary collapse
-
#dump(object) ⇒ Either<Error, Object>
private
Dump object to hash or JSON string.
-
#load(input) ⇒ Either<Error, Object>
private
Load object from hash or JSON string.
Class Method Details
.build(dump:, load:) ⇒ JSON
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Build a JSON transform that wraps raw transforms with JSON.parse/generate
rubocop:disable Metrics/MethodLength
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mutant/transform/json.rb', line 19 def self.build(dump:, load:) new( dump_transform: Sequence.new( steps: [ dump, Success.new(block: ::JSON.public_method(:generate)) ] ), load_transform: Sequence.new( steps: [ Exception.new(error_class: ::JSON::ParserError, block: ::JSON.public_method(:parse)), load ] ) ) end |
.for_anima(klass) ⇒ JSON
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Build a JSON transform for simple Anima objects with JSON-primitive fields
42 43 44 45 46 47 |
# File 'lib/mutant/transform/json.rb', line 42 def self.for_anima(klass) new( dump_transform: Success.new(block: ->(object) { object.to_h.transform_keys(&:to_s) }), load_transform: Success.new(block: ->(hash) { klass.new(**hash.transform_keys(&:to_sym)) }) ) end |
Instance Method Details
#dump(object) ⇒ Either<Error, Object>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Dump object to hash or JSON string
54 55 56 |
# File 'lib/mutant/transform/json.rb', line 54 def dump(object) dump_transform.call(object) end |
#load(input) ⇒ Either<Error, Object>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Load object from hash or JSON string
63 64 65 |
# File 'lib/mutant/transform/json.rb', line 63 def load(input) load_transform.call(input) end |