its-it 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -2
- data/README.md +6 -7
- data/lib/its-it/it.rb +14 -4
- data/lib/its-it/version.rb +1 -1
- data/spec/it_spec.rb +14 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91594b559afe659b0b318b56656b24734ff4578a
|
4
|
+
data.tar.gz: c80b8e6f4603ba93bfdbabda28f39a46775b366c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f855655e03f0d87b226dc8f90311782c60e39389b6b1e2c9c599c471ca33800b08a1e5be480682a37edf9a8f014192ce24e01cea0c9d918c402b1e389b9d0778
|
7
|
+
data.tar.gz: 8eb73444f90f0e353f295468036927b8d4d4f6345c3e54b76bcd52bf831f164310dd5840bde5eebb9364b39ffed342ae5acd4b52af04e12448a786cd2a0d18db
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -62,9 +62,11 @@ items.map &it.to_s.capitalize
|
|
62
62
|
|
63
63
|
### Hash comprehensions
|
64
64
|
|
65
|
-
When used with hash comprehensions, the `|key, val|` pair of arguments are presented to `its` as
|
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
66
|
|
67
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}
|
68
70
|
{dogs: 1, cats: 2, goats:3}.select &its[1].even? # => {cats: 2}
|
69
71
|
```
|
70
72
|
|
@@ -128,7 +130,7 @@ gem "its-it"
|
|
128
130
|
|
129
131
|
## Compatibility
|
130
132
|
|
131
|
-
Tested on MRI ruby 1.9.3, 2.
|
133
|
+
Tested on MRI ruby 1.9.3, 2.1.9, 2.2.5, and 2.3.1
|
132
134
|
|
133
135
|
(MRI ruby 1.8.7 was supported up through version 1.1.1)
|
134
136
|
|
@@ -136,14 +138,11 @@ Tested on MRI ruby 1.9.3, 2.0.0 and 2.2.3
|
|
136
138
|
|
137
139
|
Release Notes
|
138
140
|
|
141
|
+
* 1.3.0 Add `#key` and `#value` for Hash comprehensions; plus minor internal cleanup.
|
139
142
|
* 1.2.1 Don't leak all of ItsIt into main, just ItsIt::Kernel. Thanks to [klg](https://github.com/kjg)
|
140
143
|
* 1.2.0 Add support for Hash comprehensions; drop support for ruby 1.8.7
|
141
144
|
* 1.1.1 Remove dependency on BlankSlate
|
142
145
|
|
143
146
|
This gem is orignally based on Jay Philips'
|
144
|
-
[methodphitamine](https://github.com/jicksta/methodphitamine) gem. It has been
|
145
|
-
updated to be compatible with ruby 1.9 and gemspec, added case statement
|
146
|
-
support, renamed its-it, and installed on [rubygems.org](http://rubygems.org).
|
147
|
-
Unlike methodphitamine, this gem includes only `its` and `it`, not the
|
148
|
-
"maybe" monad.
|
147
|
+
[methodphitamine](https://github.com/jicksta/methodphitamine) gem. It has been made available on [rubygems.org](http://rubygems.org), and updated over the years.
|
149
148
|
|
data/lib/its-it/it.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
#require 'blankslate' unless defined? BasicObject
|
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
|
@@ -10,7 +8,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
|
14
12
|
|
15
13
|
instance_methods.map(&:to_s).each do |method|
|
16
14
|
undef_method method unless method.start_with? "__"
|
@@ -25,9 +23,21 @@ module ItsIt
|
|
25
23
|
self
|
26
24
|
end
|
27
25
|
|
26
|
+
module KeyValAccessors
|
27
|
+
def key
|
28
|
+
self[0]
|
29
|
+
end
|
30
|
+
def value
|
31
|
+
self[1]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
28
35
|
def to_proc
|
29
36
|
Kernel.send :proc do |*obj|
|
30
|
-
|
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
|
40
|
+
end
|
31
41
|
@queue.inject(obj) { |chain,(method, args,block)| chain.send(method, *args, &block) }
|
32
42
|
end
|
33
43
|
end
|
data/lib/its-it/version.rb
CHANGED
data/spec/it_spec.rb
CHANGED
@@ -29,7 +29,7 @@ describe ItsIt::It do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should chain methods" do
|
32
|
-
expect(it.reverse.swapcase.succ).to eq("TSET A SI SIHu")
|
32
|
+
expect((it.reverse.swapcase.succ).to_proc.call(TestString)).to eq("TSET A SI SIHu")
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should respond to to_proc()" do
|
@@ -46,8 +46,19 @@ describe ItsIt::It do
|
|
46
46
|
expect((it < 1) === 2).to be_falsey
|
47
47
|
end
|
48
48
|
|
49
|
-
|
50
|
-
|
49
|
+
context "hash comprehension" do
|
50
|
+
it "presents `key` accessor" do
|
51
|
+
expect({a: 1, b:2}.select &it.key == :b).to eq({b:2})
|
52
|
+
end
|
53
|
+
|
54
|
+
it "presents `value` accessor" do
|
55
|
+
expect({a: 1, b:2}.select &it.value == 2).to eq({b:2})
|
56
|
+
end
|
57
|
+
|
58
|
+
it "presents as an array" do
|
59
|
+
expect({a: 1, b:2}.select &it[1] == 2).to eq({b:2})
|
60
|
+
end
|
61
|
+
|
51
62
|
end
|
52
63
|
|
53
64
|
|