attr_extras 1.6.1 → 1.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -30
- data/lib/attr_extras.rb +5 -5
- data/lib/attr_extras/version.rb +1 -1
- metadata +6 -6
data/README.md
CHANGED
@@ -77,37 +77,8 @@ x.bar # => "Bar!"
|
|
77
77
|
|
78
78
|
## Why not use `Struct`?
|
79
79
|
|
80
|
-
|
80
|
+
See: ["Struct inheritance is overused"](http://thepugautomatic.com/2013/08/struct-inheritance-is-overused/)
|
81
81
|
|
82
|
-
``` ruby
|
83
|
-
class Greeter < Struct.new(:user)
|
84
|
-
def greet
|
85
|
-
puts "Hello #{user.name}!"
|
86
|
-
end
|
87
|
-
end
|
88
|
-
```
|
89
|
-
|
90
|
-
The `Struct` won't actually require you to provide any arguments. You could do this and it won't complain until `nil.name` explodes on you:
|
91
|
-
|
92
|
-
``` ruby
|
93
|
-
Greeter.new.greet
|
94
|
-
```
|
95
|
-
|
96
|
-
Also, the Greeter will have a public `user` accessor even if you only need it internally, so your public interface is unnecessarily large.
|
97
|
-
|
98
|
-
Further, inheriting from `Struct` arguably suggests that you have a mere data structure, not a full-blown class with rich behavior.
|
99
|
-
|
100
|
-
With `attr_extras`, you have none of these issues:
|
101
|
-
|
102
|
-
``` ruby
|
103
|
-
class Greeter
|
104
|
-
pattr_initialize :user
|
105
|
-
|
106
|
-
def greet
|
107
|
-
puts "Hello #{user.name}!"
|
108
|
-
end
|
109
|
-
end
|
110
|
-
```
|
111
82
|
|
112
83
|
## Why not use `private; attr_reader :foo`?
|
113
84
|
|
@@ -115,6 +86,9 @@ Instead of `attr_private :foo`, you could do `private; attr_reader :foo`.
|
|
115
86
|
|
116
87
|
Other than being more to type, declaring `attr_reader` after `private` will actually give you a warning (deserved or not) if you run Ruby with warnings turned on.
|
117
88
|
|
89
|
+
If you don't want the dependency on `attr_extras`, you can get rid of the warnings with `attr_reader :foo; private :foo` or just define a regular private method.
|
90
|
+
|
91
|
+
|
118
92
|
## Installation
|
119
93
|
|
120
94
|
Add this line to your application's `Gemfile`:
|
data/lib/attr_extras.rb
CHANGED
@@ -21,13 +21,13 @@ module AttrExtras
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def attr_private(*names)
|
24
|
-
attr_reader
|
25
|
-
private
|
24
|
+
attr_reader(*names)
|
25
|
+
private(*names)
|
26
26
|
end
|
27
27
|
|
28
28
|
def pattr_initialize(*names)
|
29
|
-
attr_initialize
|
30
|
-
attr_private
|
29
|
+
attr_initialize(*names)
|
30
|
+
attr_private(*names.flatten)
|
31
31
|
end
|
32
32
|
|
33
33
|
def method_object(method_name, *names)
|
@@ -35,7 +35,7 @@ module AttrExtras
|
|
35
35
|
new(*values).send(method_name)
|
36
36
|
end
|
37
37
|
|
38
|
-
pattr_initialize
|
38
|
+
pattr_initialize(*names)
|
39
39
|
end
|
40
40
|
|
41
41
|
def attr_query(*names)
|
data/lib/attr_extras/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_extras
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.6.
|
4
|
+
version: 1.6.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: 2013-
|
12
|
+
date: 2013-08-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70165595375620 !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: *70165595375620
|
25
25
|
description:
|
26
26
|
email:
|
27
27
|
- henrik@nyh.se
|
@@ -54,7 +54,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
54
|
version: '0'
|
55
55
|
segments:
|
56
56
|
- 0
|
57
|
-
hash:
|
57
|
+
hash: 4094106113878864252
|
58
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
59
|
none: false
|
60
60
|
requirements:
|
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
63
|
version: '0'
|
64
64
|
segments:
|
65
65
|
- 0
|
66
|
-
hash:
|
66
|
+
hash: 4094106113878864252
|
67
67
|
requirements: []
|
68
68
|
rubyforge_project:
|
69
69
|
rubygems_version: 1.8.10
|