module Kind::Functional::ClassMethods

Public Class Methods

inherited(_) click to toggle source
# File lib/kind/functional.rb, line 73
def self.inherited(_)
  raise RuntimeError, "#{self.name} is a Kind::Functional and it can't be inherited by anyone"
end

Public Instance Methods

kind_functional!() click to toggle source
# File lib/kind/functional.rb, line 54
def kind_functional!
  return self if Kind.is?(Behavior, self)

  public_methods = self.public_instance_methods - ::Object.new.methods

  unless public_methods.include?(:call)
    raise Kind::Error.new("expected #{self} to implement `#call`")
  end

  if public_methods.size > 1
    raise Kind::Error.new("#{self} can only have `#call` as its public method")
  end

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

  self.send(:include, Behavior)

  def self.inherited(_)
    raise RuntimeError, "#{self.name} is a Kind::Functional and it can't be inherited by anyone"
  end

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

  __dependencies__.freeze

  self
end