its-it 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/README.md +36 -35
- data/lib/its-it.rb +0 -1
- data/lib/its-it/version.rb +1 -1
- data/spec/main_spec.rb +11 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01864b89d966bb4cd9bbeea11caa15072e77890f
|
4
|
+
data.tar.gz: 24f7526c94e6a0b4d103581647f18fbd42f32583
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fabb650c7deb98f7d2b5dcc62897c36a5eec8f27bca33265ec307a624f4b1266f2c25e42aff209382658452a471986895c5952465c5749df2c57f0323d4915c0
|
7
|
+
data.tar.gz: d58f503c0ac24465d3aa61bf8385082b958f0765aa533f49127abf004c71a5f55127d8be505c16a2747c38d823268e7fe6f6455ced79052b6c510a318e0c0de1
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -15,80 +15,80 @@ calls. This is handy for list comprehension and case statements.
|
|
15
15
|
`its` and `it` extend the Symbol#to_proc idiom to support chaining multiple
|
16
16
|
methods.
|
17
17
|
|
18
|
-
|
18
|
+
When performing a list comprehension ruby, you can use a block argument:
|
19
|
+
|
19
20
|
|
20
21
|
```ruby
|
21
|
-
users.map{|user| user.contact
|
22
|
+
users.map{ |user| user.contact }
|
22
23
|
```
|
23
24
|
|
24
|
-
|
25
|
+
Or, to avoid needing the block and and extra parameter, you can use the `Symbol#to_proc` shortcut:
|
25
26
|
|
26
27
|
```ruby
|
27
|
-
users.map
|
28
|
+
users.map &:contact
|
28
29
|
```
|
29
30
|
|
30
|
-
|
31
|
+
But if you want to chain several methods, such as:
|
31
32
|
|
32
33
|
```ruby
|
33
|
-
users.map
|
34
|
+
users.map{ |user| user.contact.last_name.capitalize }
|
34
35
|
```
|
35
36
|
|
36
|
-
|
37
|
+
The `Symbol#to_proc` shortcut doesn't help much. At best, if you're willing to accept intermediate arrays, you can do:
|
37
38
|
|
38
39
|
```ruby
|
39
|
-
users.map(
|
40
|
+
users.map(&:contact).map(&:last_name).map(&:capitalize)
|
40
41
|
```
|
41
42
|
|
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:
|
43
44
|
|
44
45
|
```ruby
|
45
|
-
users.
|
46
|
+
users.map &its.contact.last_name.capitalize
|
46
47
|
```
|
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
|
+
```
|
48
56
|
|
49
|
-
`it` is an alias for `its`, to use with methods that describe actions rather
|
50
|
-
than posessives. For example:
|
57
|
+
As a syntactic sugar, `it` is an alias for `its`, to use with methods that describe actions rather than posessives. For example:
|
51
58
|
|
52
59
|
```ruby
|
53
|
-
items.map
|
60
|
+
items.map &it.to_s.capitalize
|
54
61
|
```
|
55
62
|
|
63
|
+
### Hash comprehensions
|
64
|
+
|
56
65
|
When used with hash comprehensions, the `|key, val|` pair of arguments are presented to `its` as an array. E.g.
|
57
66
|
|
58
67
|
```ruby
|
59
|
-
{dogs: 1, cats: 2, goats:3}.select &
|
68
|
+
{dogs: 1, cats: 2, goats:3}.select &its[1].even? # => {cats: 2}
|
60
69
|
```
|
61
70
|
|
62
71
|
## Case statements
|
63
72
|
|
64
|
-
`its` and `it`
|
65
|
-
arbitrary methods, minimizing the need to create temporary variables
|
66
|
-
`if-elsif` constructs.
|
67
|
-
|
68
|
-
In pure Ruby, doing comparisons on computed values would be done something
|
69
|
-
like this:
|
73
|
+
`its` and `it` similarly extend Ruby's `case` mechanism to support testing
|
74
|
+
arbitrary methods, minimizing the need to create temporary variables. That is, instead of:
|
70
75
|
|
71
76
|
```ruby
|
72
77
|
maxlen = arrays.map(&size).max
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
else
|
78
|
-
"okay"
|
78
|
+
case
|
79
|
+
when maxlen > 10000 then "too big"
|
80
|
+
when maxlen < 10 then "too small"
|
81
|
+
else "okay"
|
79
82
|
end
|
80
83
|
```
|
81
84
|
|
82
|
-
|
85
|
+
You can use `it`:
|
83
86
|
|
84
87
|
```ruby
|
85
88
|
case arrays.map(&size).max
|
86
|
-
when it > 1000
|
87
|
-
|
88
|
-
|
89
|
-
"too small"
|
90
|
-
else
|
91
|
-
"okay"
|
89
|
+
when it > 1000 then "too big"
|
90
|
+
when it < 10 then "too small"
|
91
|
+
else "okay"
|
92
92
|
end
|
93
93
|
```
|
94
94
|
|
@@ -104,8 +104,8 @@ end
|
|
104
104
|
|
105
105
|
## Under the hood
|
106
106
|
|
107
|
-
The `ItsIt::It` class uses `method_missing` to capture and queue up all
|
108
|
-
methods and their arguments
|
107
|
+
The `it` method creates an instance of the `ItsIt::It` class, which uses `method_missing` to capture and queue up all
|
108
|
+
methods and their arguments except for `:to_proc` and `:===` (and
|
109
109
|
also excepting `:respond_to? :to_proc` and `:respond_to? :===`).
|
110
110
|
|
111
111
|
`:to_proc` returns a proc that will evaluate the method queue on a given
|
@@ -136,6 +136,7 @@ Tested on MRI ruby 1.9.3, 2.0.0 and 2.2.3
|
|
136
136
|
|
137
137
|
Release Notes
|
138
138
|
|
139
|
+
* 1.2.1 Don't leak all of ItsIt into main, just ItsIt::Kernel. Thanks to [klg](https://github.com/kjg)
|
139
140
|
* 1.2.0 Add support for Hash comprehensions; drop support for ruby 1.8.7
|
140
141
|
* 1.1.1 Remove dependency on BlankSlate
|
141
142
|
|
data/lib/its-it.rb
CHANGED
data/lib/its-it/version.rb
CHANGED
data/spec/main_spec.rb
ADDED
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.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ronen Barzel
|
@@ -125,6 +125,7 @@ files:
|
|
125
125
|
- lib/its-it/kernel.rb
|
126
126
|
- lib/its-it/version.rb
|
127
127
|
- spec/it_spec.rb
|
128
|
+
- spec/main_spec.rb
|
128
129
|
- spec/rspec_compatibility_spec.rb
|
129
130
|
- spec/spec_helper.rb
|
130
131
|
homepage: http://github.com/ronen/its-it
|
@@ -146,11 +147,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
146
147
|
version: 1.3.7
|
147
148
|
requirements: []
|
148
149
|
rubyforge_project:
|
149
|
-
rubygems_version: 2.
|
150
|
+
rubygems_version: 2.5.1
|
150
151
|
signing_key:
|
151
152
|
specification_version: 3
|
152
153
|
summary: Defines its() and it() method-chain proxies.
|
153
154
|
test_files:
|
154
155
|
- spec/it_spec.rb
|
156
|
+
- spec/main_spec.rb
|
155
157
|
- spec/rspec_compatibility_spec.rb
|
156
158
|
- spec/spec_helper.rb
|