proc_for_case_equality 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/CHANGELOG +1 -0
- data/LICENSE +21 -0
- data/Manifest +10 -0
- data/README.rdoc +52 -0
- data/Rakefile +27 -0
- data/lib/proc_for_case_equality.rb +12 -0
- data/lib/proc_for_case_equality/pfce.rb +4 -0
- data/proc_for_case_equality.gemspec +31 -0
- data/spec/pfce_spec.rb +8 -0
- data/spec/proc_for_case_equality_spec.rb +9 -0
- data/spec/spec_helper.rb +1 -0
- metadata +112 -0
- metadata.gz.sig +0 -0
data.tar.gz.sig
ADDED
Binary file
|
data/CHANGELOG
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
v0.1. First release
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2012 Maurizio De Santis <desantis.maurizio at gmail.com>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
= ProcForCaseEquality.new { puts 'procs in case statements are cool!' }
|
2
|
+
|
3
|
+
== Features
|
4
|
+
|
5
|
+
Simple yet powerful: it lets you use procs for case comparisons (see the example below).
|
6
|
+
|
7
|
+
== How
|
8
|
+
|
9
|
+
+case+ statements call the <tt>===</tt> method, so I wrote a +ProcForCaseEquality+ class
|
10
|
+
that inherits from +Proc+ and overrides <tt>===</tt>, letting the case statement call the proc
|
11
|
+
passing the value of the case as argument.
|
12
|
+
|
13
|
+
The source code is so simple that I can put it in full here:
|
14
|
+
|
15
|
+
class ProcForCaseEquality < Proc
|
16
|
+
def ===(*params)
|
17
|
+
self.call *params
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
5 LOCs :P
|
22
|
+
|
23
|
+
== Installation
|
24
|
+
|
25
|
+
<tt>gem install proc_for_case_equality</tt> *COMING*
|
26
|
+
|
27
|
+
== Usage / Examples
|
28
|
+
|
29
|
+
require 'proc_for_case_equality' # OR:
|
30
|
+
require 'proc_for_case_equality/pfce' # if you want PFCE constant to point to ProcForCaseEquality
|
31
|
+
|
32
|
+
# Define some procs
|
33
|
+
all_multiples_of_3 = ProcWithCaseEquality.new { |numbers| numbers.all? { |number| number.modulo(3).zero? } }
|
34
|
+
any_multiple_of_3 = ProcWithCaseEquality.new { |numbers| numbers.any? { |number| number.modulo(3).zero? } }
|
35
|
+
|
36
|
+
# Here we come
|
37
|
+
case [1, 2, 3]
|
38
|
+
when all_multiples_of_3
|
39
|
+
puts 'all numbers are multiples of 3'
|
40
|
+
when any_multiple_of_3
|
41
|
+
puts 'at least one number is multiple of 3'
|
42
|
+
else
|
43
|
+
puts 'no multiples of 3'
|
44
|
+
end
|
45
|
+
|
46
|
+
== Inspired by
|
47
|
+
|
48
|
+
{This article}[http://www.aimred.com/news/developers/2008/08/14/unlocking_the_power_of_case_equality_proc/]
|
49
|
+
|
50
|
+
== License
|
51
|
+
|
52
|
+
MIT (see {LICENSE}[https://github.com/ProGNOMmers/proc_for_case_equality/blob/master/LICENSE])
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'psych'
|
2
|
+
require 'echoe'
|
3
|
+
|
4
|
+
Echoe.new("proc_for_case_equality") do |p|
|
5
|
+
p.project = 'proc_for_case_equality'
|
6
|
+
p.author = "De Santis Maurizio"
|
7
|
+
p.email = 'desantis.maurizio@gmail.com'
|
8
|
+
p.description = "proc_for_case_equality - For using procs in case comparisons"
|
9
|
+
p.url = "https://github.com/ProGNOMmers/proc_for_case_equality"
|
10
|
+
p.summary = "It lets you use procs for case comparisons. Example:
|
11
|
+
all_multiples_of_3 = ProcForCaseEquality.new { |numbers| numbers.all? { |number| number.modulo(3).zero? } }
|
12
|
+
any_multiple_of_3 = ProcForCaseEquality.new { |numbers| numbers.any? { |number| number.modulo(3).zero? } }
|
13
|
+
case [1, 2, 3]
|
14
|
+
when all_multiples_of_3 then puts 'all numbers are multiples of 3'
|
15
|
+
when any_multiple_of_3 then puts 'at least one number is multiple of 3'
|
16
|
+
else puts 'no multiples of 3'
|
17
|
+
end"
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'IRB console'
|
21
|
+
task :irb do
|
22
|
+
$LOAD_PATH << "#{Dir.pwd}/lib"
|
23
|
+
require 'proc_for_case_equality/pfce'
|
24
|
+
require 'irb'
|
25
|
+
ARGV.clear
|
26
|
+
IRB.start
|
27
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# This class inherits from +Proc+ and overrides its +===+ method
|
2
|
+
# letting you use proc instances in +case+ statements (see the {file:README.rdoc README})
|
3
|
+
class ProcForCaseEquality < Proc
|
4
|
+
# It calls +Proc#call+ in order to get used together in
|
5
|
+
# the +case+ statement.
|
6
|
+
#
|
7
|
+
# @param *params Any object.
|
8
|
+
# @return the proc calling using its argument(s) as argument(s)
|
9
|
+
def ===(*params)
|
10
|
+
self.call *params
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "proc_for_case_equality"
|
5
|
+
s.version = "0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["De Santis Maurizio"]
|
9
|
+
s.cert_chain = ["/home/mau/.gem_keys/gem-public_cert.pem"]
|
10
|
+
s.date = "2012-01-20"
|
11
|
+
s.description = "proc_for_case_equality - For using procs in case comparisons"
|
12
|
+
s.email = "desantis.maurizio@gmail.com"
|
13
|
+
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README.rdoc", "lib/proc_for_case_equality.rb", "lib/proc_for_case_equality/pfce.rb"]
|
14
|
+
s.files = ["CHANGELOG", "LICENSE", "README.rdoc", "Rakefile", "lib/proc_for_case_equality.rb", "lib/proc_for_case_equality/pfce.rb", "spec/pfce_spec.rb", "spec/proc_for_case_equality_spec.rb", "spec/spec_helper.rb", "Manifest", "proc_for_case_equality.gemspec"]
|
15
|
+
s.homepage = "https://github.com/ProGNOMmers/proc_for_case_equality"
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Proc_for_case_equality", "--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = "proc_for_case_equality"
|
19
|
+
s.rubygems_version = "1.8.15"
|
20
|
+
s.signing_key = "/home/mau/.gem_keys/gem-private_key.pem"
|
21
|
+
s.summary = "It lets you use procs for case comparisons. Example: all_multiples_of_3 = ProcForCaseEquality.new { |numbers| numbers.all? { |number| number.modulo(3).zero? } } any_multiple_of_3 = ProcForCaseEquality.new { |numbers| numbers.any? { |number| number.modulo(3).zero? } } case [1, 2, 3] when all_multiples_of_3 then puts 'all numbers are multiples of 3' when any_multiple_of_3 then puts 'at least one number is multiple of 3' else puts 'no multiples of 3' end"
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
s.specification_version = 3
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
27
|
+
else
|
28
|
+
end
|
29
|
+
else
|
30
|
+
end
|
31
|
+
end
|
data/spec/pfce_spec.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require File.expand_path('../../lib/proc_for_case_equality', __FILE__)
|
3
|
+
|
4
|
+
describe ProcForCaseEquality, '#===' do
|
5
|
+
it 'should return the result of the proc' do
|
6
|
+
p = ProcForCaseEquality.new{ |i| i }
|
7
|
+
p.send(:===, 'foo').should == 'foo'
|
8
|
+
end
|
9
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rspec'
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: proc_for_case_equality
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- De Santis Maurizio
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain:
|
12
|
+
- ! '-----BEGIN CERTIFICATE-----
|
13
|
+
|
14
|
+
MIIDRDCCAiygAwIBAgIBADANBgkqhkiG9w0BAQUFADBIMRowGAYDVQQDDBFkZXNh
|
15
|
+
|
16
|
+
bnRpcy5tYXVyaXppbzEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYKCZImiZPy
|
17
|
+
|
18
|
+
LGQBGRYDY29tMB4XDTExMDYzMDExMjIyOVoXDTEyMDYyOTExMjIyOVowSDEaMBgG
|
19
|
+
|
20
|
+
A1UEAwwRZGVzYW50aXMubWF1cml6a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L2UkpVJU7b+7d
|
47
|
+
|
48
|
+
1qFkvs/eJXM+w3TkQ2vaW9KFXQkIewZs
|
49
|
+
|
50
|
+
-----END CERTIFICATE-----
|
51
|
+
|
52
|
+
'
|
53
|
+
date: 2012-01-20 00:00:00.000000000 Z
|
54
|
+
dependencies: []
|
55
|
+
description: proc_for_case_equality - For using procs in case comparisons
|
56
|
+
email: desantis.maurizio@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files:
|
60
|
+
- CHANGELOG
|
61
|
+
- LICENSE
|
62
|
+
- README.rdoc
|
63
|
+
- lib/proc_for_case_equality.rb
|
64
|
+
- lib/proc_for_case_equality/pfce.rb
|
65
|
+
files:
|
66
|
+
- CHANGELOG
|
67
|
+
- LICENSE
|
68
|
+
- README.rdoc
|
69
|
+
- Rakefile
|
70
|
+
- lib/proc_for_case_equality.rb
|
71
|
+
- lib/proc_for_case_equality/pfce.rb
|
72
|
+
- spec/pfce_spec.rb
|
73
|
+
- spec/proc_for_case_equality_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- Manifest
|
76
|
+
- proc_for_case_equality.gemspec
|
77
|
+
homepage: https://github.com/ProGNOMmers/proc_for_case_equality
|
78
|
+
licenses: []
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options:
|
81
|
+
- --line-numbers
|
82
|
+
- --inline-source
|
83
|
+
- --title
|
84
|
+
- Proc_for_case_equality
|
85
|
+
- --main
|
86
|
+
- README.rdoc
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '1.2'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project: proc_for_case_equality
|
103
|
+
rubygems_version: 1.8.15
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: ! 'It lets you use procs for case comparisons. Example: all_multiples_of_3
|
107
|
+
= ProcForCaseEquality.new { |numbers| numbers.all? { |number| number.modulo(3).zero?
|
108
|
+
} } any_multiple_of_3 = ProcForCaseEquality.new { |numbers| numbers.any? { |number|
|
109
|
+
number.modulo(3).zero? } } case [1, 2, 3] when all_multiples_of_3 then puts ''all
|
110
|
+
numbers are multiples of 3'' when any_multiple_of_3 then puts ''at least one number
|
111
|
+
is multiple of 3'' else puts ''no multiples of 3'' end'
|
112
|
+
test_files: []
|
metadata.gz.sig
ADDED
Binary file
|