necklace 0.0.1 → 0.1.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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +36 -0
- data/VERSION +1 -1
- data/lib/necklace.rb +19 -1
- data/necklace.gemspec +5 -5
- data/spec/through_first_spec.rb +49 -0
- data/spec/through_last_spec.rb +49 -0
- data/spec/{threading_arguments_spec.rb → through_spec.rb} +28 -2
- metadata +23 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42d78b275452d45057290c0bb525c304e46428b7
|
4
|
+
data.tar.gz: 1388c97707c10185cc6bd724bf461a00ef9e44f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 09063d5fcad8a4d3095f1735a5c5777ba4a5a8ed4faf87ae65d3f532b250969a690c6f20cd2e4bd5ac2b5cd0d1a87bf2894cd44a88cebc16fd5e70cc9066fefa
|
7
|
+
data.tar.gz: 5a19b9271081505502783c4aa90d5afb25f04e3ef8f08768a8c7c612281156417540ec74d9b2b9b527f33d592b6cf10bce58d98b279de67b0ad62d675cc0e9fa
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -96,6 +96,42 @@ through([1, 3, 5, 7, 9],
|
|
96
96
|
)
|
97
97
|
```
|
98
98
|
|
99
|
+
For methods that have multiple arguments, both a `through_first` and
|
100
|
+
`through_last` method are provided. These thread the enumerable as the first or
|
101
|
+
last argument of the method calls respectively. Methods with arguments in
|
102
|
+
addition to the enumerable have the method and it's additional arguments
|
103
|
+
packaged in an array:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
through_first([1, 3, 5, 7, 9],
|
107
|
+
[[:add_to_each, 3]
|
108
|
+
:double
|
109
|
+
]
|
110
|
+
)
|
111
|
+
```
|
112
|
+
|
113
|
+
will call
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
add_to_each([1, 3, 5, 7, 9], 3)
|
117
|
+
```
|
118
|
+
|
119
|
+
and
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
through_last([1, 3, 5, 7, 9],
|
123
|
+
[[:add_to_each, 3]
|
124
|
+
:double
|
125
|
+
]
|
126
|
+
)
|
127
|
+
```
|
128
|
+
|
129
|
+
will call
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
add_to_each(3, [1, 3, 5, 7, 9])
|
133
|
+
```
|
134
|
+
|
99
135
|
## License
|
100
136
|
### MIT
|
101
137
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.1.1
|
data/lib/necklace.rb
CHANGED
@@ -1,7 +1,25 @@
|
|
1
1
|
module Necklace
|
2
|
+
ARG_INSERTION_ORDER = { first: 1, last: -1 }
|
3
|
+
|
2
4
|
def through(enumerable, methods)
|
5
|
+
through_last(enumerable, methods)
|
6
|
+
end
|
7
|
+
|
8
|
+
def through_first(enumerable, methods)
|
3
9
|
methods.reduce(enumerable) do |result, method|
|
4
|
-
|
10
|
+
_call(method, result, :first)
|
5
11
|
end
|
6
12
|
end
|
13
|
+
|
14
|
+
def through_last(enumerable, methods)
|
15
|
+
methods.reduce(enumerable) do |result, method|
|
16
|
+
_call(method, result, :last)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def _call(method, result, order)
|
21
|
+
method = [method].flatten
|
22
|
+
args = method.insert(ARG_INSERTION_ORDER[order], result)
|
23
|
+
send(*args)
|
24
|
+
end
|
7
25
|
end
|
data/necklace.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'necklace'
|
3
|
-
s.version = '0.
|
3
|
+
s.version = '0.1.1'
|
4
4
|
s.date = '2010-04-28'
|
5
5
|
s.summary = "Clojures -> macro for ruby"
|
6
6
|
s.description = "Implements Clojures threading macros for Ruby Enumerable methods"
|
@@ -15,8 +15,8 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
16
16
|
s.require_paths = ['lib']
|
17
17
|
|
18
|
-
s.add_development_dependency('rake')
|
19
|
-
s.add_development_dependency('rspec')
|
20
|
-
s.add_development_dependency('simplecov')
|
21
|
-
s.add_development_dependency('pry')
|
18
|
+
s.add_development_dependency('rake', '~> 10.4')
|
19
|
+
s.add_development_dependency('rspec', '~> 3.2')
|
20
|
+
s.add_development_dependency('simplecov', '~> 0.9')
|
21
|
+
s.add_development_dependency('pry', '~> 0.10')
|
22
22
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'threading enumerable as first argument through methods with multiple arguments' do
|
4
|
+
it 'puts the enumerable first in the argument list of a method that takes multiple arguments' do
|
5
|
+
class Test
|
6
|
+
include Necklace
|
7
|
+
|
8
|
+
def add_to_each(numbers, number)
|
9
|
+
numbers.map { |n| n + number }
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_add_to_each
|
13
|
+
through_first([1, 3, 5, 7, 9],
|
14
|
+
[[:add_to_each, 3]]
|
15
|
+
)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
test = Test.new
|
20
|
+
|
21
|
+
expect(test.test_add_to_each).to eq([4, 6, 8, 10, 12])
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'accepts a mix of methods which take multiple arguments and methods which take only one' do
|
25
|
+
class Test
|
26
|
+
include Necklace
|
27
|
+
|
28
|
+
def add_to_each(numbers, number)
|
29
|
+
numbers.map { |n| n + number }
|
30
|
+
end
|
31
|
+
|
32
|
+
def double(numbers)
|
33
|
+
numbers.map { |n| n * 2 }
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_add_to_each_double
|
37
|
+
through_first([1, 3, 5, 7, 9],
|
38
|
+
[[:add_to_each, 3],
|
39
|
+
:double
|
40
|
+
]
|
41
|
+
)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
test = Test.new
|
46
|
+
|
47
|
+
expect(test.test_add_to_each_double).to eq([8, 12, 16, 20, 24])
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Threading enumerable as the last argument through a function that takes multiple arguments' do
|
4
|
+
it 'uses the enumerable as the last argument of a method that takes multiple arguments' do
|
5
|
+
class Test
|
6
|
+
include Necklace
|
7
|
+
|
8
|
+
def add_to_each(number, numbers)
|
9
|
+
numbers.map { |n| n + number }
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_add_to_each
|
13
|
+
through_last([1, 3, 5, 7, 9],
|
14
|
+
[[:add_to_each, 3]]
|
15
|
+
)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
test = Test.new
|
20
|
+
|
21
|
+
expect(test.test_add_to_each).to eq([4, 6, 8, 10, 12])
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'mixes methods that require multiple arguments with those that take only one' do
|
25
|
+
class Test
|
26
|
+
include Necklace
|
27
|
+
|
28
|
+
def add_to_each(number, numbers)
|
29
|
+
numbers.map { |n| n + number }
|
30
|
+
end
|
31
|
+
|
32
|
+
def double(numbers)
|
33
|
+
numbers.map { |n| n * 2 }
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_double_add_to_each
|
37
|
+
through_last([1, 3, 5, 7, 9],
|
38
|
+
[[:add_to_each, 3],
|
39
|
+
:double
|
40
|
+
]
|
41
|
+
)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
test = Test.new
|
46
|
+
|
47
|
+
expect(test.test_double_add_to_each).to eq([8, 12, 16, 20, 24])
|
48
|
+
end
|
49
|
+
end
|
@@ -12,7 +12,7 @@ describe 'Threading enumerable as argument' do
|
|
12
12
|
def test_increment
|
13
13
|
through([1, 3, 5, 7, 9],
|
14
14
|
[:increment]
|
15
|
-
|
15
|
+
)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -37,7 +37,7 @@ describe 'Threading enumerable as argument' do
|
|
37
37
|
[:increment,
|
38
38
|
:double
|
39
39
|
]
|
40
|
-
|
40
|
+
)
|
41
41
|
end
|
42
42
|
|
43
43
|
def test_double_increment
|
@@ -54,4 +54,30 @@ describe 'Threading enumerable as argument' do
|
|
54
54
|
expect(test.test_increment_double).to eq([4, 8, 12, 16, 20])
|
55
55
|
expect(test.test_double_increment).to eq([3, 7, 11, 15, 19])
|
56
56
|
end
|
57
|
+
|
58
|
+
it 'handles data that has been reduced from an enumerable to a value' do
|
59
|
+
class Test
|
60
|
+
include Necklace
|
61
|
+
|
62
|
+
def sum(numbers)
|
63
|
+
numbers.reduce(&:+)
|
64
|
+
end
|
65
|
+
|
66
|
+
def double(number)
|
67
|
+
number * 2
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_sum_double
|
71
|
+
through([1, 2, 3, 4, 5],
|
72
|
+
[:sum,
|
73
|
+
:double
|
74
|
+
]
|
75
|
+
)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
test = Test.new
|
80
|
+
|
81
|
+
expect(test.test_sum_double).to eq 30
|
82
|
+
end
|
57
83
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: necklace
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stuart Terrett
|
@@ -14,58 +14,58 @@ dependencies:
|
|
14
14
|
name: rake
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '10.4'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '10.4'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '3.2'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '3.2'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: simplecov
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
47
|
+
version: '0.9'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
54
|
+
version: '0.9'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: pry
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
61
|
+
version: '0.10'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
68
|
+
version: '0.10'
|
69
69
|
description: Implements Clojures threading macros for Ruby Enumerable methods
|
70
70
|
email: shterrett@gmail.com
|
71
71
|
executables: []
|
@@ -81,7 +81,9 @@ files:
|
|
81
81
|
- lib/necklace.rb
|
82
82
|
- necklace.gemspec
|
83
83
|
- spec/spec_helper.rb
|
84
|
-
- spec/
|
84
|
+
- spec/through_first_spec.rb
|
85
|
+
- spec/through_last_spec.rb
|
86
|
+
- spec/through_spec.rb
|
85
87
|
homepage: http://github.com/shterrett/necklace
|
86
88
|
licenses:
|
87
89
|
- MIT
|
@@ -108,4 +110,6 @@ specification_version: 4
|
|
108
110
|
summary: Clojures -> macro for ruby
|
109
111
|
test_files:
|
110
112
|
- spec/spec_helper.rb
|
111
|
-
- spec/
|
113
|
+
- spec/through_first_spec.rb
|
114
|
+
- spec/through_last_spec.rb
|
115
|
+
- spec/through_spec.rb
|