Sha256: ef67d8aabf8412822bc268e91e8df4dc0986b5c003d2172e96607d8d75fc8dd1

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

module Monolens
  module Core
    class Mapping
      include Lens

      signature(Type::Any, Type::Any, {
        defn: [Type::Object, false],
        values: [Type::Object, false],    # deprecated
        default: [Type::Any, false],
        fallback: [Type::Callback, false],
        key_hash: [Type::Lenses, false],
        on_missing: [Type::Strategy.missing(%w{default fail fallback keep null}), false]
      })

      def call(arg, world = {})
        original_arg = arg
        if key_hash = option(:key_hash, nil)
          arg = key_hash.call(arg, world)
        end

        option(:defn, option(:values, {})).fetch(arg) do
          on_missing(original_arg, world)
        end
      end

    private

      def on_missing(arg, world)
        strategy = option(:on_missing, :fail)
        case strategy.to_sym
        when :default
          option(:default, nil)
        when :fail
          fail!("Unrecognized value `#{arg}`", world)
        when :fallback
          missing_fallback = ->(arg, world) do
            raise Monolens::Error, "Unexpected missing fallback handler"
          end
          option(:fallback, missing_fallback).call(self, arg, world)
        when :keep
          arg
        when :null
          nil
        else
          raise Monolens::Error, "Unexpected missing strategy `#{strategy}`"
        end
      end
      private :on_missing
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
monolens-0.6.4 lib/monolens/stdlib/core/mapping.rb
monolens-0.6.3 lib/monolens/stdlib/core/mapping.rb
monolens-0.6.2 lib/monolens/stdlib/core/mapping.rb