include_enum.cr
include_enum
A tiny Crystal shard that lets you use an enum's values as local constants without the EnumType:: prefix. An optional prefix string can be added to each constant name to avoid collisions.
Installation
Add the dependency to your shard.yml:
dependencies:
include_enum:
github: plambert/include_enum.cr
Then run shards install.
Usage
require "include_enum"
enum Color
Red
Green
Blue
end
Include the module, then call the macro
class Palette
include IncludeEnum
include_enum Color
# Red, Green, and Blue are now available as constants:
def default
Green
end
end
Call the macro directly (no include needed)
class Palette
IncludeEnum.include_enum Color
def default
Green
end
end
Add a prefix to avoid name collisions
class Palette
IncludeEnum.include_enum Color, "C_"
# Constants are now C_Red, C_Green, C_Blue:
def default
C_Green
end
end
Include only specific values
class Palette
include IncludeEnum
include_enum Color, only: {Red, Blue}
# Only Red and Blue are available; Green is not.
end
Exclude specific values
class Palette
include IncludeEnum
include_enum Color, except: {Green}
# Red and Blue are available; Green is not.
end
only and except are mutually exclusive — using both raises a compile-time error.
What gets generated
The macro expands to simple constant assignments:
Red = Color::Red
Green = Color::Green
Blue = Color::Blue
Or with a prefix of "C_":
C_Red = Color::Red
C_Green = Color::Green
C_Blue = Color::Blue
Contributing
- Fork it (https://github.com/plambert/include_enum.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
- Paul M. Lambert - creator and maintainer
Repository
include_enum.cr
Owner
Statistic
- 0
- 0
- 0
- 0
- 0
- 15 days ago
- March 19, 2026
License
MIT License
Links
Synced at
Thu, 19 Mar 2026 23:11:28 GMT
Languages