cls 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -2
- data/lib/cls.rb +14 -19
- metadata +5 -5
data/README.md
CHANGED
@@ -6,8 +6,9 @@ https://github.com/garybernhardt/cls
|
|
6
6
|
|
7
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
8
|
|
9
|
-
```
|
9
|
+
```ruby
|
10
10
|
class UserPresenter
|
11
|
+
extend Cls
|
11
12
|
takes :user
|
12
13
|
let(:full_name) { [@user.first_name, @user.last_name].join(" ") }
|
13
14
|
end
|
@@ -15,7 +16,7 @@ end
|
|
15
16
|
|
16
17
|
Contrast that with the standard Ruby version:
|
17
18
|
|
18
|
-
```
|
19
|
+
```ruby
|
19
20
|
class UserPresenter
|
20
21
|
def initialize(user)
|
21
22
|
@user = user
|
@@ -29,6 +30,14 @@ end
|
|
29
30
|
|
30
31
|
Pretty big difference, huh?
|
31
32
|
|
33
|
+
## HISTORY
|
34
|
+
|
35
|
+
Cls started as the Shorty class in [Raptor](https://github.com/garybernhardt/raptor). There's also a Destroy All Software [screencast](https://www.destroyallsoftware.com/screencasts/catalog/shorter-class-syntax) that discusses the implementation.
|
36
|
+
|
37
|
+
## WHY NOT USE STRUCT?
|
38
|
+
|
39
|
+
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.
|
40
|
+
|
32
41
|
## CAVEATS
|
33
42
|
|
34
43
|
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.
|
data/lib/cls.rb
CHANGED
@@ -1,26 +1,21 @@
|
|
1
1
|
module Cls
|
2
|
-
VERSION = "0.0.
|
3
|
-
def self.included(base)
|
4
|
-
base.send :extend, ClassMethods
|
5
|
-
end
|
2
|
+
VERSION = "0.0.2"
|
6
3
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
4
|
+
def takes(*args)
|
5
|
+
define_initialize(args)
|
6
|
+
end
|
11
7
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
8
|
+
def define_initialize(args)
|
9
|
+
assignments = args.map { |a| "@#{a} = #{a}" }.join("\n")
|
10
|
+
self.class_eval %{
|
11
|
+
def initialize(#{args.join(", ")})
|
12
|
+
#{assignments}
|
13
|
+
end
|
14
|
+
}
|
15
|
+
end
|
20
16
|
|
21
|
-
|
22
|
-
|
23
|
-
end
|
17
|
+
def let(name, &block)
|
18
|
+
define_method(name, &block)
|
24
19
|
end
|
25
20
|
end
|
26
21
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cls
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2156645160 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2156645160
|
25
25
|
description: ! 'Cls: terse syntax for your classes'
|
26
26
|
email:
|
27
27
|
- gary.bernhardt@gmail.com
|
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
51
|
version: 1.3.6
|
52
52
|
requirements: []
|
53
53
|
rubyforge_project:
|
54
|
-
rubygems_version: 1.8.
|
54
|
+
rubygems_version: 1.8.10
|
55
55
|
signing_key:
|
56
56
|
specification_version: 3
|
57
57
|
summary: ! 'Cls: terse syntax for your classes'
|