{"id":13807078,"url":"https://github.com/anicholson/crystal-version-tools","last_synced_at":"2026-01-19T07:34:21.971Z","repository":{"id":144339895,"uuid":"255236423","full_name":"anicholson/crystal-version-tools","owner":"anicholson","description":"Crystal tools for providing different behaviour at compile-time based on semantic versions","archived":false,"fork":false,"pushed_at":"2020-07-17T11:45:18.000Z","size":22,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-18T23:51:34.223Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Crystal","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anicholson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-04-13T05:08:35.000Z","updated_at":"2024-10-24T13:21:25.000Z","dependencies_parsed_at":"2024-01-27T09:20:11.422Z","dependency_job_id":null,"html_url":"https://github.com/anicholson/crystal-version-tools","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anicholson%2Fcrystal-version-tools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anicholson%2Fcrystal-version-tools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anicholson%2Fcrystal-version-tools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anicholson%2Fcrystal-version-tools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anicholson","download_url":"https://codeload.github.com/anicholson/crystal-version-tools/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254046198,"owners_count":22005551,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T01:01:20.521Z","updated_at":"2026-01-19T07:34:21.964Z","avatar_url":"https://github.com/anicholson.png","language":"Crystal","funding_links":[],"categories":["Misc"],"sub_categories":[],"readme":"# VersionTools\n\n![Current CI status](https://api.travis-ci.org/anicholson/crystal-version-tools.svg?branch=master)\n\nSwitch behaviour at Crystal compile-time based on versions, using macros.\n\nYou might want this if you need to support multiple versions of a dependency (with breaking changes),\nwhile maintaining a consistent API for your consumers/callers.\n\n## Installation\n\n```yaml\ndependencies:\n  version_tools:\n    github: anicholson/crystal-version-tools\n    version: ~\u003e 0.1.0\n```\n\n## Usage\n\nMake sure to include the library in your project.\n\n```crystal\nrequire \"version_tools\"\n```\n\n### Built-in check: Crystal version being used\n\nVersionTools provides a predefined checker that checks the version of Crystal being used\nagainst a version you specify, like this:\n\n```crystal\nwith_crystal_version(\"0.32.0\") do\n  greater do\n    puts \"#{Crystal::VERSION} is later than 0.32.0\"\n  end\n\n  lesser_or_equal do\n    puts \"#{Crystal::VERSION} is earlier or equal to 0.32.0\"\n  end\nend\n```\n\nThis code snippet will compile to the following on each version:\n\n```crystal\n# on 0.32.1 or later\nputs \"#{Crystal::VERSION} is greater than 0.32.0\"\n\n# on 0.32.0 or earlier\nputs \"#{Crystal::VERSION} is earlier or equal to 0.32.0\"\n```\n\n### Defining your own checker\n\nIf you wish to define your own checkers, use `define_version_checker!`.\n\nFor example, to create a checker called `with_api_version` that checks against (say) version \"1.0.0\" of something,\nyou invoke:\n\n```crystal\nVersionTools.define_version_checker!(with_api_version, \"1.0.0\")\n```\n\nAnd now you can use `with_api_version` the same way as `with_crystal_version` above.\n\n### Scoped within modules\n\nDefining a checker within a module scopes the checker to that module. For example:\n\n```crystal\nmodule Foo\n  VersionTools.define_version_checker!(with_bar, \"0.0.1\")\nend\n\n# Works\nFoo.with_bar(\"...\") do\n  #...\nend\n\n# Does not work\nwith_bar(\"...\") do\n  #...\nend\n```\n\n### Supported clauses\n\nThe following clauses are supported.\nIn these tables, `my_version` refers to the version baked into the checker, and\n`compared_version` refers to the version when the checker is _invoked_.\n\n| Clause             | Description                         | Aliases                 |\n|--------------------|-------------------------------------|-------------------------|\n| `lesser`           | If `my_version \u003c compared_version`  | `less_than`             |\n| `lesser_or_equal`  | If `my_version \u003c= compared_version` | `less_than_or_equal`    |\n| `equal`            | If `my_version == compared_version` | `equals`                |\n| `greater_or_equal` | If `my_version \u003e= compared_version` | `greater_than_or_equal` |\n| `greater`          | If `my_version \u003e compared_version`  | `greater_than`          |\n\nAny other clauses will raise a compile-time error:\n\n```crystal\nwith_some_checker(\"1.0.0\") do\n  elephant do\n    raise \"Will not raise\"\n  end\nend\n\n# emits:\n# 4 | VersionTools.define_version_checker!(with_some_checker, \"0.1.0\")\n#     ^\n# Error: Unknown clause: :elephant\n```\n\nAny code not within a clause will also raise a compile-time error:\n\n```crystal\nwith_some_checker(\"1.0.0\") do\n  x = Object.new\nend\n\n# emits:\n# \n#  4 | VersionTools.define_version_checker!(with_some_checker, \"0.1.0\")\n#      ^\n# Error: The following code caused an error:\n# \n#   x = Object.new\n# \n# Use a clause instead, for example:\n#   greater do\n#     # your code here\n#   end\n# \n# The following clauses are supported:\n#   * lesser (or less_than)\n#   * lesser_or_equal (or less_than_or_equal)\n#   * equal (or equals)\n#   * greater_or equal (or greater_than_or_equal)\n#   * greater (or greater_than)\n```\n\n# Contributing\n\n1.  Fork it ( anicholson/crystal-version-tools/fork )\n2.  Create your feature branch (git checkout -b my-new-feature)\n3.  Commit your changes (git commit -am 'Add some feature')\n4.  Push to the branch (git push origin my-new-feature)\n5.  Create a new Pull Request\n\n# Contributors\n\n- [anicholson](https://github.com/anicholson) Andy Nicholson - creator, maintainer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanicholson%2Fcrystal-version-tools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanicholson%2Fcrystal-version-tools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanicholson%2Fcrystal-version-tools/lists"}