v1.3.0
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
- Expose an interface coercer by @kylekthompson in #26
Full Changelog: v1.2.0...v1.3.0