0.0
No commit activity in last 3 years
No release in over 3 years
Define an initializer along with attribute accessors in one line
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 2.0
~> 10.0
~> 3.0
 Project Readme

Build Status

DefInitialize

Another approach to reduce initialization boilerplate

Installation

Add this line to your application's Gemfile:

gem 'def_initialize'

And then execute:

$ bundle

Or install it yourself as:

$ gem install def_initialize

Usage

class Employee
  include DefInitialize.with("name, uuid = SecureRandom.uuid, *params, age:, position: 'manager', **opts")
end

# is the same as:

class Employee
  def initialize(name, uuid = SecureRandom.uuid, *params, age:, position: 'manager', **opts)
    @name = name
    @uuid = uuid
    @params = params
    @age = age
    @position = position
    @opts = opts
  end

  private

  attr_reader :name, :uuid, :params, :age, :position, :opts
end

By convention, parameters starting with underscore symbol '_' are ignored:

class Point
  include DefInitialize.with("x, y, _c")
end

# transforms to:

class Point
  def initialize(x, y, _c) # Note that `_c` is still required to pass
    @x = x
    @y = y
  end

  private

  attr_reader :x, :y
end

DSL

Alternatively, you can extend a class with DefInitialize::DSL and use def_initialize method. Note, how close it looks to the native declaration!

class Base
  extend DefInitialize::DSL
end

class Circle < Base
  def_initialize("radius")
end

class Rectangle < Base
  def_initialize("length, width")
end

Access control

You can specify level of access for your readers and writers:

class Person < Base
  def_initialize("name", readers: :public, writers: :private)
end

# transforms to:

class Person
  def initialize(name)
    @name = name
  end

  attr_reader :name

  private

  attr_writer :name
end

Allowed values are :public, :private, :protected and nil. If value is nil, accessors won't be defined at all.

default value for readers is private, default value for writers is nil

What should I do in more complex cases?

Just write plain old def initialize.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/antoshalee/def_initialize.

License

The gem is available as open source under the terms of the MIT License.