cls 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +35 -0
- data/lib/cls.rb +26 -0
- metadata +58 -0
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Cls
|
2
|
+
|
3
|
+
https://github.com/garybernhardt/cls
|
4
|
+
|
5
|
+
## DESCRIPTION
|
6
|
+
|
7
|
+
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:
|
8
|
+
|
9
|
+
```
|
10
|
+
class UserPresenter
|
11
|
+
takes :user
|
12
|
+
let(:full_name) { [@user.first_name, @user.last_name].join(" ") }
|
13
|
+
end
|
14
|
+
```
|
15
|
+
|
16
|
+
Contrast that with the standard Ruby version:
|
17
|
+
|
18
|
+
```
|
19
|
+
class UserPresenter
|
20
|
+
def initialize(user)
|
21
|
+
@user = user
|
22
|
+
end
|
23
|
+
|
24
|
+
def full_name
|
25
|
+
[@user.first_name, @user.last_name].join(" ")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
Pretty big difference, huh?
|
31
|
+
|
32
|
+
## CAVEATS
|
33
|
+
|
34
|
+
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.
|
35
|
+
|
data/lib/cls.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Cls
|
2
|
+
VERSION = "0.0.1"
|
3
|
+
def self.included(base)
|
4
|
+
base.send :extend, ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def takes(*args)
|
9
|
+
define_initialize(args)
|
10
|
+
end
|
11
|
+
|
12
|
+
def define_initialize(args)
|
13
|
+
assignments = args.map { |a| "@#{a} = #{a}" }.join("\n")
|
14
|
+
self.class_eval %{
|
15
|
+
def initialize(#{args.join(", ")})
|
16
|
+
#{assignments}
|
17
|
+
end
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def let(name, &block)
|
22
|
+
define_method(name, &block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cls
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gary Bernhardt
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-26 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &2157269460 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2157269460
|
25
|
+
description: ! 'Cls: terse syntax for your classes'
|
26
|
+
email:
|
27
|
+
- gary.bernhardt@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- lib/cls.rb
|
33
|
+
- README.md
|
34
|
+
homepage: https://github.com/garybernhardt/cls
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: 1.3.6
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.6
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: ! 'Cls: terse syntax for your classes'
|
58
|
+
test_files: []
|