Skip to content

mushishi78/attire

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Helper to remove some boiler plate in defining classes.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages