<commit_before>require "reverse_parameters/version"
require "reverse_parameters/core_ext/refinements"

module ReverseParameters

  # @param [Proc, Array] input
  def self.new(*args)
    Base.new(*args)
  end

  class Base

    # @param [Proc, Array] input
    def initialize(input)
      if input.respond_to?(:to_proc)
        @params = input.to_proc.parameters
      elsif input.respond_to?(:to_ary)
        @params = input.to_ary
      elsif UnboundMethod
        @params = input.parameters
      else
        raise ArgumentError.new("Input must be an Array of parameters or a Proc object.")
      end
    end

    # Method parameters are the names listed in the function definition.
    # @return [ReverseParameters::Parameters]
    def parameters
      Parameters.new(params)
    end

    # Method arguments are the real values passed to (and received by) the function.
    # @return [ReverseParameters::Arguments]
    # @param [true, false] blocks_as_values: express block as variable vs a proc passed to the end of a method.
    # def my_method(&block)
    # end
    # ReverseParameters.new(method(:my_method)).arguments(blocks_as_values: true).to_s
    #   #=> "block"
    #
    # # ReverseParameters.new(method(:my_method)).arguments.to_s
    #   #=> "&block"
    def arguments(blocks_as_values: false)
      Arguments.new(params, blocks_as_values: blocks_as_values)
    end

    private
    attr_reader :params
  end

  class BaseCollection
    include Enumerable

    def initialize(collection, **options)
      @collection = collection.map { |state, name| item_class.new(state: state, name: name, **options) }
    end

    def each(&block)
      @collection.send(:each, &block)
    end

    def to_s
      to_a.join(', ')
    end

    def to_a
      map(&:to_s)
    end

    def [](value)
      @collection[value]
    end

    class Item
      def initialize(name:, state:, **options)
        @name  = name
        @state = state
        post_initialize(options)
      end

      def post_initialize(*)
      end

      attr_reader :name, :state
    end
  end

  class Arguments < BaseCollection

    class Arg < BaseCollection::Item
      def to_s
        case state
        when :key, :keyreq
          "#{name}: #{name}"
        when :keyrest
          "**#{name}"
        when :rest
          "*#{name}"
        when :block
          block(name)
        else
          name
        end.to_s
      end

      def block_as_value
        case state
        when :key, :keyreq
          "#{name}: #{name}"
        else
          name
        end.to_s
      end

      private

      def post_initialize(blocks_as_values:)
        @blocks_as_values = blocks_as_values
      end

      def block(name)
        if @blocks_as_values
          name
        else
          "&#{name}"
        end
      end
    end


    def item_class
      Arguments::Arg
    end

    private

    def block(name, pre="&")
      [pre, name].compact!.join("")
    end
  end
  class Parameters < BaseCollection;
    class Param < BaseCollection::Item
      def to_s
        case state
        when :req
          name
        when :rest
          "*#{name}"
        when :keyrest
          "**#{name}"
        when :opt
          "#{name}=nil"
        when :keyreq
          "#{name}:"
        when :key
          "#{name}: nil"
        when :block
          "&#{name}"
        end.to_s
      end
    end

    def item_class
      Parameters::Param
    end
  end
end
<commit_msg>Adjust type checking<commit_after>require "reverse_parameters/version"
require "reverse_parameters/core_ext/refinements"

module ReverseParameters

  # @param [Proc, Array] input
  def self.new(*args)
    Base.new(*args)
  end

  class Base

    # @param [Proc, Array] input
    def initialize(input)
      if input.respond_to?(:to_proc)
        @params = input.to_proc.parameters
      elsif input.respond_to?(:to_ary)
        @params = input.to_ary
      elsif input.is_a? UnboundMethod
        @params = input.parameters
      else
        raise ArgumentError.new("Input must be an Array of parameters or a Proc object.")
      end
    end

    # Method parameters are the names listed in the function definition.
    # @return [ReverseParameters::Parameters]
    def parameters
      Parameters.new(params)
    end

    # Method arguments are the real values passed to (and received by) the function.
    # @return [ReverseParameters::Arguments]
    # @param [true, false] blocks_as_values: express block as variable vs a proc passed to the end of a method.
    # def my_method(&block)
    # end
    # ReverseParameters.new(method(:my_method)).arguments(blocks_as_values: true).to_s
    #   #=> "block"
    #
    # # ReverseParameters.new(method(:my_method)).arguments.to_s
    #   #=> "&block"
    def arguments(blocks_as_values: false)
      Arguments.new(params, blocks_as_values: blocks_as_values)
    end

    private
    attr_reader :params
  end

  class BaseCollection
    include Enumerable

    def initialize(collection, **options)
      @collection = collection.map { |state, name| item_class.new(state: state, name: name, **options) }
    end

    def each(&block)
      @collection.send(:each, &block)
    end

    def to_s
      to_a.join(', ')
    end

    def to_a
      map(&:to_s)
    end

    def [](value)
      @collection[value]
    end

    class Item
      def initialize(name:, state:, **options)
        @name  = name
        @state = state
        post_initialize(options)
      end

      def post_initialize(*)
      end

      attr_reader :name, :state
    end
  end

  class Arguments < BaseCollection

    class Arg < BaseCollection::Item
      def to_s
        case state
        when :key, :keyreq
          "#{name}: #{name}"
        when :keyrest
          "**#{name}"
        when :rest
          "*#{name}"
        when :block
          block(name)
        else
          name
        end.to_s
      end

      def block_as_value
        case state
        when :key, :keyreq
          "#{name}: #{name}"
        else
          name
        end.to_s
      end

      private

      def post_initialize(blocks_as_values:)
        @blocks_as_values = blocks_as_values
      end

      def block(name)
        if @blocks_as_values
          name
        else
          "&#{name}"
        end
      end
    end


    def item_class
      Arguments::Arg
    end

    private

    def block(name, pre="&")
      [pre, name].compact!.join("")
    end
  end
  class Parameters < BaseCollection;
    class Param < BaseCollection::Item
      def to_s
        case state
        when :req
          name
        when :rest
          "*#{name}"
        when :keyrest
          "**#{name}"
        when :opt
          "#{name}=nil"
        when :keyreq
          "#{name}:"
        when :key
          "#{name}: nil"
        when :block
          "&#{name}"
        end.to_s
      end
    end

    def item_class
      Parameters::Param
    end
  end
end

{'fraction_non_alphanumeric': 0.09696446554452597, 'fraction_numerical': 0.0, 'mean_word_length': 1.947011445527766, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2234212', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2074, 'n_words': 565}