auto_constructor

Version, currently 0.23 versions

github.com/kostya/auto_constructor

Auto construct initialize methods for classes and structs

9 stars
1 dependent
License: MIT

Nothing has been indexed for 0.2 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:
  auto_constructor:
    github: kostya/auto_constructor
    version: ~> 0.2

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.2. 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.

# auto_constructor

Auto construct initialize methods for classes and structs

## Installation

Add this to your application's `shard.yml`:

```yaml
dependencies:
  auto_constructor:
    github: kostya/auto_constructor
```

## Usage

```crystal
require "auto_constructor"

struct A
  include AutoConstructor

  field :a, Int32
  field :b, String, default: "def"
  field :c, Int32
  field :d, String?
  field :e, Float64
end

p A.new(1, "what", 3, "bla", 1.0)                # => A(@a=1, @b="what", @c=3, @d="bla", @e=1.0)
p A.new(a: 1, b: "what", c: 3, d: "bla", e: 1.0) # => A(@a=1, @b="what", @c=3, @d="bla", @e=1.0)
p A.new(a: 1, c: 3, e: 1.0)                      # => A(@a=1, @b="def", @c=3, @d=nil, @e=1.0)
p A.new({:a => 1, :c => 3, :e => 1.0})           # => A(@a=1, @b="def", @c=3, @d=nil, @e=1.0)
p A.new({"a" => 1, "c" => 3, "e" => 1.0})        # => A(@a=1, @b="def", @c=3, @d=nil, @e=1.0)
```

## Fields options

```
  :default - set default value
  :getter - (enabled by default, false to disable)
  :setter - (enabled by default, false to disable)
```

## After initialize hook

```crystal
require "auto_constructor"

class A
  include AutoConstructor
  field :x, Int32

  property y : Int32

  after_initialize do
    @y = @x + 1
  end
end

p A.new(1) # => #<A:0x10befc0 @x=1, @y=2>
```

## Auto expanding classes

```crystal
require "auto_constructor"

# some base class
class A
  include AutoConstructor
  field :x, Int32
end

# some user code extend this class, with another field
class A
  field :y, String
end

p A.new(1, "bla") # => #<A:0x1032d2f00 @x=1, @y="bla">
```

## Example usage with auto_json and auto_msgpack

```crystal
require "auto_json"
require "auto_msgpack"

struct Person
  include AutoJson
  include AutoMsgpack

  field :name, String
  field :age, Int32
  field :email, String?, json_key: "mail"
  field :balance, Float64, default: 0.0, json: false
  field :data, String?, msgpack: false
end

person = Person.new(name: "Vasya", age: 20, balance: 10.0, email: "bla@ru")
p person # => Person(@age=20, @balance=10.0, @data=nil, @email="bla@ru", @name="Vasya")

json = person.to_json
puts json # => {"name":"Vasya","age":20,"mail":"bla@ru"}

person2 = Person.from_json(json)
p person2 # => Person(@age=20, @balance=0.0, @data=nil, @email="bla@ru", @name="Vasya")

msgpack = person2.to_msgpack
puts msgpack # => Bytes[132, 164, 110, ...]

person3 = Person.from_msgpack(msgpack)
p person3 # Person(@age=20, @balance=0.0, @data=nil, @email="bla@ru", @name="Vasya")
```