must.cr
must.cr
Library for dynamic typing in Crystal.
Core class pollution
Object#must
Features
- Checking Types : Dynamically guarantee object types
- Casting Types : Dynamically cast object types
- Matching Features : Object matching and data extraction
# checking
1.must(Int32) # => 1
1.must(Bool) # raises Must::TypeError
# casting
1.must.cast(String) # => "1"
1.must.cast(Bool) # raises Must::CastError
# matching
708.must.match(/0/) # => 708
708.must.match(/0(.)/, "$1") # => "8"
- crystal: 0.31.1
Why not use as
for casting?
as
checks types at compile timemust
checks type at runtime
json = JSON.parse(%|{"a": 1}|)
json["a"].as_f # Unhandled exception: cast from Int64 to Float64 failed (runtime)
json["a"].raw.to_f # undefined method 'to_f' for Array(JSON::Any) (compile time)
json["a"].raw.must.cast(Float64) # => 1.0
Installation
- Add the dependency to your
shard.yml
:
dependencies:
must:
github: maiha/must
version: 0.2.0
- Run
shards install
Usage
require "must"
API
any?, any
Checks empty?
or not. It returns true
if the class doesn't have empty?
method.
1.must.any? # => true
1.must.any # => 1
Set(Int32).new.any? # => false
"".must.any? # => false
"".must.any # raises Must::Error("(value) is empty")
"".must.any(name: "foo") # raises Must::Error("foo is empty")
Development
Contributing
- Fork it (https://github.com/maiha/must.cr/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- maiha - creator and maintainer
Repository
must.cr
Owner
Statistic
- 1
- 0
- 0
- 2
- 0
- over 4 years ago
- January 12, 2020
License
MIT License
Links
Synced at
Tue, 07 May 2024 21:11:03 GMT
Languages