Sha256: e167973e71368a53ca6ac3c997ccc3b37c1f01dab3d17391a92b44802899926a

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require "clowne/ext/lambda_as_proc"

module Clowne
  module Utils
    class Params # :nodoc: all
      class BaseProxy
        attr_reader :value

        def initialize(value)
          @value = value
        end

        def permit(_params)
          raise NotImplementedError
        end
      end

      class PassProxy < BaseProxy
        def permit(params:, **)
          params
        end
      end

      class NullProxy < BaseProxy
        def permit(_params)
          {}
        end
      end

      class BlockProxy < BaseProxy
        using Clowne::Ext::LambdaAsProc

        def permit(params:, parent:)
          value.to_proc.call(params, parent)
        end
      end

      class KeyProxy < BaseProxy
        def permit(params:, **)
          nested_params = params.fetch(value)
          return nested_params if nested_params.is_a?(Hash)

          raise KeyError, "value by key '#{value}' must be a Hash"
        end
      end

      class << self
        def proxy(value)
          if value == true
            PassProxy
          elsif value.nil? || value == false
            NullProxy
          elsif value.is_a?(Proc)
            BlockProxy
          else
            KeyProxy
          end.new(value)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
clowne-1.5.0 lib/clowne/utils/params.rb
clowne-1.4.0 lib/clowne/utils/params.rb
clowne-1.3.0 lib/clowne/utils/params.rb
clowne-1.2.0 lib/clowne/utils/params.rb