Skip to content

v1.3.0

Compare
Choose a tag to compare
@kylekthompson kylekthompson released this 18 Oct 19:00
· 27 commits to main since this release
1d72f84

Interface Coercers

Strict Interface Coercers

You can now easily coerce into interfaces defined with Strict::Interface. MyInterface.coercer will provide a coercer that will attempt to coercer a passed-in implementation into your interface (and strictly validate it along the way, of course).

class Storage
  extend Strict::Interface

  expose(:write) do
    key String
    contents String
    returns Boolean()
  end

  expose(:read) do
    key String
    returns AnyOf(String, nil)
  end
end

module Storages
  class Memory
    def initialize
      @storage = {}
    end

    def write(key:, contents:)
      storage[key] = contents
      true
    end

    def read(key:)
      storage[key]
    end

    private

    attr_reader :storage
  end
end

class Writer
  include Strict::Object

  attributes do
    storage Storage, coerce: Storage.coercer
  end
end

writer = Writer.new(storage: Storages::Memory.new)
# => #<Writer storage=#<Storage implementation=#<Storages::Memory>>>

What's Changed

Full Changelog: v1.2.0...v1.3.0