its-it 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/README.md +151 -0
- data/Rakefile +3 -26
- data/its-it.gemspec +4 -4
- data/lib/its-it.rb +0 -1
- data/lib/its-it/it.rb +25 -19
- data/lib/its-it/version.rb +1 -1
- data/spec/it_spec.rb +69 -53
- data/spec/main_spec.rb +11 -0
- data/spec/rspec_compatibility_spec.rb +6 -8
- data/spec/spec_helper.rb +3 -1
- metadata +106 -98
- data/README.rdoc +0 -107
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f1f6a4b053ef34ba91f453a9ca02fb417c272ff809388495a7305ffa0df48d32
|
4
|
+
data.tar.gz: 494832c699a13a34401757bc915e62eaf94e3c147d29e8fe5517448b4696e8e2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e40f49a07ecb80ea873b3bf78d415bd96b357f6e3c1f478f3f306adfda1b326c71a99fd07874fb34d2871a2a50309f43b4efe22c99de1e3ffb9126a751e6a18
|
7
|
+
data.tar.gz: 9d7889759739aa30597012fbb23aac386f404df0ed9f256f518fc9f3c2f8347c9964c8bcb152924e55911318b645a8ce6cfcb9def0e2767017f420bab1ab1c76
|
data/.rspec
CHANGED
data/.travis.yml
ADDED
data/README.md
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
# its-it
|
2
|
+
|
3
|
+
|
4
|
+
## Overview
|
5
|
+
|
6
|
+
This gem defines kernel methods `its` and `it` that queue and defer method
|
7
|
+
calls. This is handy for list comprehension and case statements.
|
8
|
+
|
9
|
+
[![Gem Version](https://badge.fury.io/rb/its-it.png)](http://badge.fury.io/rb/its-it)
|
10
|
+
[![Build Status](https://secure.travis-ci.org/ronen/its-it.png)](http://travis-ci.org/ronen/its-it)
|
11
|
+
[![Dependency Status](https://gemnasium.com/ronen/its-it.png)](https://gemnasium.com/ronen/its-it)
|
12
|
+
|
13
|
+
## List Comprehension
|
14
|
+
|
15
|
+
`its` and `it` extend the Symbol#to_proc idiom to support chaining multiple
|
16
|
+
methods.
|
17
|
+
|
18
|
+
When performing a list comprehension ruby, you can use a block argument:
|
19
|
+
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
users.map{ |user| user.contact }
|
23
|
+
```
|
24
|
+
|
25
|
+
Or, to avoid needing the block and and extra parameter, you can use the `Symbol#to_proc` shortcut:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
users.map &:contact
|
29
|
+
```
|
30
|
+
|
31
|
+
But if you want to chain several methods, such as:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
users.map{ |user| user.contact.last_name.capitalize }
|
35
|
+
```
|
36
|
+
|
37
|
+
The `Symbol#to_proc` shortcut doesn't help much. At best, if you're willing to accept intermediate arrays, you can do:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
users.map(&:contact).map(&:last_name).map(&:capitalize)
|
41
|
+
```
|
42
|
+
|
43
|
+
To improve the situation, this gem provides a Kernel method `its`, which lets you get the same shortcut advantages as `Symbol#to_proc` but supports chaining:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
users.map &its.contact.last_name.capitalize
|
47
|
+
```
|
48
|
+
|
49
|
+
Also, `its` supports arguments and blocks, allowing you to do things like
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
users.map &its.contact.last_name[0,3].capitalize
|
53
|
+
users.select &its.contact.last_name.length > 10
|
54
|
+
users.select(&its.addresses.any? { |address| airline.flies_to address.city })
|
55
|
+
```
|
56
|
+
|
57
|
+
As a syntactic sugar, `it` is an alias for `its`, to use with methods that describe actions rather than posessives. For example:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
items.map &it.to_s.capitalize
|
61
|
+
```
|
62
|
+
|
63
|
+
### Hash comprehensions
|
64
|
+
|
65
|
+
When used with hash comprehensions, the `|key, val|` pair of arguments are presented to `its` as a tuple that can be accessed array-like via `[0]` or `[1]` and/or struct-like via `#key` and `#value` methods. E.g.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
{dogs: 1, cats: 2, goats:3}.select &its.key =~ /^c/ # => {cats: 2}
|
69
|
+
{dogs: 1, cats: 2, goats:3}.select &its.value.even? # => {cats: 2}
|
70
|
+
{dogs: 1, cats: 2, goats:3}.select &its[1].even? # => {cats: 2}
|
71
|
+
```
|
72
|
+
|
73
|
+
## Case statements
|
74
|
+
|
75
|
+
`its` and `it` similarly extend Ruby's `case` mechanism to support testing
|
76
|
+
arbitrary methods, minimizing the need to create temporary variables. That is, instead of:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
maxlen = arrays.map(&size).max
|
80
|
+
case
|
81
|
+
when maxlen > 10000 then "too big"
|
82
|
+
when maxlen < 10 then "too small"
|
83
|
+
else "okay"
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
You can use `it`:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
case arrays.map(&size).max
|
91
|
+
when it > 1000 then "too big"
|
92
|
+
when it < 10 then "too small"
|
93
|
+
else "okay"
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
97
|
+
Of course method chanining can be used here too:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
case users.first
|
101
|
+
when its.name == "Gimme Cookie" then ...
|
102
|
+
when its.name.length > 10 then ...
|
103
|
+
else ...
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
## Under the hood
|
108
|
+
|
109
|
+
The `it` method creates an instance of the `ItsIt::It` class, which uses `method_missing` to capture and queue up all
|
110
|
+
methods and their arguments except for `:to_proc` and `:===` (and
|
111
|
+
also excepting `:respond_to? :to_proc` and `:respond_to? :===`).
|
112
|
+
|
113
|
+
`:to_proc` returns a proc that will evaluate the method queue on a given
|
114
|
+
argument. `:===` takes an argument and evaluates that proc, returning the
|
115
|
+
result.
|
116
|
+
|
117
|
+
## Installation
|
118
|
+
|
119
|
+
Install as usual from http://rubygems.org via
|
120
|
+
|
121
|
+
```bash
|
122
|
+
$ gem install "its-it"
|
123
|
+
```
|
124
|
+
|
125
|
+
or in a Gemfile
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
gem "its-it"
|
129
|
+
```
|
130
|
+
|
131
|
+
## Compatibility
|
132
|
+
|
133
|
+
Tested on MRI ruby 2.7.1
|
134
|
+
|
135
|
+
(MRI ruby 1.9.3, 2.1.9, 2.2.5, and 2.3.1 were supported up through version 1.3.0)
|
136
|
+
|
137
|
+
(MRI ruby 1.8.7 was supported up through version 1.1.1)
|
138
|
+
|
139
|
+
## History
|
140
|
+
|
141
|
+
Release Notes
|
142
|
+
|
143
|
+
* 2.0.0 Switch to Ruby 2.7 keyword syntax. No other breaking changes.
|
144
|
+
* 1.3.0 Add `#key` and `#value` for Hash comprehensions; plus minor internal cleanup.
|
145
|
+
* 1.2.1 Don't leak all of ItsIt into main, just ItsIt::Kernel. Thanks to [klg](https://github.com/kjg)
|
146
|
+
* 1.2.0 Add support for Hash comprehensions; drop support for ruby 1.8.7
|
147
|
+
* 1.1.1 Remove dependency on BlankSlate
|
148
|
+
|
149
|
+
This gem is orignally based on Jay Philips'
|
150
|
+
[methodphitamine](https://github.com/jicksta/methodphitamine) gem. It has been made available on [rubygems.org](http://rubygems.org), and updated over the years.
|
151
|
+
|
data/Rakefile
CHANGED
@@ -1,43 +1,20 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
|
3
|
-
begin
|
4
|
-
require 'bundler'
|
5
|
-
rescue LoadError
|
6
|
-
$stderr.puts "You must install bundler - run `gem install bundler`"
|
7
|
-
end
|
8
|
-
|
9
|
-
begin
|
10
|
-
Bundler.setup
|
11
|
-
rescue Bundler::BundlerError => e
|
12
|
-
$stderr.puts e.message
|
13
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
14
|
-
exit e.status_code
|
15
|
-
end
|
16
|
-
require 'rake'
|
17
|
-
|
18
|
-
require 'bueller'
|
19
|
-
Bueller::Tasks.new
|
2
|
+
require "bundler/gem_tasks"
|
20
3
|
|
21
4
|
require 'rspec/core/rake_task'
|
22
5
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
23
6
|
spec.rspec_opts = '-Ispec'
|
24
7
|
end
|
25
8
|
|
26
|
-
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
27
|
-
spec.rspec_opts = '-Ispec'
|
28
|
-
spec.rcov = true
|
29
|
-
end
|
30
|
-
|
31
9
|
task :default => :spec
|
32
10
|
|
33
|
-
require '
|
11
|
+
require 'rdoc/task'
|
34
12
|
Rake::RDocTask.new do |rdoc|
|
35
13
|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
36
14
|
|
37
15
|
rdoc.main = 'README.rdoc'
|
38
16
|
rdoc.rdoc_dir = 'rdoc'
|
39
|
-
rdoc.title = "
|
17
|
+
rdoc.title = "enumerable_hashify #{version}"
|
40
18
|
rdoc.rdoc_files.include('README*')
|
41
19
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
42
20
|
end
|
43
|
-
|
data/its-it.gemspec
CHANGED
@@ -23,9 +23,9 @@ for ruby 1.9 and gemspec compatibility and adding the case statement functionali
|
|
23
23
|
}
|
24
24
|
s.extra_rdoc_files = [
|
25
25
|
"LICENSE.txt",
|
26
|
-
"README.rdoc"
|
27
26
|
]
|
28
27
|
|
28
|
+
s.required_ruby_version = '~> 2.7'
|
29
29
|
s.required_rubygems_version = Gem::Requirement.new('>= 1.3.7')
|
30
30
|
s.rubygems_version = '1.3.7'
|
31
31
|
s.specification_version = 3
|
@@ -35,10 +35,10 @@ for ruby 1.9 and gemspec compatibility and adding the case statement functionali
|
|
35
35
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
36
36
|
s.require_paths = ['lib']
|
37
37
|
|
38
|
-
s.
|
39
|
-
s.add_development_dependency '
|
38
|
+
s.add_development_dependency 'rake'
|
39
|
+
s.add_development_dependency 'rdoc'
|
40
|
+
s.add_development_dependency 'rspec', "~> 3.0"
|
40
41
|
s.add_development_dependency 'bundler'
|
41
|
-
s.add_development_dependency 'bueller'
|
42
42
|
s.add_development_dependency 'simplecov'
|
43
43
|
s.add_development_dependency 'simplecov-gem-adapter'
|
44
44
|
|
data/lib/its-it.rb
CHANGED
data/lib/its-it/it.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'blankslate'
|
2
|
-
|
3
1
|
# This module contains an It class which queues any methods called on it
|
4
2
|
# and can be converted into a Proc. The Proc it generates executes the queued
|
5
3
|
# methods in the order received on the argument passed to the block, allowing
|
@@ -8,38 +6,46 @@ require 'blankslate'
|
|
8
6
|
# (1..10).select &it % 2 == 0
|
9
7
|
#
|
10
8
|
module ItsIt
|
11
|
-
|
9
|
+
|
12
10
|
# The class instantiated by the <code>it</code> and <code>its</code> kernel methods.
|
13
|
-
class It <
|
11
|
+
class It < BasicObject
|
12
|
+
|
13
|
+
instance_methods.map(&:to_s).each do |method|
|
14
|
+
undef_method method unless method.start_with? "__"
|
15
|
+
end
|
14
16
|
|
15
17
|
def initialize #:nodoc:
|
16
|
-
@
|
18
|
+
@queue = []
|
17
19
|
end
|
18
20
|
|
19
|
-
def method_missing(*args, &block)
|
20
|
-
@
|
21
|
+
def method_missing(method, *args, **kw, &block)
|
22
|
+
@queue << [method, args, kw, block] unless method == :respond_to? and [:to_proc, :===].include?(args.first.to_sym)
|
21
23
|
self
|
22
24
|
end
|
23
|
-
|
25
|
+
|
26
|
+
module KeyValAccessors
|
27
|
+
def key
|
28
|
+
self[0]
|
29
|
+
end
|
30
|
+
def value
|
31
|
+
self[1]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
24
35
|
def to_proc
|
25
|
-
|
26
|
-
|
27
|
-
|
36
|
+
Kernel.send :proc do |*obj|
|
37
|
+
case obj.size
|
38
|
+
when 1 then obj = obj.shift # array comprehension, gets one arg
|
39
|
+
when 2 then obj.extend(KeyValAccessors) # hash comprehension, gets two args
|
28
40
|
end
|
41
|
+
@queue.inject(obj) { |chain,(method, args, kw, block)| chain.send(method, *args, **kw, &block) }
|
29
42
|
end
|
30
43
|
end
|
31
44
|
|
32
45
|
def ===(obj)
|
33
|
-
to_proc.call(obj)
|
46
|
+
self.to_proc.call(obj)
|
34
47
|
end
|
35
48
|
|
36
|
-
# Used for testing. This method is hidden but can be revealed using
|
37
|
-
# ItsIt::It.reveal(:method_queue)
|
38
|
-
def method_queue
|
39
|
-
@methods
|
40
|
-
end
|
41
|
-
hide(:method_queue)
|
42
|
-
|
43
49
|
end
|
44
50
|
|
45
51
|
end
|
data/lib/its-it/version.rb
CHANGED
data/spec/it_spec.rb
CHANGED
@@ -1,75 +1,91 @@
|
|
1
1
|
require File.dirname(__FILE__) + "/spec_helper"
|
2
2
|
|
3
|
-
describe
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should
|
32
|
-
|
33
|
-
blocks = @it.method_queue.map { |x| x.last }
|
34
|
-
blocks.size.should == 4
|
35
|
-
blocks.each do |block|
|
36
|
-
block.should be_kind_of(Proc)
|
37
|
-
end
|
3
|
+
describe ItsIt::It do
|
4
|
+
|
5
|
+
TestString = "This is a test"
|
6
|
+
|
7
|
+
it "should start with identity via to_proc" do
|
8
|
+
expect(it.to_proc.call(TestString)).to eq(TestString)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should start with identity via ===" do
|
12
|
+
expect(it === TestString).to eq(TestString)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should work with a simple method via to_proc" do
|
16
|
+
expect((it.length).to_proc.call(TestString)).to eq(TestString.length)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should work with a simple method using ===" do
|
20
|
+
expect((it.length) === TestString).to eq(TestString.length)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should work with arguments" do
|
24
|
+
expect((it.sub(/test/, 'kumquat')).call(TestString)).to eq('This is a kumquat')
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should work with keyword arguments" do
|
28
|
+
expect((it.lines(chomp:true)).call(TestString + "\n")).to eq([TestString])
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should work with a block" do
|
32
|
+
expect((it.sub(/test/) {"balloon"}).to_proc.call(TestString)).to eq('This is a balloon')
|
38
33
|
end
|
39
|
-
|
40
|
-
it "should
|
41
|
-
|
42
|
-
queue = @it.method_queue
|
43
|
-
queue.size.should == 26
|
44
|
-
queue.map { |x| x.first.first.to_s }.should == ('a'..'z').to_a
|
34
|
+
|
35
|
+
it "should chain methods" do
|
36
|
+
expect((it.reverse.swapcase.succ).to_proc.call(TestString)).to eq("TSET A SI SIHu")
|
45
37
|
end
|
46
|
-
|
38
|
+
|
47
39
|
it "should respond to to_proc()" do
|
48
|
-
|
40
|
+
expect(it).to respond_to(:to_proc)
|
49
41
|
end
|
50
42
|
|
51
43
|
it "should respond to ===" do
|
52
|
-
|
44
|
+
expect(it).to respond_to(:===)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should work with numbers" do
|
48
|
+
expect((it < 1) === 0).to be_truthy
|
49
|
+
expect((it < 1) === 1).to be_falsey
|
50
|
+
expect((it < 1) === 2).to be_falsey
|
51
|
+
end
|
52
|
+
|
53
|
+
context "hash comprehension" do
|
54
|
+
it "presents `key` accessor" do
|
55
|
+
expect({a: 1, b:2}.select(&(it.key == :b))).to eq({b:2})
|
56
|
+
end
|
57
|
+
|
58
|
+
it "presents `value` accessor" do
|
59
|
+
expect({a: 1, b:2}.select(&(it.value == 2))).to eq({b:2})
|
60
|
+
end
|
61
|
+
|
62
|
+
it "presents as an array" do
|
63
|
+
expect({a: 1, b:2}.select(&(it[1] == 2))).to eq({b:2})
|
64
|
+
end
|
65
|
+
|
53
66
|
end
|
54
67
|
|
68
|
+
|
55
69
|
it "should work in a case statement" do
|
56
70
|
[0,1,2].each do |i|
|
57
71
|
case i
|
58
|
-
when it < 1 then i.
|
59
|
-
when it == 1 then i.
|
60
|
-
else i.
|
72
|
+
when it < 1 then expect(i).to eq(0)
|
73
|
+
when it == 1 then expect(i).to eq(1)
|
74
|
+
else expect(i).to eq(2)
|
61
75
|
end
|
62
76
|
end
|
63
77
|
end
|
64
|
-
|
78
|
+
|
65
79
|
it "should not queue the method respond_to? when given :to_proc as an arg" do
|
66
|
-
|
67
|
-
@it.method_queue.should be_empty
|
80
|
+
expect(it.respond_to? :to_proc).to be_true
|
68
81
|
end
|
69
82
|
|
70
83
|
it "should not queue the method respond_to? when given :=== as an arg" do
|
71
|
-
|
72
|
-
|
84
|
+
expect(it.respond_to? :===).to be_true
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should queue the method respond_to? when given generic arg" do
|
88
|
+
expect((it.respond_to? :size).to_proc.call(TestString)).to be_truthy
|
73
89
|
end
|
74
|
-
|
90
|
+
|
75
91
|
end
|
data/spec/main_spec.rb
ADDED
@@ -1,20 +1,18 @@
|
|
1
1
|
require File.dirname(__FILE__) + "/spec_helper"
|
2
2
|
|
3
|
-
describe "
|
3
|
+
describe "RSpec compatibility" do
|
4
4
|
|
5
5
|
# Surprisingly, RSpec's it() method isn't even defined within the context
|
6
6
|
# of each expectation block. Man, that's some crazy voodoo.
|
7
7
|
|
8
8
|
it "should make available the it and its methods" do
|
9
|
-
|
10
|
-
|
11
|
-
method(:it).should == method(:its) # Ensure it's not RSpec's it() method
|
12
|
-
lambda do
|
9
|
+
expect(method(:it)).to eq method(:its) # Ensure it's not RSpec's it() method
|
10
|
+
expect {
|
13
11
|
it.should be_kind_of(ItsIt::It)
|
14
|
-
|
12
|
+
}.to_not raise_error
|
15
13
|
end
|
16
14
|
|
17
|
-
it "should work with RSpec's
|
18
|
-
[1,2,3].each &it.should(be_kind_of(
|
15
|
+
it "should work with RSpec's old :should syntax" do
|
16
|
+
[1,2,3].each &it.should(be_kind_of(Integer))
|
19
17
|
end
|
20
18
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,108 +1,123 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: its-it
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
version: 1.1.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
6
5
|
platform: ruby
|
7
|
-
authors:
|
6
|
+
authors:
|
8
7
|
- Ronen Barzel
|
9
|
-
autorequire:
|
8
|
+
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
-
none: false
|
20
|
-
requirements:
|
11
|
+
date: 2011-04-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
21
17
|
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version:
|
24
|
-
type: :
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
25
21
|
prerelease: false
|
26
|
-
version_requirements:
|
27
|
-
|
28
|
-
name: rspec
|
29
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
-
none: false
|
31
|
-
requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
32
24
|
- - ">="
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version:
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rdoc
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
35
34
|
type: :development
|
36
35
|
prerelease: false
|
37
|
-
version_requirements:
|
38
|
-
|
39
|
-
name: bundler
|
40
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
|
-
requirements:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
43
38
|
- - ">="
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version:
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
46
48
|
type: :development
|
47
49
|
prerelease: false
|
48
|
-
version_requirements:
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
54
59
|
- - ">="
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
version:
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
57
62
|
type: :development
|
58
63
|
prerelease: false
|
59
|
-
version_requirements:
|
60
|
-
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
61
70
|
name: simplecov
|
62
|
-
requirement:
|
63
|
-
|
64
|
-
requirements:
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
65
73
|
- - ">="
|
66
|
-
- !ruby/object:Gem::Version
|
67
|
-
version:
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
68
76
|
type: :development
|
69
77
|
prerelease: false
|
70
|
-
version_requirements:
|
71
|
-
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
72
84
|
name: simplecov-gem-adapter
|
73
|
-
requirement:
|
74
|
-
|
75
|
-
requirements:
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
76
87
|
- - ">="
|
77
|
-
- !ruby/object:Gem::Version
|
78
|
-
version:
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
79
90
|
type: :development
|
80
91
|
prerelease: false
|
81
|
-
version_requirements:
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: |2
|
98
|
+
|
99
|
+
This gem defines the Kernel method "it" that queue and defer method calls.
|
100
|
+
This extends the Symbol#to_proc idiom to support chaining multiple methods.
|
101
|
+
For example, items.collect(&it.to_s.capitalize). This also allows
|
102
|
+
conditionals in case statements, such as: case ... when it > 3 then [etc.].
|
103
|
+
The method is also aliased as "its", for methods that describe possessives
|
104
|
+
rather than actions, such as items.collect(&its.name.capitalize)
|
105
|
+
|
106
|
+
[This gem is an extension of Jay Philips' "methodphitamine" gem, updated
|
107
|
+
for ruby 1.9 and gemspec compatibility and adding the case statement functionality.]
|
91
108
|
email: ronen@barzel.org
|
92
109
|
executables: []
|
93
|
-
|
94
110
|
extensions: []
|
95
|
-
|
96
|
-
extra_rdoc_files:
|
111
|
+
extra_rdoc_files:
|
97
112
|
- LICENSE.txt
|
98
|
-
|
99
|
-
|
100
|
-
- .
|
101
|
-
- .
|
102
|
-
- .
|
113
|
+
files:
|
114
|
+
- ".document"
|
115
|
+
- ".gitignore"
|
116
|
+
- ".rspec"
|
117
|
+
- ".travis.yml"
|
103
118
|
- Gemfile
|
104
119
|
- LICENSE.txt
|
105
|
-
- README.
|
120
|
+
- README.md
|
106
121
|
- Rakefile
|
107
122
|
- its-it.gemspec
|
108
123
|
- lib/its-it.rb
|
@@ -110,40 +125,33 @@ files:
|
|
110
125
|
- lib/its-it/kernel.rb
|
111
126
|
- lib/its-it/version.rb
|
112
127
|
- spec/it_spec.rb
|
128
|
+
- spec/main_spec.rb
|
113
129
|
- spec/rspec_compatibility_spec.rb
|
114
130
|
- spec/spec_helper.rb
|
115
|
-
has_rdoc: true
|
116
131
|
homepage: http://github.com/ronen/its-it
|
117
132
|
licenses: []
|
118
|
-
|
119
|
-
post_install_message:
|
133
|
+
metadata: {}
|
134
|
+
post_install_message:
|
120
135
|
rdoc_options: []
|
121
|
-
|
122
|
-
require_paths:
|
136
|
+
require_paths:
|
123
137
|
- lib
|
124
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
-
|
126
|
-
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - "~>"
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '2.7'
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
127
145
|
- - ">="
|
128
|
-
- !ruby/object:Gem::Version
|
129
|
-
hash: -664830529632804485
|
130
|
-
segments:
|
131
|
-
- 0
|
132
|
-
version: "0"
|
133
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
-
none: false
|
135
|
-
requirements:
|
136
|
-
- - ">="
|
137
|
-
- !ruby/object:Gem::Version
|
146
|
+
- !ruby/object:Gem::Version
|
138
147
|
version: 1.3.7
|
139
148
|
requirements: []
|
140
|
-
|
141
|
-
|
142
|
-
rubygems_version: 1.6.2
|
143
|
-
signing_key:
|
149
|
+
rubygems_version: 3.1.2
|
150
|
+
signing_key:
|
144
151
|
specification_version: 3
|
145
152
|
summary: Defines its() and it() method-chain proxies.
|
146
|
-
test_files:
|
153
|
+
test_files:
|
147
154
|
- spec/it_spec.rb
|
155
|
+
- spec/main_spec.rb
|
148
156
|
- spec/rspec_compatibility_spec.rb
|
149
157
|
- spec/spec_helper.rb
|
data/README.rdoc
DELETED
@@ -1,107 +0,0 @@
|
|
1
|
-
= its-it
|
2
|
-
|
3
|
-
== Overview
|
4
|
-
|
5
|
-
This gem defines kernel methods <code>its</code> and <code>it</code> that
|
6
|
-
queue and defer method calls. This is handy for list enumeration and case
|
7
|
-
statements.
|
8
|
-
|
9
|
-
== List Enumeration
|
10
|
-
|
11
|
-
<code>its</code> and <code>it</code> extend the Symbol#to_proc idiom to
|
12
|
-
support chaining multiple methods.
|
13
|
-
|
14
|
-
The pure Ruby way to chain methods when enumerating a list would be:
|
15
|
-
|
16
|
-
users.collect{|user| user.contact}.collect{|contact| contact.last_name}.collect{|name| name.capitalize}
|
17
|
-
|
18
|
-
Using <code>Symbol#to_proc</code>, this becomes simpler:
|
19
|
-
|
20
|
-
users.collect(&:contact).collect(&:last_name).collect(&:capitalize)
|
21
|
-
|
22
|
-
And using <code>its</code>, this becomes becomes simpler still:
|
23
|
-
|
24
|
-
users.collect(&its.contact.last_name.capitalize)
|
25
|
-
|
26
|
-
Note that <code>its</code> captures arguments and blocks, allowing constructs like this,
|
27
|
-
which will select users whose names include any non-hyphenated word that's more than 10
|
28
|
-
letters long:
|
29
|
-
users.select(&its.name.split(/ /).reject{|word| word =~ /-/}.collect(&:length).max > 10)
|
30
|
-
|
31
|
-
<code>it</code> is an alias for <code>its</code>, to use with methods that
|
32
|
-
describe actions rather than posessives. For example:
|
33
|
-
|
34
|
-
items.collect(&it.to_s.capitalize)
|
35
|
-
|
36
|
-
== Case statements
|
37
|
-
|
38
|
-
<code>its</code> and <code>it</code> likewise extend Ruby's
|
39
|
-
<code>code</code> statement to support testing arbitrary methods,
|
40
|
-
minimizing the need to create temporary variables and use
|
41
|
-
<code>if-elsif</code> constructs.
|
42
|
-
|
43
|
-
In pure Ruby, doing comparisons on computed values would be done something
|
44
|
-
like this:
|
45
|
-
|
46
|
-
maxlen = arrays.collect(&size).max
|
47
|
-
if maxlen > 10000
|
48
|
-
"too big"
|
49
|
-
elsif maxlen < 10
|
50
|
-
"too small"
|
51
|
-
else
|
52
|
-
"okay"
|
53
|
-
end
|
54
|
-
|
55
|
-
But using <code>it</code> this becomes:
|
56
|
-
|
57
|
-
case arrays.collect(&size).max
|
58
|
-
when it > 1000
|
59
|
-
"too big"
|
60
|
-
when it < 10
|
61
|
-
"too small"
|
62
|
-
else
|
63
|
-
"okay"
|
64
|
-
end
|
65
|
-
|
66
|
-
Of course method chanining can be used here too:
|
67
|
-
|
68
|
-
case users.first
|
69
|
-
when its.name == "Gimme Cookie" then ...
|
70
|
-
when its.name.length > 10 then ...
|
71
|
-
else ...
|
72
|
-
end
|
73
|
-
|
74
|
-
== Under the hood
|
75
|
-
|
76
|
-
The <code>ItsIt::It</code> class starts from <code>BlankSlate</code>, and
|
77
|
-
uses <code>method_missing</code> to capture and queue up all methods and
|
78
|
-
their arguments, with the exception of <code>:to_proc</code> and <code>:===</code>
|
79
|
-
(and also excepting <code>:respond_to? :to_proc</code> and
|
80
|
-
<code>:respond_to? :===</code>).
|
81
|
-
|
82
|
-
<code>:to_proc</code> returns a proc that will evaluate the method queue on
|
83
|
-
a given argument. <code>:===</code> takes an argument and evaluates that
|
84
|
-
proc, returning the result.
|
85
|
-
|
86
|
-
== Installation
|
87
|
-
|
88
|
-
Install from http://rubygems.org via
|
89
|
-
|
90
|
-
$ gem install "its-it"
|
91
|
-
|
92
|
-
or in a Gemfile
|
93
|
-
|
94
|
-
gem "its-it"
|
95
|
-
|
96
|
-
== Compatibility
|
97
|
-
|
98
|
-
Tested with <b>ruby 1.8.7</b> and <b>ruby 1.9.2</b>
|
99
|
-
|
100
|
-
== History
|
101
|
-
|
102
|
-
This gem is orignally based on Jay Philips'
|
103
|
-
methodphitamine[https://github.com/jicksta/methodphitamine] gem. It has
|
104
|
-
been updated to be compatible with ruby 1.9.2 and gemspec, renamed its-it,
|
105
|
-
and installed on rubygems.org[http://rubygems.org]. Unlike
|
106
|
-
methodphitamine, this gem includes only <code>its</code> and
|
107
|
-
<code>it</code>, not the "maybe" monad.
|