Skip to content

alexpeachey/attr_defaultable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Circle CI Coverage Status Code Climate

AttrDefaultable

Easily add attributes with default values to your classes.

Installation

Add this line to your application's Gemfile:

gem 'attr_defaultable'

To optionally slip attr_defaultable into Module so it is available without extending:

gem 'attr_defaultable', require: 'extend_attr_defaultable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install attr_defaultable

Usage

class Example
  extend AttrDefaultable
  attr_defaultable :foo, -> { 'bar' }
end

This will create a getter/setter pair for your variable foo. If the setter is not used before the getter the proc given will be called and it's result used to set the value of foo.

Very useful for creating dependency definitions with protected default values that are evaluated just in time.

Alternate Usage

By using the alternate gem declaration in your Gemfile or manually requiring extend_attr_defaultable early in your application, you can avoid the explicit extend in your class.

class Example
  attr_defaultable :foo, -> { 'bar' }
end

Contributing

  1. Fork it ( http://github.com/Originate/attr_defaultable/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

An attr macro for attributes with default values.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages