Sha256: beba9b8e41610b8635899c3a1f22ff10a6ac0c6a6fb09e64be2640a3be03fdd7

Contents?: true

Size: 1.31 KB

Versions: 5

Compression:

Stored size: 1.31 KB

Contents

module Monolens
  module Array
    class Map
      include Lens

      signature(Type::Array, Type::Array, {
        on_error: [Type::Strategy.error(%w{handler keep fail null skip}), false],
        lenses: [Type::Lenses, true]
      })

      def call(arg, world = {})
        is_array!(arg, world)

        lenses = option(:lenses)
        result = []
        arg.each_with_index do |member, i|
          deeper(world, i) do |w|
            begin
              result << lenses.call(member, w)
            rescue Monolens::LensError => ex
              strategy = option(:on_error, :fail)
              handle_error(strategy, member, ex, result, world)
            end
          end
        end
        result
      end

      def handle_error(strategy, member, ex, result, world)
        strategy = strategy.to_sym unless strategy.is_a?(::Array)
        case strategy
        when ::Array
          strategy.each{|s| handle_error(s, member, ex, result, world) }
        when :handler
          error_handler!(world).call(ex)
        when :keep
          result << member
        when :fail
          raise
        when :null
          result << nil
        when :skip
          nil
        else
          raise Monolens::Error, "Unexpected error strategy `#{strategy}`"
        end
      end
      private :handle_error
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
monolens-0.6.4 lib/monolens/stdlib/array/map.rb
monolens-0.6.3 lib/monolens/stdlib/array/map.rb
monolens-0.6.2 lib/monolens/stdlib/array/map.rb
monolens-0.6.1 lib/monolens/stdlib/array/map.rb
monolens-0.6.0 lib/monolens/stdlib/array/map.rb