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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 01864b89d966bb4cd9bbeea11caa15072e77890f
4
- data.tar.gz: 24f7526c94e6a0b4d103581647f18fbd42f32583
3
+ metadata.gz: 91594b559afe659b0b318b56656b24734ff4578a
4
+ data.tar.gz: c80b8e6f4603ba93bfdbabda28f39a46775b366c
5
5
  SHA512:
6
- metadata.gz: fabb650c7deb98f7d2b5dcc62897c36a5eec8f27bca33265ec307a624f4b1266f2c25e42aff209382658452a471986895c5952465c5749df2c57f0323d4915c0
7
- data.tar.gz: d58f503c0ac24465d3aa61bf8385082b958f0765aa533f49127abf004c71a5f55127d8be505c16a2747c38d823268e7fe6f6455ced79052b6c510a318e0c0de1
6
+ metadata.gz: f855655e03f0d87b226dc8f90311782c60e39389b6b1e2c9c599c471ca33800b08a1e5be480682a37edf9a8f014192ce24e01cea0c9d918c402b1e389b9d0778
7
+ data.tar.gz: 8eb73444f90f0e353f295468036927b8d4d4f6345c3e54b76bcd52bf831f164310dd5840bde5eebb9364b39ffed342ae5acd4b52af04e12448a786cd2a0d18db
@@ -1,7 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
- - 2.0.0
5
- - 2.2.3
4
+ - 2.1.9
5
+ - 2.2.5
6
+ - 2.3.1
6
7
 
7
8
  before_install: gem install bundler -v ">= 1.10.0" --conservative
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 an array. E.g.
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.0.0 and 2.2.3
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
 
@@ -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 < (defined?(BasicObject) ? BasicObject : Object)
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
- obj = obj.shift if obj.size == 1
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
@@ -1,3 +1,3 @@
1
1
  module ItsIt
2
- VERSION = "1.2.1"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -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
- it "should work with hashes (multiple args)" do
50
- expect({a: 1, b:2}.select &it[1] == 2).to eq({b:2})
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: its-it
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ronen Barzel