Skip to content

Commit

Permalink
Merge pull request #20 from kylekthompson/kt-strict-interface-readme
Browse files Browse the repository at this point in the history
Add Strict::Interface examples
  • Loading branch information
kylekthompson authored Oct 14, 2022
2 parents fa8e74c + 8c89c1b commit f86607f
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,71 @@ UpdateEmail.new.call(user_id: "123", email: "456")
# => Strict::MethodReturnError
```

### `Strict::Interface`

```rb
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

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

storage.write(key: "some/path/to/file.rb", contents: "Hello")
# => true

storage.write(key: "some/path/to/file.rb", contents: {})
# => Strict::MethodCallError

storage.read(key: "some/path/to/file.rb")
# => "Hello"

storage.read(key: "some/path/to/other.rb")
# => nil

module Storages
class Wat
def write(key:)
end
end
end

storage = Storage.new(Storages::Wat.new)
# => Strict::ImplementationDoesNotConformError
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down

0 comments on commit f86607f

Please sign in to comment.