callable 0.0.3 → 0.0.4
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 +24 -84
- data/Rakefile +1 -0
- data/lib/callable.rb +2 -1
- data/lib/callable/version.rb +1 -1
- data/spec/callable_spec.rb +7 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e768ec5b42620b5cddec4479ea0ce5018d32d462
|
4
|
+
data.tar.gz: 430ea16944fb5b4684b25b747edae15906f8f5c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4184989f5a317828f747c8638276c3997a95ac95f61bb208015b088d4b4fa70fbba75ab5921a371c15c37fbe2f857adad93ef2bd63451ad2edbacbd619a0552
|
7
|
+
data.tar.gz: 3ec6e7a740d65954e64bc9b8a40d1ad5c99586050c91356982a1e38e6acca4a919e18816a72476b6f66c228be1265d959d7e34b30a1a8b9e615161384c4e6aee
|
data/README.md
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Create callable objects on the fly.
|
4
4
|
|
5
|
-
It's easy to create a
|
6
|
-
as an object that supports the call method), you just wrap it in a
|
5
|
+
It's easy to create a callable object in Ruby (understanding callable
|
6
|
+
as an object that supports the `#call` method), you just wrap it in a
|
7
7
|
lambda and that's it.
|
8
8
|
|
9
|
-
Although this approach is correct, it
|
9
|
+
Although this approach is correct, it lacks some expressiveness. Wouldn't it
|
10
10
|
be better to just say:
|
11
11
|
|
12
12
|
```ruby
|
@@ -20,15 +20,18 @@ This gem allows you to do exactly that (see Usage)
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
24
|
-
in one of two ways (don't forget to install the gem first).
|
23
|
+
To use this library, you first need to require it. That's all the setup you need.
|
25
24
|
|
26
|
-
|
25
|
+
```ruby
|
26
|
+
require "callable"
|
27
|
+
```
|
28
|
+
|
29
|
+
If you need to return a callable object for some reason, you can do it by invoking the callable method:
|
27
30
|
|
28
31
|
```ruby
|
29
32
|
c = Callable( :ret_val )
|
30
33
|
c.call
|
31
|
-
=> ret_val
|
34
|
+
# => ret_val
|
32
35
|
```
|
33
36
|
|
34
37
|
Take into account that if you pass a callable object (such as a
|
@@ -37,10 +40,12 @@ lambda), you'll get it back as the return value:
|
|
37
40
|
```ruby
|
38
41
|
c = Callable( ->{ :ret_val } )
|
39
42
|
c.call
|
40
|
-
=> ret_val
|
43
|
+
# => ret_val
|
44
|
+
c
|
45
|
+
# => #<Proc:0x0000000261e138@-:6 (lambda)>
|
41
46
|
```
|
42
47
|
|
43
|
-
The gem also ships with a #callable? method
|
48
|
+
The gem also ships with a #callable? method that returns true if the
|
44
49
|
object is callable and false if it's not.
|
45
50
|
|
46
51
|
```ruby
|
@@ -57,7 +62,7 @@ This is the same as saying
|
|
57
62
|
xxx.respond_to? :call
|
58
63
|
```
|
59
64
|
|
60
|
-
But I felt it would be more illustrative of
|
65
|
+
But I felt it would be more illustrative of its purpose.
|
61
66
|
|
62
67
|
## Where to use it?
|
63
68
|
|
@@ -107,90 +112,25 @@ And if we switch the policy value:
|
|
107
112
|
# >>
|
108
113
|
```
|
109
114
|
|
110
|
-
This allows us to have a lot of flexibility. But we could provide the user a way to say the same with less code
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
*******************************
|
117
|
-
Let me say where to use this gem with a very
|
118
|
-
trivial example.
|
119
|
-
|
120
|
-
Imagine we have some class that admits an informer object that
|
121
|
-
responds to the get_info method and returns a some information on a
|
122
|
-
String.
|
123
|
-
|
124
|
-
```ruby
|
125
|
-
class SomeClass
|
126
|
-
attr_writer :informer
|
127
|
-
def info
|
128
|
-
@informer.get_info
|
129
|
-
end
|
130
|
-
end
|
131
|
-
```
|
132
|
-
|
133
|
-
If we want to use this "informer" object, we must define a new class
|
134
|
-
or module that responds to the "get_info" method.
|
135
|
-
|
136
|
-
When we have a case like this, is a common practice to name that
|
137
|
-
method "call", instead of "get_info", because we now can toss a simple
|
138
|
-
lambda to substitute it. We can rewrite the code above like this:
|
139
|
-
|
140
|
-
```ruby
|
141
|
-
class SomeClass
|
142
|
-
attr_writer :informer
|
143
|
-
def info
|
144
|
-
@informer.call
|
145
|
-
end
|
146
|
-
end
|
147
|
-
```
|
115
|
+
This allows us to have a lot of flexibility. But we could provide the user a way to say the same with a little less code.
|
148
116
|
|
149
|
-
|
150
|
-
method. In that call method, we can get as fancy as we want:
|
117
|
+
If we wrap the policy to call with the Callable method:
|
151
118
|
|
152
119
|
```ruby
|
153
|
-
|
154
|
-
|
155
|
-
# retrieve the information we need from wherever we want
|
156
|
-
# maybe a web service
|
157
|
-
# maybe a local file
|
158
|
-
end
|
120
|
+
def do_if(permission, policies=POLICIES)
|
121
|
+
yield if Callable(policies[permission]).call
|
159
122
|
end
|
160
123
|
```
|
161
124
|
|
162
|
-
|
163
|
-
|
164
|
-
```ruby
|
165
|
-
something = SomeClass.new
|
166
|
-
something.informer = Informer
|
167
|
-
something.info
|
168
|
-
```
|
169
|
-
|
170
|
-
We trigger some weird and complex logic.
|
171
|
-
|
172
|
-
But when we test our code (or in some special case), we need that
|
173
|
-
logic to be as simple (an decoupled) as it can get.
|
174
|
-
|
175
|
-
Say we now want info to return just a fixed string saying "No info
|
176
|
-
available". With a lambda is fairly easy to do it
|
177
|
-
|
178
|
-
```ruby
|
179
|
-
something = SomeClass.new
|
180
|
-
something.informer = ->{ "No info available" }
|
181
|
-
something.info
|
182
|
-
```
|
183
|
-
|
184
|
-
Here is where the Callable gem comes in handy, we could say the same
|
185
|
-
thing like this:
|
125
|
+
Now we can put the raw value we want to get back without the need of the lambda
|
186
126
|
|
187
127
|
```ruby
|
188
|
-
|
189
|
-
|
190
|
-
|
128
|
+
POLICIES = {
|
129
|
+
development: true
|
130
|
+
}
|
191
131
|
```
|
192
132
|
|
193
|
-
|
133
|
+
Even though using a lambda adds just a few more characters, in my opinion, it clutters the code. By being able to leave it out, the code reads much better.
|
194
134
|
|
195
135
|
## Installation
|
196
136
|
|
data/Rakefile
CHANGED
data/lib/callable.rb
CHANGED
data/lib/callable/version.rb
CHANGED
data/spec/callable_spec.rb
CHANGED
@@ -75,4 +75,11 @@ scope Callable do
|
|
75
75
|
@c.call("Call", "me", "now") == "Call me now"
|
76
76
|
end
|
77
77
|
end
|
78
|
+
scope "default value" do
|
79
|
+
spec "returns the default value when passing nil" do
|
80
|
+
@c = Callable(nil, default: "DEFAULT VALUE")
|
81
|
+
|
82
|
+
@c.call == "DEFAULT VALUE"
|
83
|
+
end
|
84
|
+
end
|
78
85
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: callable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Federico Iachetti
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
90
|
version: '0'
|
91
91
|
requirements: []
|
92
92
|
rubyforge_project:
|
93
|
-
rubygems_version: 2.4.
|
93
|
+
rubygems_version: 2.4.6
|
94
94
|
signing_key:
|
95
95
|
specification_version: 4
|
96
96
|
summary: It allows you to define callable objects.
|