auto_constructor
auto_constructor
Auto construct initialize methods for classes and structs
Installation
Add this to your application's shard.yml
:
dependencies:
auto_constructor:
github: kostya/auto_constructor
Usage
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
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
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
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")