must

Version, currently 0.1.02 versions

github.com/maiha/must.cr

Library for dynamic typing in Crystal

1 stars
1 dependent
License: MIT

Nothing has been indexed for 0.1.0 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  must:
    github: maiha/must.cr
    version: ~> 0.1.0

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.1.0. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

must.cr Build Status

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 time
  • must 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

  1. Add the dependency to your shard.yml:
dependencies:
  must:
    github: maiha/must
    version: 0.2.0
  1. 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

  1. Fork it (https://github.com/maiha/must.cr/fork)
  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

  • maiha - creator and maintainer