Sha256: 30f5a6faf5bda5040de776fc857ce34a9e5aefe27138dfc8c137ae8d3da17c73

Contents?: true

Size: 765 Bytes

Versions: 1

Compression:

Stored size: 765 Bytes

Contents

# frozen_string_literal: true

require 'plumb/steppable'

module Plumb
  class InterfaceClass
    include Steppable

    attr_reader :method_names

    def initialize(method_names = [])
      @method_names = method_names
      freeze
    end

    def of(*args)
      case args
      in Array => symbols if symbols.all? { |s| s.is_a?(::Symbol) }
      self.class.new(symbols)
    else
      raise ::ArgumentError, "unexpected value to Types::Interface#of #{args.inspect}"
    end
    end

    alias [] of

    def call(result)
      obj = result.value
      missing_methods = @method_names.reject { |m| obj.respond_to?(m) }
      return result.invalid(errors: "missing methods: #{missing_methods.join(', ')}") if missing_methods.any?

      result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
plumb-0.0.1 lib/plumb/interface_class.rb