Sha256: d8db41c26d2cb810ba0defccd1108134d9cacbf3d579cd03da82920a5cd0bb87

Contents?: true

Size: 938 Bytes

Versions: 5

Compression:

Stored size: 938 Bytes

Contents

# frozen_string_literal: true

require 'plumb/composable'

module Plumb
  class InterfaceClass
    include Composable

    attr_reader :method_names

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

    def ==(other)
      other.is_a?(self.class) && other.method_names == method_names
    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

    private def _inspect = "Interface[#{method_names.join(', ')}]"
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
plumb-0.0.8 lib/plumb/interface_class.rb
plumb-0.0.7 lib/plumb/interface_class.rb
plumb-0.0.6 lib/plumb/interface_class.rb
plumb-0.0.5 lib/plumb/interface_class.rb
plumb-0.0.4 lib/plumb/interface_class.rb