module Prop

Overview

Prop utilities.

Mixin module that should be included in a class or a struct. This module improves the std's accessor macros (getter, getter!, getter?, property, ...).

If a block is defined to an instance variable, it will be executed on the initialization of the instance (called by each initialize).

property my_var : String = "default value" do |default_value|
  # Remove leading and trailing whitespace
  # "    hello    ".strip # => "hello"
  @my_var = @my_var.strip
end

You can provide arguments:

property my_var : String = "default value", "any type of argument" do |default_value, args|
  puts args
  @my_var = @my_var.strip
end

The behavior can easily be extended:

module CustomProp
  macro included
    include Prop

    macro finished
      {% verbatim do %}
        {% for k, prop in PROPS %}
          {% if prop[:args] %}
            some_ioc_method({{prop[:name]}}: { {{prop[:args].double_splat}} })
          {% end %}
        {% end %}
      {% end %}
    end
  end
end

See README for more details.

If you are looking for a validator to validate data before instantiating a class or a struct, you may be interested by validator. This validator shard uses Prop internally to define and handle validation rules on each instance variable.

Defined in:

prop.cr