Skip to content

attr_reader + initialize with keyword arguments

License

Notifications You must be signed in to change notification settings

etiennebarrie/kwattr

Repository files navigation

KWattr

Keyword arguments meet attr_reader and initialize:

class FooBar
  kwattr :foo, bar: 21
end

foobar = FooBar.new(foo: 42) # => #<FooBar @foo=42, @bar=21>
foobar.foo # => 42
foobar.bar # => 21

instead of

class FooBar
  attr_reader :foo, :bar

  def initialize(foo:, bar: 21)
    @foo = foo
    @bar = bar
  end
end

initialize

The provided initialize is prepended so there's no need to call super, and attributes are already set when your code is reached.

class BarInitialize
  kwattr foo: 42

  def initialize(bar: 2)
    @bar = foo / bar
    yield @bar if block_given?
  end
end
BarInitialize.new # => #<BarInitialize @foo=42, @bar=21>

The prepended initialize passes on any block it receives, so you can yield from your initialize as usual.

default_bar = nil
BarInitialize.new { |bar| default_bar = bar }
default_bar # => 21

return value

It returns the list of keyword attributes so you can combine with methods like Module#protected.

class FooProtected
  protected *kwattr(foo: 42)
end
FooProtected.new # => #<FooProtected @foo=42>
FooProtected.protected_instance_methods # => [:foo]

useful exception messages

When it can, kwattr tries to extract the required keyword arguments from the super method to show more useful exception messages.

class FooBar2
  kwattr :foo

  def initialize(bar:)
  end
end
FooBar2.new
# Ruby 2.2 and later: ArgumentError: missing keywords: bar, foo
# Rubinius:           ArgumentError: missing keyword: foo

compatibility

  • Ruby 2.6, Ruby 2.5, Ruby 2.4,Ruby 2.3, Ruby 2.2, TruffleRuby and JRuby 9000 are fully supported.

  • Rubinius is supported, but exceptions don't include keywords from super.

free features

kwattr has no specific code to support those, but they work and are supported use cases.

subclass

class Foo
  kwattr :foo
end

class FooWithBar < Foo
  kwattr :bar
end

FooWithBar.new(foo: 42, bar: 21) # => #<FooWithBar @foo=42, @bar=21>

include

module Mod
  kwattr :mod
end

class Inc
  include Mod
end

Inc.new(mod: 42)

See also

Installation

Add this line to your application's Gemfile:

gem 'kwattr'

And then execute:

$ bundle

Or install it yourself as:

$ gem install kwattr

Development

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

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/etiennebarrie/kwattr. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

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

Code of Conduct

Everyone interacting in the KWattr project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

attr_reader + initialize with keyword arguments

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published