• Stars
    star
    17
  • Rank 1,217,161 (Top 25 %)
  • Language
    Crystal
  • License
    Apache License 2.0
  • Created almost 3 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This shard provides facilities for checking whether a constant exists at compile time, and for a variety of different conditional compilation options. Code can be conditionally compiled based on the existence of a constant, version number constraints, or whether an environment variable is set truthy or not.

Defined.cr CI Defined.cr CI

GitHub release GitHub commits since latest release (by SemVer)

defined.cr

This shard provides a variety of macros that can be used to check, at compile time, if a given constant is defined, and also to conditionally compile code based on whether a constant is or is not defined, whether a VERSION meets a requirement, or whether an environment variable is or is not defined.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      defined:
        github: wyhaines/defined.cr
  2. Run shards install

Usage

require "defined"

Constant Check Macro Notes

The constant check that of of the constant validation macros (defined?, if_defined?, unless_defined?) use first checks for the constant relative to the type that the macro is running in, and then if it doesn't find it there, it checks starting at the top level, with one exception. If the class name starts with a :: then the check only operates from the top level. The class name can be provided as a String, a Symbol, or as a bare Class reference.

i.e.

if_defined?("Int32") do
  puts "This will always work"
end

unless_defined?(ClassName::That::Does::Not::Exist) do
  puts "This works, too. Cool."
end

defined?

if defined?(OptionalFeature)
  # The OptionalFeature library has been included into the code, so use it.
else
  # It's not there.
end

stuff = defined?(My::Stuff::Library) || Array(String)

if_defined?

require "defined"

class One; end

if_defined?(One) do
  class Two
    def call
      "I am class Two, but I can only exist of class one is defined."
    end
  end
end

puts Two.new.call

unless_defined?

unless_defined?(PerfectShard) do
  require "project/imperfect_shard"
end

Version Checks

The version check macros leverage the same constant lookup code as the constant check macros, but they automatically search for a VERSION or a Version constant under that namespace, unless the provided constant resolves to a String.

if_version? and unless_version?

class VerMe
  VERSION = "1.2.3"

  class Inner
    Version = "0.1.0"
  end

  class SecondInner
    ClassVersion = "0.3.7"
  end
end

if_version?(VerMe, :>=, "1.2.0") do
  puts "This will only run if the version of VerMe is >= 1.2.0"
end

if_version?(VerMe::Inner, :>=, "0.1.0") do
  puts "This will only run if the version of VerMe::Inner is >= 0.1.0"
end

unless_version?(VerMe::SecondInner::ClassVersion, :>=, "0.4.0") do
  puts "This also works. Hey, maybe you need a new version of that class?"
end

class VerMe
  if_version?(Inner, :<, "0.2.0") do
    puts "WARNING -- Using version of Inner less than 0.2.0 is deprecated, and support will be removed"
  end
end

class MacrosAreTough
  VERSION = {{ `shards version "#{__DIR__}"`.chomp.stringify }}
end

if_version?(MacrosAreTough, :>=, "0.1.0") do
  puts "This will only run if the version of MacrosAreTough is >= 0.1.0"
end

The last example, with MacrosAreTough, deserves additional explanation, because inline macro expressions like that pose a particular challenge for the library, and there are some edge cases.

Environment Variable Checks

This facility provides four very simple macros (if_enabled?, unless_enabled?, if_disabled?, unless_disabled?) which can be used to conditionally compile code based on whether an environment variable is set, or not. The macros use a shell based definition of truthy and falsey, so 0 and false and "" are considered false, while all other values are true.

if_enabled? instantiates the code in the provided block if the environment variable exists, and is set to a shell truthy value.

unless_enabled? instantiates the code in the provided block if the environment variable does not exist, or is set to a shell falsey value.

if_disabled? instantiates the code in the provided block if the environment variable des not exist, or is set to a shell falsey value.

unless_disabled? instantiates the code in the provided block if the environment variable exists, and is set to a shell truthy value.

if_enabled?("ENV_VAR") do
  puts "This will only run if the environment variable ENV_VAR is set to a truthy value"
end

unless_enabled?("ENV_VAR") do
  puts "This will only run if the environment variable ENV_VAR is not set to a truthy value"
end

if_disabled?("ENV_VAR") do
  puts "This will only run if the environment variable ENV_VAR is not set to a truthy value"
end

unless_disabled?("ENV_VAR") do
  puts "This will only run if the environment variable ENV_VAR is set to a truthy value"
end

More Documentation

https://wyhaines.github.io/defined.cr/toplevel.html

Contributing

  1. Fork it (https://github.com/wyhaines/defined.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

GitHub code size in bytes GitHub issues

More Repositories

1

swiftiply

A high performance clustering proxy / web server for web applications.
Ruby
68
star
2

scrawls

A Simple Ruby Web Server
Ruby
37
star
3

analogger

A very fast distributed asynchronous logging service
Ruby
25
star
4

iowa

A fast, productive, component based web development framework for Ruby.
Ruby
21
star
5

opentelemetry-instrumentation.cr

Bundled integrations for opentelemetry-api.cr (https://github.com/wyhaines/opentelemetry-api.cr).
Crystal
16
star
6

csuuid.cr

This is a small UUID library that implements a chronologically sortable UUID.
Crystal
15
star
7

splay_tree_map.cr

This is a Crystal implementation of a Splay Tree; which is a type of binary search tree that is semi-balanced and that tends to self-optimize so that the most accessed items are the fastest to retrieve.
Crystal
14
star
8

datapack.cr

You are building an application, be it web or something else. It has data -- images, CSS, JS, CSV files, whatever -- and you want to be able to easily bundle those into your compiled executable. This shard gives you a simple utility to add this data to your executable, access it, and manage it.
Crystal
13
star
9

opentelemetry-api.cr

The core of open telemetry instrumentation is the OpenTelemetry API/SDK. The initial aim of this shard is to implement the OpenTelemetry specification for metrics, traces, and logs.
Crystal
12
star
10

Send.cr

This shard implements a usable dynamic dispatch function for Crystal code, modeled on the Ruby #send function of the same name.
Crystal
12
star
11

tracer.cr

This library implements a method call tracing facility that can be used to generate either summary or detailed logs of method calls, or to inject arbitrary code execution into method calls.
Crystal
10
star
12

rubyconfmini_2022_crystal_for_rubyists_workshop

HTML
8
star
13

base58.cr

This is a performance optimized implementation of base58 binary-to-text encoding algorithm.
Crystal
8
star
14

opentelemetry-sdk.cr

This repository contains an implementation of an OpenTelemetry SDK for Crystal
Crystal
7
star
15

git-index.cr

This tool makes a local database of git repositories, indexed by initial commit hashes.
Crystal
5
star
16

bus.cr

It is sometimes useful to have a pubsub type message bus inside your software. This library implements a bus to send messages to interested subscribers. Those subscribers can reply to those messages, and are guaranteed that the reply will be routed back to the sender.
Crystal
5
star
17

crypt-isaac

A Ruby implementation of the cryptographically secure ISAAC pseudorandom number generator, with both pure Ruby and C extension implementations.
Ruby
5
star
18

nbchannel.cr

A NBChannel is a non-blocking channel. Normal Crystal channels block on send and on receive. The NBChannel will never block on send, and there are both blocking and nonblocking receive calls available. The channel should work both for single producer, single consumer scenarios, and for scenarios with multiple producers or multiple consumers.
Crystal
5
star
19

ParseDate.cr

This is a small library that attempts to parse a wide variety of date formats with a single call.
Crystal
4
star
20

Chord

A Ruby implementation of the Chord protocol.
Ruby
4
star
21

ruby-gnome2

A mirror of the ruby-gnome2 project.
4
star
22

timeout.cr

This implements a very simple timeout method for Crystal. Use it to wrap code in a timeout, so that execution of that code can not continue forever.
Crystal
4
star
23

find.cr

Crystal
3
star
24

Floodgate

A fast, easy to use, customizeable caching reverse proxy implemented substantially in Ruby
3
star
25

for.cr

This tiny little shard implements a "for" loop for Crystal by leveraging macros.
Crystal
3
star
26

wisteria

An extremely fast event based microframework/webserver written with Ruby.
Ruby
3
star
27

apm.cr

This is an open source APM agent for Crystal. Initial development is targeting the OpenTelemetry spec for data exchange, but it may also support the New Relic specific protocol. It should, however, be usable with any provider that permits OpenTelementry ingest.
Crystal
3
star
28

shellac

Shellac is a very simple proof-of-concept caching reverse proxy. It is intended mostly to demonstrate a concept, though it could be refined into something more polished.
Ruby
3
star
29

hpack.cr

This shard is a standalone implementation of HPack, the HTTP2 header compression algorithm.
Crystal
3
star
30

advent-of-code-2022

Here are my Advent of Code solutions for 2022.
Rust
3
star
31

alias_method.cr

Crystal does not provide a ready-to-use mechanism for creating method aliases, and the general Crystal code style recommendation is that one should avoid having multiple names that invoke the same method. However, there are times where creating method aliases is useful. This shard creates an alias_method macro that can be used to easily create method aliases which are functionally identical to the original method.
Crystal
3
star
32

eaba2-tools

A small collection of tools for dice rolling, probability simulations, and similar things for use with my EABA2 games.
2
star
33

lockfile.cr

This shard implements a portable lockfile, for use with the Crystal Language.
Crystal
2
star
34

evita

A spike to play with some ideas surrounding a chat bot implementation.
Crystal
2
star
35

NewRelic.cr

This is a binding to the New Relic C SDK, so that Crystal programs can add observability features with New Relic.
Crystal
2
star
36

hash_serializable.cr

The module provides a Hash::Serializeable module which, like JSON::Serializeable and YAML::Serializeable, can be mixed into a class to allow the class's ivars to be serialized out to a hash, and to allow a hash to be instantiated as an instance of a class.
Crystal
2
star
37

learning-crystal-via-conways-game-of-life

This repository is to be used as a lesson or guided workshop to introduce Crystal to someone who already knows how to program, by building a Crystal implementation of Conway's game of life. It includes a codespace to facilitate this lesson.
Shell
2
star
38

time-ext.cr

This is a very small extension of the Time class for Crystal. It has been extracted from another project as it provides a small number of logical and useful extensions to Time.
Crystal
2
star
39

simple_irc.cr

This is a simple IRC protocol implementation. No bundled bot capabilities or anything fancy. Just the basics, made easy to use.
Crystal
2
star
40

git-index

This is a very simple little tool that creates an index of git repositories, keyed by the commit hash codes for the first and second commits in the repository.
Ruby
2
star
41

liquidcrystal

This is a port of the Liquid gem from Shopify, for the Crystal language. It aims to be as compatible as possible with the upstream source of truth that is the Shopify Ruby implementation of Liquid.
Crystal
2
star
42

jekyll-include-with-frontmatter

Implements a couple tags to include files that contain front matter. Inspired by a desire to be able to edit Includes with Netflify-CMS.
Ruby
2
star
43

content

My content workspace
1
star
44

SwiftRPC

An RPC library implementation.
Ruby
1
star
45

ey-cloud-recipes

A starter repo for custom chef recipes on EY's cloud platform
Ruby
1
star
46

crystal-conf-1.0-dynamic-crystal

Find ye herein all of the code examples that are used in my talk.
1
star
47

analogger.cr

This is a port of https://github.com/wyhaines/analogger using Crystal.
Crystal
1
star
48

crypt-xorshift

Implementation of a few variants of the Xorshift family of pseudo random number generators for Ruby, with both pure Ruby and C extension implementations.
Ruby
1
star
49

swiftcore-tasks

This little helper code for managing a queue of tasks to execute is used by multiple software packages, so I am packaging it for reuse.
Ruby
1
star
50

TwitchEventSub.cr

This shard encapsulates logic and handling for interacting with the Twitch EventSub API.
Crystal
1
star
51

simplereactor

A very, very barebones event reactor with timer support, implemented with Ruby, backed by either a Ruby/select() based IO system or a Nio4r based IO system.
Ruby
1
star
52

scrawls-ioengine-multithread

A multithreaded io engine for Scrawls.
Ruby
1
star
53

serf-handler

A simple little library that provides a basic framework for Ruby powered Serf handlers.
Ruby
1
star
54

apm-therelicans

This code is the code that was used for an article on how to build an APM system, written for therelicans.com.
Crystal
1
star
55

ridgepole-executor

This is a script intended to be ran by ridgepole as an external script for performing database changes. It will vector blocking alters through a tool to perform them without full table locks, like pt-osc, while sending the safe SQL to the database more directly.
Ruby
1
star
56

serf-handler.cr

This implements a port of the Ruby Serf Handler gem -- https://github.com/wyhaines/serf-handler -- as a library that can be used to easily build compiled, distributable handler binaries using the Crystal language.
Crystal
1
star