its-it 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0940411f44c31b8869ba17e8efa1505c63c01c97
4
- data.tar.gz: 3a0b78717605b167e8017b4d9108f6cb08a3b680
3
+ metadata.gz: 01864b89d966bb4cd9bbeea11caa15072e77890f
4
+ data.tar.gz: 24f7526c94e6a0b4d103581647f18fbd42f32583
5
5
  SHA512:
6
- metadata.gz: f0a011722987ec5ac5a566c23e54c869a9faadec3a036cf3f207e9b92a32078527a25a06409668941706e490dfdfa011a5d2d8111603fe385bde91b7e2b3a4d0
7
- data.tar.gz: a386fe0ed1440033d01c5a794ff447892225bcdf98e610c66d14a7324085e42458741809107f7d00c6b7a1d6bd116363c11a1c94c9d483bbd454767f6639501d
6
+ metadata.gz: fabb650c7deb98f7d2b5dcc62897c36a5eec8f27bca33265ec307a624f4b1266f2c25e42aff209382658452a471986895c5952465c5749df2c57f0323d4915c0
7
+ data.tar.gz: d58f503c0ac24465d3aa61bf8385082b958f0765aa533f49127abf004c71a5f55127d8be505c16a2747c38d823268e7fe6f6455ced79052b6c510a318e0c0de1
data/.travis.yml CHANGED
@@ -3,3 +3,5 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
5
  - 2.2.3
6
+
7
+ before_install: gem install bundler -v ">= 1.10.0" --conservative
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
- The pure Ruby way to chain methods when iterating through a list would be:
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}.map{|contact| contact.last_name}.map{|name| name.capitalize}
22
+ users.map{ |user| user.contact }
22
23
  ```
23
24
 
24
- Using `Symbol#to_proc`, this becomes simpler (at the cost of generating intermediate arrays):
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(&:contact).map(&:last_name).map(&:capitalize)
28
+ users.map &:contact
28
29
  ```
29
30
 
30
- And using `its`, this becomes becomes simpler still:
31
+ But if you want to chain several methods, such as:
31
32
 
32
33
  ```ruby
33
- users.map(&its.contact.last_name.capitalize)
34
+ users.map{ |user| user.contact.last_name.capitalize }
34
35
  ```
35
36
 
36
- Note that `its` captures arguments and blocks, allowing you to do things like
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(&its.contact.last_name[0,3].capitalize)
40
+ users.map(&:contact).map(&:last_name).map(&:capitalize)
40
41
  ```
41
42
 
42
- or
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.select(&its.addresses.any? { |address| airline.flies_to address.city })
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(&it.to_s.capitalize)
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 &it[1].even? # => {cats: 2}
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` likewise extend Ruby's `case` statement to support testing
65
- arbitrary methods, minimizing the need to create temporary variables and use
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
- if maxlen > 10000
74
- "too big"
75
- elsif maxlen < 10
76
- "too small"
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
- But using `it` this becomes:
85
+ You can use `it`:
83
86
 
84
87
  ```ruby
85
88
  case arrays.map(&size).max
86
- when it > 1000
87
- "too big"
88
- when it < 10
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, with the exception of `:to_proc` and `:===` (and
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
@@ -4,5 +4,4 @@ require 'its-it/it'
4
4
  require 'its-it/kernel'
5
5
  require 'its-it/version'
6
6
 
7
- include ItsIt
8
7
  include ItsIt::Kernel
@@ -1,3 +1,3 @@
1
1
  module ItsIt
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
data/spec/main_spec.rb ADDED
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe Object do
4
+ it "doesn't get ItsIt's version" do
5
+ expect(defined?(::VERSION)).to be_nil
6
+ end
7
+
8
+ it "does't get the It class" do
9
+ expect(defined?(::It)).to be_nil
10
+ end
11
+ end
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.0
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.4.5.1
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