https://github.com/kostya/auto_constructor
Auto construct initialize methods for classes and structs
https://github.com/kostya/auto_constructor
auto construct crystal
Last synced: 9 months ago
JSON representation
Auto construct initialize methods for classes and structs
- Host: GitHub
- URL: https://github.com/kostya/auto_constructor
- Owner: kostya
- License: mit
- Created: 2017-02-01T17:14:49.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-06-24T14:40:43.000Z (over 7 years ago)
- Last Synced: 2025-05-12T22:55:35.590Z (9 months ago)
- Topics: auto, construct, crystal
- Language: Crystal
- Homepage:
- Size: 20.5 KB
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# THIS PROJECT DEPRECATED, use https://github.com/kostya/auto_initialize instead.
# 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) # => #
```
## 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") # => #
```
## 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")
```