casting 0.6.8 → 0.6.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +45 -1
- data/lib/casting.rb +1 -0
- data/lib/casting/null.rb +18 -0
- data/lib/casting/version.rb +1 -1
- data/test/null_module_test.rb +23 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 817477d1f96444e97690553a806289ef4abfce2f
|
4
|
+
data.tar.gz: f50a67f05c760518b3e2b1b93e817db06c8a2adf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19dd0a57a2a785282fd3eb7bdce9ab973c332b13530000f8419a3fbc6ee69476fddd585073bf72d9b16318c960cdfe9c7c04b496071323a734bd2874e4977a9e
|
7
|
+
data.tar.gz: 88f35215738b381bdac0893fd3e4946c435e8aed8b62876e454f2674b316f53c31b07a93a956d412cc36d93190b2d5fd7ae648885d7f20e9ab29d013f91a52b3
|
data/README.md
CHANGED
@@ -91,7 +91,7 @@ _That's great, but why do I need to do these extra steps? I just want to run the
|
|
91
91
|
|
92
92
|
Casting gives you the option to do what you want. You can run just a single method once, or alter your object to always delegate. Even better, you can alter your object to delegate temporarily...
|
93
93
|
|
94
|
-
|
94
|
+
### Temporary Behavior
|
95
95
|
|
96
96
|
Casting also provides an option to temporarily apply behaviors to an object.
|
97
97
|
|
@@ -147,6 +147,42 @@ actor.hello_world # => NoMethodError
|
|
147
147
|
|
148
148
|
These methods are only defined on your `Casting::Client` object when you tell it to `delegate_missing_methods`. Because these require `method_missing`, they do not exist until you opt-in.
|
149
149
|
|
150
|
+
### Duck-typing with NullObject-like behavior
|
151
|
+
|
152
|
+
Casting has a few modules built in to help with treating your objects like null objects.
|
153
|
+
Take a look at the following example:
|
154
|
+
|
155
|
+
```ruby
|
156
|
+
module SpecialStuff
|
157
|
+
def special_link
|
158
|
+
# some link code
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
special_user.cast_as(SpecialStuff)
|
163
|
+
special_user.special_link # outputs your link
|
164
|
+
```
|
165
|
+
|
166
|
+
If your app, for example, generates a list of info for a collection of users, how do you manage the objects which don't have the expected behavior?
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
[normal_user, other_user, special_user].each do |user|
|
170
|
+
user.special_link #=> blows up for normal_user or other_user
|
171
|
+
end
|
172
|
+
```
|
173
|
+
|
174
|
+
You can cast the other objects with `Casting::Null` or `Casting::Blank`:
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
normal_user.cast_as(Casting::Null)
|
178
|
+
other_user.cast_as(Casting::Blank)
|
179
|
+
special_user.cast_as(SpecialStuff)
|
180
|
+
|
181
|
+
[normal_user, other_user, special_user].each do |user|
|
182
|
+
user.special_link #=> normal_user yields nil, other_user yields "", and special_user yields the special_link
|
183
|
+
end
|
184
|
+
```
|
185
|
+
|
150
186
|
## I have a Rails app, how does this help me?
|
151
187
|
|
152
188
|
Well, a common use for this behavior would be in using decorators.
|
@@ -191,6 +227,8 @@ end
|
|
191
227
|
|
192
228
|
This keeps your code focused on the object you care about.
|
193
229
|
|
230
|
+
Check out [Characterize](http://github.com/saturnflyer/characterize) for hooking into Rails automatically.
|
231
|
+
|
194
232
|
## Oh, my! Could this be used to add behavior like refinements?
|
195
233
|
|
196
234
|
You can apply methods from a delegate to all instances of a class.
|
@@ -312,6 +350,12 @@ If you are using Bundler, add this line to your application's Gemfile:
|
|
312
350
|
gem 'casting'
|
313
351
|
```
|
314
352
|
|
353
|
+
If you're using Ruby 1.9, be sure to use a version lower than 0.7
|
354
|
+
|
355
|
+
```ruby
|
356
|
+
gem 'casting', '~> 0.6.9'
|
357
|
+
```
|
358
|
+
|
315
359
|
And then execute:
|
316
360
|
|
317
361
|
$ bundle
|
data/lib/casting.rb
CHANGED
data/lib/casting/null.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Casting
|
2
|
+
module Null
|
3
|
+
def self.instance_method(name)
|
4
|
+
Empty.instance_method(:null)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
module Blank
|
8
|
+
def self.instance_method(name)
|
9
|
+
Empty.instance_method(:blank)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
module Empty
|
13
|
+
def null(*args, &block); end
|
14
|
+
def blank(*args, &block)
|
15
|
+
""
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/casting/version.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
if test_rebinding_methods?
|
4
|
+
describe Casting::Null do
|
5
|
+
it 'will answer to any method with nil' do
|
6
|
+
client = TestPerson.new
|
7
|
+
client.extend(Casting::Client)
|
8
|
+
attendant = Casting::Null
|
9
|
+
|
10
|
+
assert_nil client.delegate('greet', attendant)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe Casting::Blank do
|
15
|
+
it 'will answer to any method with an empty string' do
|
16
|
+
client = TestPerson.new
|
17
|
+
client.extend(Casting::Client)
|
18
|
+
attendant = Casting::Blank
|
19
|
+
|
20
|
+
assert_empty client.delegate('greet', attendant)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: casting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Gay
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |-
|
14
14
|
Casting assists in method delegation which preserves the binding of 'self' to the object receiving a message.
|
@@ -26,6 +26,7 @@ files:
|
|
26
26
|
- lib/casting/method_consolidator.rb
|
27
27
|
- lib/casting/missing_method_client.rb
|
28
28
|
- lib/casting/missing_method_client_class.rb
|
29
|
+
- lib/casting/null.rb
|
29
30
|
- lib/casting/prepared_delegation.rb
|
30
31
|
- lib/casting/super_delegate.rb
|
31
32
|
- lib/casting/version.rb
|
@@ -41,6 +42,7 @@ files:
|
|
41
42
|
- test/delegation_test.rb
|
42
43
|
- test/method_consolidator_test.rb
|
43
44
|
- test/missing_method_client_test.rb
|
45
|
+
- test/null_module_test.rb
|
44
46
|
- test/module_cleanup_test.rb
|
45
47
|
- test/super_test.rb
|
46
48
|
homepage: http://github.com/saturnflyer/casting
|
@@ -77,5 +79,6 @@ test_files:
|
|
77
79
|
- test/delegation_test.rb
|
78
80
|
- test/method_consolidator_test.rb
|
79
81
|
- test/missing_method_client_test.rb
|
82
|
+
- test/null_module_test.rb
|
80
83
|
- test/module_cleanup_test.rb
|
81
84
|
- test/super_test.rb
|