Project

attire

0.0
No commit activity in last 3 years
No release in over 3 years
Helper to remove some boiler plate in defining classes.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 3.1.0, ~> 3.1
 Project Readme

Attire

Build Status Gem Version

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

  1. Fork it ( https://github.com/[my-github-username]/attire/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 a new Pull Request