https://github.com/wizzardx/bracket
A Crystal shard implementing the bracket pattern for safe resource management. Ensures proper initialization and cleanup of resources, similar to Python's context managers or Haskell's bracket pattern.
https://github.com/wizzardx/bracket
bracket-pattern context-manager crystal crystal-shard raii resource-management safe-resource-handling
Last synced: about 1 year ago
JSON representation
A Crystal shard implementing the bracket pattern for safe resource management. Ensures proper initialization and cleanup of resources, similar to Python's context managers or Haskell's bracket pattern.
- Host: GitHub
- URL: https://github.com/wizzardx/bracket
- Owner: wizzardx
- License: mit
- Created: 2025-02-22T07:53:31.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-22T08:04:38.000Z (about 1 year ago)
- Last Synced: 2025-02-22T08:30:56.518Z (about 1 year ago)
- Topics: bracket-pattern, context-manager, crystal, crystal-shard, raii, resource-management, safe-resource-handling
- Language: Crystal
- Homepage: https://wizzardx.github.io/bracket/
- Size: 0 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bracket
A Crystal shard that implements the bracket pattern for safe resource management. This pattern ensures that resources are properly initialized and cleaned up, similar to Python's context managers or Haskell's bracket pattern.
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
bracket:
github: wizzardx/bracket
```
2. Run `shards install`
## Usage
```crystal
require "bracket"
# Simple example with a string resource
setup = -> { "my resource" }
teardown = ->(resource : String) { puts "Cleaning up #{resource}"; nil }
Bracket.with_resource(setup, teardown) do |resource|
puts "Using #{resource}"
end
# Example with server resource
server_setup = -> {
port = find_available_port
server = start_server(port)
{server, port}
}
server_teardown = ->(resource : Tuple(Server, Int32)) {
server, port = resource
server.stop
nil
}
Bracket.with_resource(server_setup, server_teardown) do |resource|
server, port = resource
# Use server...
end
```
## Features
- Guarantees resource cleanup even if an exception occurs
- Type-safe resource handling
- Simple, functional interface
- Works with any resource type
## Development
Run tests:
```crystal
crystal spec
```
## Contributing
1. Fork it ()
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
## Contributors
- [David Purdy](https://github.com/wizzardx) - creator and maintainer