Project

cls

0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Cls: terse syntax for your classes
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
 Project Readme

Cls

https://github.com/garybernhardt/cls

DESCRIPTION

Cls lets you define classes with a more terse syntax. It's useful for presenters and other classes that have many small methods. For example:

class UserPresenter
    extend Cls
    takes :user
    let(:full_name) { [@user.first_name, @user.last_name].join(" ") }
end

Contrast that with the standard Ruby version:

class UserPresenter
    def initialize(user)
        @user = user
    end

    def full_name
        [@user.first_name, @user.last_name].join(" ")
    end
end

Pretty big difference, huh?

HISTORY

Cls started as the Shorty class in Raptor. There's also a Destroy All Software screencast that discusses the implementation.

WHY NOT USE STRUCT?

Struct can give you approximately what takes does, but it requires inheriting from Struct and it will default omitted constructor arguments to nil. I don't like it as a solution, inheritance and silent nils both being evil.

CAVEATS

Ruby 1.8 will allow you to pass the wrong number of arguments to a block, which can be confusing. It throws a warning, so you should at least be notified that you're doing it. This problem doesn't exist in Ruby 1.9.

LICENSE

Released under the MIT license: