Sha256: de433a4d702a378e805e5c602d47d0cf033d4483d73c50077b3d554837d7e050

Contents?: true

Size: 898 Bytes

Versions: 1

Compression:

Stored size: 898 Bytes

Contents

# frozen_string_literal: false
require 'json' unless defined?(::JSON::JSON_LOADED) && ::JSON::JSON_LOADED

require 'dry/monads'

# Inspired by standard library implementation
# for Time serialization/deserialization see (json/lib/json/add/time.rb)
#
module Dry
  module Monads
    class Maybe
      # Deserializes JSON string by using Dry::Monads::Maybe#lift method
      def self.json_create(serialized)
        lift(serialized.fetch('value'))
      end

      # Returns a hash, that will be turned into a JSON object and represent this
      # object.
      def as_json(*)
        {
          JSON.create_id => self.class.name,
          value: none? ? nil : @value
        }
      end

      # Stores class name (Dry::Monads::Maybe::Some or Dry::Monads::Maybe::None)
      # with the monad value as JSON string
      def to_json(*args)
        as_json.to_json(*args)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-monads-0.4.0 lib/json/add/dry/monads/maybe.rb