callable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +180 -0
- data/Rakefile +3 -0
- data/callable.gemspec +24 -0
- data/lib/callable.rb +5 -0
- data/lib/callable/version.rb +3 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1ef39ede893ce0a86d29f34da19b77b7b2850090
|
4
|
+
data.tar.gz: d05ad76db5f5914b825e8c23f2a4b6453c598c33
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 00df6afe990ba3facb4d54ac73fb9ee4057086db2546afaba496eec83181ef7147e89b84a9691873357fab4f30adaa6a18ee3db371353618017879be35d0bde6
|
7
|
+
data.tar.gz: 7ed99cd5b51200dbce8bd34192272af15bf7c7b9c627eb661f6647f6d8145a48ba31a6516c11e89bc3d1fb475a230a1517faeae890df41bf875a3be455c4f139
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Federico Iachetti
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
# Callable
|
2
|
+
|
3
|
+
Create callable objects on the fly.
|
4
|
+
|
5
|
+
It's easy to create a calable object in Ruby (understandong callable
|
6
|
+
as an object that supports the call method), you just wrap it in a
|
7
|
+
lambda and that's it.
|
8
|
+
|
9
|
+
Although this approach is correct, it lucks some expresiveness. Wouldn't it
|
10
|
+
be better to just say:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
Callable( :i_wasnt_callable_but_i_am_now )
|
14
|
+
```
|
15
|
+
|
16
|
+
This line of code tells you exacly what it's doing.
|
17
|
+
|
18
|
+
This gem allows you to do exactly that (see Usage)
|
19
|
+
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
If you need to return a callable object for some reason, you can do it
|
24
|
+
in one of two ways (don't forget to install the gem first).
|
25
|
+
|
26
|
+
The first way is by invoking the callable method:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
c = Callable( :ret_val )
|
30
|
+
c.call
|
31
|
+
=> ret_val
|
32
|
+
```
|
33
|
+
|
34
|
+
Take into account that if you pass a callable object (such as a
|
35
|
+
lambda), you'll get it back as the return value:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
c = Callable( ->{ :ret_val } )
|
39
|
+
c.call
|
40
|
+
=> ret_val
|
41
|
+
```
|
42
|
+
|
43
|
+
The second way to use it is invoking the #callable directly on the object you
|
44
|
+
want to be callable:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
c = :ret_val.callable
|
48
|
+
c.call
|
49
|
+
=> ret_val
|
50
|
+
```
|
51
|
+
|
52
|
+
Like the example before, lambda, you'll get it back as the return value:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
c = ->{ :ret_val }.callable
|
56
|
+
c.call
|
57
|
+
=> ret_val
|
58
|
+
```
|
59
|
+
|
60
|
+
The gem also ships with a #callable? method thar returns true if the
|
61
|
+
object is callable and false if it's not.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
:not_callable.callable?
|
65
|
+
=> false
|
66
|
+
->{ :not_callable }.callable?
|
67
|
+
=> true
|
68
|
+
```
|
69
|
+
|
70
|
+
This is the same as saying
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
xxx.respond_to? :call
|
74
|
+
```
|
75
|
+
|
76
|
+
But I felt it would be more illustrative of it's purpose.
|
77
|
+
|
78
|
+
## Where to use it?
|
79
|
+
|
80
|
+
Let me say where to use this gem (with a very
|
81
|
+
trivial example).
|
82
|
+
|
83
|
+
Imagine we have some class that admits an informer object that
|
84
|
+
responds to the get_info method and returns a some information on a
|
85
|
+
String.
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
class SomeClass
|
89
|
+
attr_writer :informer
|
90
|
+
def info
|
91
|
+
@informer.get_info
|
92
|
+
end
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
If we want to use this "informer" object, we must define a new class
|
97
|
+
or module that responds to the "get_info" method.
|
98
|
+
|
99
|
+
When we have a case like this, is a common practice to name that
|
100
|
+
method "call", instead of "get_info", because we now can toss a simple
|
101
|
+
lambda to substitute it. We can rewrite the code above like this:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
class SomeClass
|
105
|
+
attr_writer :informer
|
106
|
+
def info
|
107
|
+
@informer.call
|
108
|
+
end
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
And now we can define a class or module that responds to the call
|
113
|
+
method. In that call method, we can get as fancy as we want:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
module Informer
|
117
|
+
def call
|
118
|
+
# retrieve the information we need from wherever we want
|
119
|
+
# maybe a web service
|
120
|
+
# maybe a local file
|
121
|
+
end
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
So, when we do:
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
something = SomeClass.new
|
129
|
+
something.informer = Informer
|
130
|
+
something.info
|
131
|
+
```
|
132
|
+
|
133
|
+
We trigger some weird and complex logic.
|
134
|
+
|
135
|
+
But when we test our code (or in some special case), we need that
|
136
|
+
logic to be as simple (an decoupled) as it can get.
|
137
|
+
|
138
|
+
Say we now want info to return just a fixed string saying "No info
|
139
|
+
available". With a lambda is fairly easy to do it
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
something = SomeClass.new
|
143
|
+
something.informer = ->{ "No info available" }
|
144
|
+
something.info
|
145
|
+
```
|
146
|
+
|
147
|
+
Here is where the Callable gem comes in handy, we could say the same
|
148
|
+
thing like this:
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
something = SomeClass.new
|
152
|
+
something.informer = Callable "No info available"
|
153
|
+
something.info
|
154
|
+
```
|
155
|
+
|
156
|
+
And now is much more expressive.
|
157
|
+
|
158
|
+
## Installation
|
159
|
+
|
160
|
+
Add this line to your application's Gemfile:
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
gem 'callable'
|
164
|
+
```
|
165
|
+
|
166
|
+
And then execute:
|
167
|
+
|
168
|
+
$ bundle
|
169
|
+
|
170
|
+
Or install it yourself as:
|
171
|
+
|
172
|
+
$ gem install callable
|
173
|
+
|
174
|
+
## Contributing
|
175
|
+
|
176
|
+
1. Fork it ( https://github.com/[my-github-username]/callable/fork )
|
177
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
178
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
179
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
180
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/callable.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'callable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "callable"
|
8
|
+
spec.version = Callable::VERSION
|
9
|
+
spec.authors = ["Federico Iachetti"]
|
10
|
+
spec.email = ["iachetti.federico@gmail.com"]
|
11
|
+
spec.summary = %q{It allows you to define callable objects.}
|
12
|
+
spec.description = %q{It makes available the callable, Callable and callable? methods that allows you to make callable objects and know if an object can be called.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "matest", "~> 1.5"
|
24
|
+
end
|
data/lib/callable.rb
ADDED
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: callable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Federico Iachetti
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: matest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
description: It makes available the callable, Callable and callable? methods that
|
56
|
+
allows you to make callable objects and know if an object can be called.
|
57
|
+
email:
|
58
|
+
- iachetti.federico@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- callable.gemspec
|
69
|
+
- lib/callable.rb
|
70
|
+
- lib/callable/version.rb
|
71
|
+
homepage: ''
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.4.5
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: It allows you to define callable objects.
|
95
|
+
test_files: []
|