Attire
Helper to remove some boiler plate in defining classes.
Usage
The attire
method defines an initialize
method where all it's parameters are stored as instance variables that can be retrieve with private getters. So a class defined like this:
class Measurement
def initialize(value:, units: :grams)
@value = value
@units = units
end
def to_s
"#{value} (#{units})"
end
private
attr_reader :value, :units
end
Can be shortened to:
require 'attire'
class Measurement
attire 'value:, units: :grams'
def to_s
"#{value} (#{units})"
end
end
Method Objects
Sometimes it's useful for objects that are designed to do only a single task to have a class method that both initializes the object and executes the task. For this purpose, attire
allows you to set the verb
keyword like so:
class CheeseSpreader
attire 'cheese, cracker: Jacobs.new', verb: :spread
def spread
cracker.spreads << cheese
cracker
end
end
CheeseSpreader.spread(:roquefort)
Installation
Add to Gemfile:
gem 'attire'
Inspirations
Contributing
- Fork it ( https://github.com/[my-github-username]/attire/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request