Skip to content
This repository has been archived by the owner on Jan 15, 2019. It is now read-only.

garybernhardt/cls

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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:

About

Cls: terse syntax for your classes

Resources

Stars

Watchers

Forks

Packages

No packages published