named_proc 1.0 → 1.1
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 +7 -0
- data/.travis.yml +17 -0
- data/CHANGELOG.rdoc +7 -0
- data/{LICENSE.txt → MIT-LICENSE.txt} +1 -1
- data/README.rdoc +10 -69
- data/lib/named_proc.rb +3 -54
- data/lib/named_proc/core_ext.rb +3 -0
- data/lib/named_proc/implementation.rb +55 -0
- data/lib/named_proc/version.rb +3 -0
- data/named_proc.gemspec +10 -9
- data/spec/named_proc_spec.rb +12 -13
- metadata +39 -28
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cbf28423889dfba4dbbdcb64f38cd3c83dfb9922
|
4
|
+
data.tar.gz: 52119a8217037574c2e8fc755e57d9655737306c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e303b748dbd73e992f7bbc54ce167abaeb9805cca83fa0f7ecb0c25f83312e9d6ac84b8835fb8b5b3f23627ce9f121b541586f0e4d4b8dd3cd60a52b3250645f
|
7
|
+
data.tar.gz: 346258671dd9f5ceac348d2271e20b2f9fcf0f3cbd64a33f8e9edec7b729f82bfe9a1b64ad3bf695d55f17e58917c3155e4d4e3f6fdb31742d1f27b805fb9229
|
data/.travis.yml
ADDED
data/CHANGELOG.rdoc
ADDED
data/README.rdoc
CHANGED
@@ -1,15 +1,18 @@
|
|
1
|
-
=
|
1
|
+
= Named Proc {<img src="https://badge.fury.io/rb/named_proc.svg" />}[http://badge.fury.io/rb/named_proc] {<img src="https://travis-ci.org/janlelis/named_proc.png" />}[https://travis-ci.org/janlelis/named_proc]
|
2
2
|
|
3
|
-
|
3
|
+
When working with a lot of procs, naming them might become handy. It makes identifying them later easier, which can be utilized by your code. For example, it enables improves the usefulness of {multi_block}[https:/github.com/janlelis/multi_block], a gem that allows you to pass multiple procs to methods.
|
4
4
|
|
5
|
-
These gem was build during a codebrawl contest. You might also take a look at the other entries: http://codebrawl.com/contests/methods-taking-multiple-blocks
|
6
5
|
|
7
6
|
== Setup
|
8
7
|
|
9
|
-
|
8
|
+
Add to Gemfile:
|
9
|
+
|
10
|
+
gem 'named_proc'
|
11
|
+
|
10
12
|
|
11
13
|
== Named Procs
|
12
|
-
|
14
|
+
|
15
|
+
A named proc acts like a normal proc, but it has a name attribute. You create it by calling a method with the desired name on +proc+:
|
13
16
|
|
14
17
|
>> a = proc.even?{ |e| e.even? }
|
15
18
|
=> #<NamedProc:0x00000001ffc340@(irb):1>
|
@@ -29,67 +32,5 @@ In the same way, you can create lambdas:
|
|
29
32
|
>> b.lambda?
|
30
33
|
=> true
|
31
34
|
|
32
|
-
==
|
33
|
-
|
34
|
-
|
35
|
-
The first argument given to yield always defines the desired block(s). The other arguments get directly passed to the block(s). So these are example calls to the block:
|
36
|
-
|
37
|
-
yield # calls all given procs without args
|
38
|
-
yield :success # calls :success proc without args
|
39
|
-
yield :success, "Code Brawl!" # calls :success proc with message
|
40
|
-
yield 1 # calls first proc (:success in this case)
|
41
|
-
yield [:success, :bonus] # calls :success and :bonus without args
|
42
|
-
yield [:success, :bonus], "Code Brawl!" # calls both procs with same arg
|
43
|
-
yield success: "Code Brawl!", # calls each keyed proc,
|
44
|
-
error: [500, "Internal Brawl Error"] # values are the args
|
45
|
-
|
46
|
-
Consider these two example methods:
|
47
|
-
|
48
|
-
def ajax
|
49
|
-
yield rand(6) != 0 ? :success : :error # calls the :success block if everything worked well
|
50
|
-
end
|
51
|
-
|
52
|
-
def dice
|
53
|
-
random_number = rand(6)
|
54
|
-
yield random_number, random_number + 1 # calls the n-th block
|
55
|
-
end
|
56
|
-
|
57
|
-
=== Calling methods with multiple blocks
|
58
|
-
|
59
|
-
It's done by calling the +blocks+ helper method:
|
60
|
-
|
61
|
-
ajax &blocks[
|
62
|
-
proc.success do puts "Yeah!" end,
|
63
|
-
proc.error do puts "Error..." end,
|
64
|
-
]
|
65
|
-
|
66
|
-
The dice method could, for example, be called in this way:
|
67
|
-
|
68
|
-
dice &blocks[
|
69
|
-
proc{ ":(" },
|
70
|
-
proc{ ":/" },
|
71
|
-
proc{ ":O" },
|
72
|
-
proc{ ":b" },
|
73
|
-
proc{ ":P" },
|
74
|
-
proc{ rand(42) != 0 ? ":)" : ":D"},
|
75
|
-
]
|
76
|
-
|
77
|
-
== Bonus sugar: Array extension
|
78
|
-
|
79
|
-
If you like the slim <tt>&to_proc</tt> operator, you can further optimize the syntax by calling:
|
80
|
-
|
81
|
-
Array.send :include, MultiBlock::Array
|
82
|
-
|
83
|
-
Now, it's getting real hot:
|
84
|
-
|
85
|
-
do_something, some_argument, &[
|
86
|
-
proc.easy_way do
|
87
|
-
# do it the easy way
|
88
|
-
end,
|
89
|
-
|
90
|
-
proc.complex_way do
|
91
|
-
# use complex heuristics, etc.
|
92
|
-
end,
|
93
|
-
]
|
94
|
-
|
95
|
-
== J-_-L
|
35
|
+
== MIT License
|
36
|
+
The original gist: https://gist.github.com/4b2f5fd0b45118e46d0f
|
data/lib/named_proc.rb
CHANGED
@@ -1,54 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
class NamedProc < Proc
|
5
|
-
attr_reader :name
|
6
|
-
|
7
|
-
def initialize(name)
|
8
|
-
@name = name
|
9
|
-
super
|
10
|
-
end
|
11
|
-
|
12
|
-
# create one from a given proc/lambda object
|
13
|
-
def self.create(name, block, lambda = false)
|
14
|
-
name = name.to_sym
|
15
|
-
# sorry for this ugly hack, is there a better way to lambdafy?
|
16
|
-
block = Module.new.send(:define_method, name.to_sym, &block) if lambda
|
17
|
-
|
18
|
-
new(name, &block)
|
19
|
-
end
|
20
|
-
|
21
|
-
# Proxy object to ease named proc initialization
|
22
|
-
module Proxy
|
23
|
-
Proc = BasicObject.new
|
24
|
-
def Proc.method_missing(name, &block) NamedProc.create(name, block) end
|
25
|
-
|
26
|
-
Lambda = BasicObject.new
|
27
|
-
def Lambda.method_missing(name, &block) NamedProc.create(name, block, true) end
|
28
|
-
end
|
29
|
-
|
30
|
-
# Mixing in low level method "links"
|
31
|
-
module Object
|
32
|
-
private
|
33
|
-
|
34
|
-
# create a proc with name if given
|
35
|
-
def proc
|
36
|
-
if block_given?
|
37
|
-
super
|
38
|
-
else
|
39
|
-
NamedProc::Proxy::Proc
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
# same for lambda
|
44
|
-
def lambda
|
45
|
-
if block_given?
|
46
|
-
super
|
47
|
-
else
|
48
|
-
NamedProc::Proxy::Lambda
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
::Object.send :include, NamedProc::Object
|
54
|
-
end
|
1
|
+
require_relative 'named_proc/version'
|
2
|
+
require_relative 'named_proc/implementation'
|
3
|
+
require_relative 'named_proc/core_ext'
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Class for a proc that's got a name
|
2
|
+
class NamedProc < Proc
|
3
|
+
attr_reader :name
|
4
|
+
|
5
|
+
def initialize(name)
|
6
|
+
@name = name
|
7
|
+
super()
|
8
|
+
end
|
9
|
+
|
10
|
+
def inspect
|
11
|
+
super.sub /^#<#{self.class}/, "#<#{self.class}[#{name}]"
|
12
|
+
end
|
13
|
+
|
14
|
+
# Create a named proc from a given proc/lambda object
|
15
|
+
def self.create(name, block, create_lambda = false)
|
16
|
+
if create_lambda
|
17
|
+
lambdafyer = Module.new
|
18
|
+
lambdafyer.singleton_class.send(:define_method, :lambdafy, &block)
|
19
|
+
block = lambdafyer.method(:lambdafy).to_proc
|
20
|
+
end
|
21
|
+
new(name.to_sym, &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Proxy object to ease named proc initialization
|
25
|
+
module Proxy
|
26
|
+
Proc = BasicObject.new
|
27
|
+
def Proc.method_missing(name, &block) NamedProc.create(name, block) end
|
28
|
+
|
29
|
+
Lambda = BasicObject.new
|
30
|
+
def Lambda.method_missing(name, &block) NamedProc.create(name, block, true) end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Mixing in low level method "links"
|
34
|
+
module Object
|
35
|
+
private
|
36
|
+
|
37
|
+
# Create a proc with name if given
|
38
|
+
def proc
|
39
|
+
if block_given?
|
40
|
+
super
|
41
|
+
else
|
42
|
+
NamedProc::Proxy::Proc
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Same for lambda
|
47
|
+
def lambda
|
48
|
+
if block_given?
|
49
|
+
super
|
50
|
+
else
|
51
|
+
NamedProc::Proxy::Lambda
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/named_proc.gemspec
CHANGED
@@ -1,18 +1,19 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
|
2
|
+
|
3
|
+
require File.expand_path('../lib/named_proc/version', __FILE__)
|
4
|
+
|
4
5
|
Gem::Specification.new do |s|
|
5
6
|
s.name = "named_proc"
|
6
|
-
s.version =
|
7
|
+
s.version = NamedProc::VERSION
|
7
8
|
s.authors = ["Jan Lelis"]
|
8
9
|
s.email = "mail@janlelis.de"
|
9
|
-
s.homepage = "https://
|
10
|
+
s.homepage = "https://github.com/janlelis/named_proc"
|
10
11
|
s.summary = "NamedProc: Like anonymous procs, but have a name."
|
11
12
|
s.description = "NamedProc: Like anonymous procs, but have a name. Example: lambda.codebrawl {} # creates an empty lambda with the name :codebrawl"
|
12
|
-
s.
|
13
|
-
s.
|
14
|
-
s.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
|
13
|
+
s.files = Dir.glob %w{named_proc.gemspec lib/named_proc.rb lib/named_proc/version.rb lib/named_proc/implementation.rb lib/named_proc/core_ext.rb spec/named_proc_spec.rb}
|
14
|
+
s.extra_rdoc_files = ["README.rdoc", "MIT-LICENSE.txt", "CHANGELOG.rdoc", ".travis.yml"]
|
15
15
|
s.license = 'MIT'
|
16
|
-
s.
|
17
|
-
s.add_development_dependency 'rspec
|
16
|
+
s.required_ruby_version = '>= 1.9.3'
|
17
|
+
s.add_development_dependency 'rspec', '~> 3.2'
|
18
|
+
s.add_development_dependency 'rake', '~> 10.4'
|
18
19
|
end
|
data/spec/named_proc_spec.rb
CHANGED
@@ -1,34 +1,33 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
require_relative '../lib/named_proc'
|
3
2
|
|
4
3
|
describe "proc" do
|
5
4
|
it "creates a new proc as usual when called with a block" do
|
6
5
|
a = proc{}
|
7
|
-
a.
|
8
|
-
a.
|
6
|
+
expect( a ).to be_instance_of Proc
|
7
|
+
expect( a ).not_to be_lambda
|
9
8
|
end
|
10
9
|
|
11
10
|
it "creates a named proc when a method gets called on it" do
|
12
11
|
a = proc.brawl{}
|
13
|
-
a.
|
14
|
-
a.
|
15
|
-
a.
|
16
|
-
a.name
|
12
|
+
expect( a ).to be_a Proc
|
13
|
+
expect( a ).to be_instance_of NamedProc
|
14
|
+
expect( a ).not_to be_lambda
|
15
|
+
expect( a.name ).to eq :brawl
|
17
16
|
end
|
18
17
|
end
|
19
18
|
|
20
19
|
describe "lambda" do
|
21
20
|
it "creates a new lambda as usual when called with a block" do
|
22
21
|
a = lambda{}
|
23
|
-
a.
|
24
|
-
a.
|
22
|
+
expect( a ).to be_instance_of Proc
|
23
|
+
expect( a ).to be_lambda
|
25
24
|
end
|
26
25
|
|
27
26
|
it "creates a named lambda when a method gets called on it" do
|
28
27
|
a = lambda.brawl{}
|
29
|
-
a.
|
30
|
-
a.
|
31
|
-
a.
|
32
|
-
a.name
|
28
|
+
expect( a ).to be_a Proc
|
29
|
+
expect( a ).to be_instance_of NamedProc
|
30
|
+
expect( a ).to be_lambda
|
31
|
+
expect( a.name ).to eq :brawl
|
33
32
|
end
|
34
33
|
end
|
metadata
CHANGED
@@ -1,75 +1,86 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: named_proc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
5
|
-
prerelease:
|
4
|
+
version: '1.1'
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Jan Lelis
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-03-20 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rspec
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version: '
|
19
|
+
version: '3.2'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
25
27
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
requirement:
|
28
|
-
none: false
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
29
30
|
requirements:
|
30
|
-
- -
|
31
|
+
- - "~>"
|
31
32
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
33
|
+
version: '10.4'
|
33
34
|
type: :development
|
34
35
|
prerelease: false
|
35
|
-
version_requirements:
|
36
|
-
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.4'
|
41
|
+
description: 'NamedProc: Like anonymous procs, but have a name. Example: lambda.codebrawl
|
37
42
|
{} # creates an empty lambda with the name :codebrawl'
|
38
43
|
email: mail@janlelis.de
|
39
44
|
executables: []
|
40
45
|
extensions: []
|
41
46
|
extra_rdoc_files:
|
42
47
|
- README.rdoc
|
43
|
-
- LICENSE.txt
|
48
|
+
- MIT-LICENSE.txt
|
49
|
+
- CHANGELOG.rdoc
|
50
|
+
- ".travis.yml"
|
44
51
|
files:
|
45
|
-
-
|
52
|
+
- ".travis.yml"
|
53
|
+
- CHANGELOG.rdoc
|
54
|
+
- MIT-LICENSE.txt
|
55
|
+
- README.rdoc
|
46
56
|
- lib/named_proc.rb
|
57
|
+
- lib/named_proc/core_ext.rb
|
58
|
+
- lib/named_proc/implementation.rb
|
59
|
+
- lib/named_proc/version.rb
|
60
|
+
- named_proc.gemspec
|
47
61
|
- spec/named_proc_spec.rb
|
48
|
-
|
49
|
-
- LICENSE.txt
|
50
|
-
homepage: https://gist.github.com/4b2f5fd0b45118e46d0f
|
62
|
+
homepage: https://github.com/janlelis/named_proc
|
51
63
|
licenses:
|
52
64
|
- MIT
|
65
|
+
metadata: {}
|
53
66
|
post_install_message:
|
54
67
|
rdoc_options: []
|
55
68
|
require_paths:
|
56
69
|
- lib
|
57
70
|
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
-
none: false
|
59
71
|
requirements:
|
60
|
-
- -
|
72
|
+
- - ">="
|
61
73
|
- !ruby/object:Gem::Version
|
62
|
-
version: 1.9.
|
74
|
+
version: 1.9.3
|
63
75
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
-
none: false
|
65
76
|
requirements:
|
66
|
-
- -
|
77
|
+
- - ">="
|
67
78
|
- !ruby/object:Gem::Version
|
68
79
|
version: '0'
|
69
80
|
requirements: []
|
70
81
|
rubyforge_project:
|
71
|
-
rubygems_version:
|
82
|
+
rubygems_version: 2.4.6
|
72
83
|
signing_key:
|
73
|
-
specification_version:
|
74
|
-
summary:
|
84
|
+
specification_version: 4
|
85
|
+
summary: 'NamedProc: Like anonymous procs, but have a name.'
|
75
86
|
test_files: []
|