Sha256: 142e20579d4d2017fb4bcdecebf20a95cec34b80b3108d25a1e37835d058b98d

Contents?: true

Size: 1016 Bytes

Versions: 10

Compression:

Stored size: 1016 Bytes

Contents

# frozen_string_literal: true

require 'kind/basic'

module Kind
  module Function
    module Behavior
      def self.extended(base)
        base.send(:alias_method, :[], :call)
        base.send(:alias_method, :===, :call)
        base.send(:alias_method, :yield, :call)
      end
    end

    def self.included(_)
      raise RuntimeError, "The Kind::Function can't be included, it can be only extended."
    end

    def self.extended(base)
      base.extend(Kind.of_module(base))
    end

    def kind_function!
      return self if Kind.is?(Behavior, self)

      KIND.respond_to!(:call, self).extend(Behavior)

      if method(:call).parameters.empty?
        raise ArgumentError, "#{self.name}.call must receive at least one argument"
      end

      self.instance_eval(
        'def to_proc; @to_proc ||= method(:call).to_proc; end' \
        "\n" \
        'def curry; @curry ||= to_proc.curry; end'
      )

      self.to_proc
      self.curry
      self
    end

    private_constant :Behavior
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
kind-5.10.0 lib/kind/function.rb
kind-5.9.0 lib/kind/function.rb
kind-5.8.1 lib/kind/function.rb
kind-5.8.0 lib/kind/function.rb
kind-5.7.0 lib/kind/function.rb
kind-5.6.0 lib/kind/function.rb
kind-5.5.0 lib/kind/function.rb
kind-5.4.1 lib/kind/function.rb
kind-5.4.0 lib/kind/function.rb
kind-5.3.0 lib/kind/function.rb