junk_drawer 1.0.0 → 1.1.0
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/.rubocop.yml +3 -0
- data/README.md +68 -9
- data/lib/junk_drawer/callable.rb +8 -0
- data/lib/junk_drawer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 033f2ab3b77953431ed14a2e0cfdf6f0b7f8d80b
|
4
|
+
data.tar.gz: 0b1b9ddb51d43c0760585b915f8448719d8b6ea8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 452971946ec2297e949ce82a14805732ae35e1c5862d71681af2f35dd9ad287f8596c933caede70c45409f7b4c095888af3041968ebdd01a047b438ec3e0928d
|
7
|
+
data.tar.gz: 5f5d67af5ba7ddeed0e9a5dc0003a1af62123a30393796da18a1ad76b9e99a0c1c80873c6d1e5abbf8946f9e3da01b7e771fec6fba55ce53e87454ff3f7641b7
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -1,11 +1,7 @@
|
|
1
1
|
# JunkDrawer
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
file `lib/junk_drawer`. To experiment with that code, run `bin/console` for an
|
6
|
-
interactive prompt.
|
7
|
-
|
8
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
`JunkDrawer` is a gem providing exactly one (and someday more) random utility
|
4
|
+
that is commonly useful across projects.
|
9
5
|
|
10
6
|
## Installation
|
11
7
|
|
@@ -25,7 +21,71 @@ Or install it yourself as:
|
|
25
21
|
|
26
22
|
## Usage
|
27
23
|
|
28
|
-
|
24
|
+
### Callable
|
25
|
+
|
26
|
+
`JunkDrawer::Callable` is a module that provides constraints and conveniences
|
27
|
+
for objects that implement a single method `#call`. It comes with the
|
28
|
+
philosophy that objects that *do* something, should do only one thing. When including the `JunkDrawer::Callable` in one of your classes, you will get the following:
|
29
|
+
|
30
|
+
1) It raises an error if you try to implement a public method other than
|
31
|
+
`#call`.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class Foo
|
35
|
+
include JunkDrawer::Callable
|
36
|
+
def bar # Bad: can't define public method "#bar"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
produces:
|
42
|
+
|
43
|
+
```
|
44
|
+
JunkDrawer::CallableError: invalid method name bar, only public method allowed is "call"
|
45
|
+
```
|
46
|
+
|
47
|
+
Private methods are fine:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
class Foo
|
51
|
+
include JunkDrawer::Callable
|
52
|
+
private
|
53
|
+
def bar # private methods are okay!
|
54
|
+
end
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
2) It delegates `call` on the class to a new instance:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
class Foo
|
62
|
+
include JunkDrawer::Callable
|
63
|
+
def call(stuff)
|
64
|
+
puts "I am a Foo! I've got #{stuff}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
```
|
70
|
+
> Foo.call('a brochure')
|
71
|
+
I am a Foo! I've got a brochure
|
72
|
+
> Foo.new.call('a brochure')
|
73
|
+
I am a Foo! I've got a brochure
|
74
|
+
```
|
75
|
+
|
76
|
+
3) It implements `to_proc`, both on the class and instance, allowing operations
|
77
|
+
such as:
|
78
|
+
|
79
|
+
```
|
80
|
+
> ['puppies', 'a cold', 'cheeseburgers'].each(&Foo)
|
81
|
+
I am a Foo! I've got puppies
|
82
|
+
I am a Foo! I've got a cold
|
83
|
+
I am a Foo! I've got cheeseburgers
|
84
|
+
```
|
85
|
+
|
86
|
+
See here for a great explanation of `to_proc` and the `&` operator:
|
87
|
+
|
88
|
+
http://www.brianstorti.com/understanding-ruby-idiom-map-with-symbol/
|
29
89
|
|
30
90
|
## Development
|
31
91
|
|
@@ -41,11 +101,10 @@ git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygem
|
|
41
101
|
## Contributing
|
42
102
|
|
43
103
|
Bug reports and pull requests are welcome on GitHub at
|
44
|
-
https://github.com/
|
104
|
+
https://github.com/mockdeep/junk_drawer. This project is intended to be a
|
45
105
|
safe, welcoming space for collaboration, and contributors are expected to
|
46
106
|
adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
47
107
|
|
48
|
-
|
49
108
|
## License
|
50
109
|
|
51
110
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/lib/junk_drawer/callable.rb
CHANGED
@@ -10,6 +10,10 @@ module JunkDrawer
|
|
10
10
|
raise NotImplementedError
|
11
11
|
end
|
12
12
|
|
13
|
+
def to_proc
|
14
|
+
proc { |args| self.(*args) }
|
15
|
+
end
|
16
|
+
|
13
17
|
# `ClassMethods` defines a class level method `call` that delegates to
|
14
18
|
# an instance. It also causes an error to be raised if a public instance
|
15
19
|
# method is defined with a name other than `call`
|
@@ -18,6 +22,10 @@ module JunkDrawer
|
|
18
22
|
new.(*args)
|
19
23
|
end
|
20
24
|
|
25
|
+
def to_proc
|
26
|
+
new.to_proc
|
27
|
+
end
|
28
|
+
|
21
29
|
def method_added(method_name)
|
22
30
|
return if method_name == :call || !public_method_defined?(method_name)
|
23
31
|
|
data/lib/junk_drawer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: junk_drawer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Fletcher
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|