necklace 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +35 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +47 -0
- data/README.md +121 -0
- data/Rakefile +12 -0
- data/VERSION +1 -0
- data/lib/necklace.rb +5 -1
- data/necklace.gemspec +22 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/threading_arguments_spec.rb +57 -0
- metadata +70 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55f8bb4fe12f5436ceed69e294e00383353c72e4
|
4
|
+
data.tar.gz: 30c503d917f93039cbd2cfdefed3209c10163811
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb43fb6ab10775f0003e57910859615fa7d7e62b1075ac39f939b63d88cfcc75b7608197cc02dbe3291e2da57bc21d5a1f6e142cdcbe37e4d9bb72bc1e8b9e15
|
7
|
+
data.tar.gz: d38e77087e6ead1b1257cd0579e8f0dc8666ee73afdd4fd36f7a2ddff2210917dce1ac35c17b35ccd751417a26798059de2c700f86b924c2c06db3ff87a6999c
|
data/.gitignore
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
# Gemfile.lock
|
31
|
+
# .ruby-version
|
32
|
+
# .ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
necklace (0.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.0)
|
10
|
+
diff-lcs (1.2.5)
|
11
|
+
docile (1.1.5)
|
12
|
+
method_source (0.8.2)
|
13
|
+
multi_json (1.11.0)
|
14
|
+
pry (0.10.1)
|
15
|
+
coderay (~> 1.1.0)
|
16
|
+
method_source (~> 0.8.1)
|
17
|
+
slop (~> 3.4)
|
18
|
+
rake (10.4.2)
|
19
|
+
rspec (3.2.0)
|
20
|
+
rspec-core (~> 3.2.0)
|
21
|
+
rspec-expectations (~> 3.2.0)
|
22
|
+
rspec-mocks (~> 3.2.0)
|
23
|
+
rspec-core (3.2.2)
|
24
|
+
rspec-support (~> 3.2.0)
|
25
|
+
rspec-expectations (3.2.0)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.2.0)
|
28
|
+
rspec-mocks (3.2.1)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.2.0)
|
31
|
+
rspec-support (3.2.2)
|
32
|
+
simplecov (0.9.2)
|
33
|
+
docile (~> 1.1.0)
|
34
|
+
multi_json (~> 1.0)
|
35
|
+
simplecov-html (~> 0.9.0)
|
36
|
+
simplecov-html (0.9.0)
|
37
|
+
slop (3.6.0)
|
38
|
+
|
39
|
+
PLATFORMS
|
40
|
+
ruby
|
41
|
+
|
42
|
+
DEPENDENCIES
|
43
|
+
necklace!
|
44
|
+
pry
|
45
|
+
rake
|
46
|
+
rspec
|
47
|
+
simplecov
|
data/README.md
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
# Necklace
|
2
|
+
|
3
|
+
## What/Why
|
4
|
+
|
5
|
+
Ports the `->` macro from Clojure to make long chains of Enumerable more
|
6
|
+
readable
|
7
|
+
|
8
|
+
Usually when chaining method calls in ruby, the names of the methods are
|
9
|
+
descriptive and the chain is readable. (Note that this may violate the Law of
|
10
|
+
Demeter, but this is simply an example). One can imagine something like
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
food_processor.add(vinegar)
|
14
|
+
.add(garlic)
|
15
|
+
.add(pepper)
|
16
|
+
.start
|
17
|
+
.drizzle(oil)
|
18
|
+
```
|
19
|
+
|
20
|
+
Where each method returns `self` and the chain describes the actions.
|
21
|
+
|
22
|
+
However, with Enumerable, because the methods are more abstract, readability is
|
23
|
+
often sacrificed when chaining operations:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
[1, 3, 5, 7, 9].map do |n|
|
27
|
+
n + 1
|
28
|
+
end.map do |n|
|
29
|
+
n * 2
|
30
|
+
end.map |n|
|
31
|
+
n - 3
|
32
|
+
end.select |n|
|
33
|
+
n.even?
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Two possible solutions are to name only the inner methods:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
[1, 3, 5, 7, 9].map do |n|
|
41
|
+
increment(n)
|
42
|
+
end.map do |n|
|
43
|
+
double(n)
|
44
|
+
end.map |n|
|
45
|
+
subtract(n, 3)
|
46
|
+
end.select |n|
|
47
|
+
n.even?
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
or extract each transformation in a method and call them inside-out -
|
52
|
+
lisp-style:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
even(
|
56
|
+
subtract_three(
|
57
|
+
double(
|
58
|
+
incrememnt([1, 3, 5, 7, 9])
|
59
|
+
)
|
60
|
+
)
|
61
|
+
)
|
62
|
+
```
|
63
|
+
|
64
|
+
This last looks a lot like a lisp, and Clojure has a macro to make calls like
|
65
|
+
this easier to read:
|
66
|
+
|
67
|
+
```clojure
|
68
|
+
(-> [1, 3, 5, 7, 9]
|
69
|
+
increment
|
70
|
+
double
|
71
|
+
subtract_three
|
72
|
+
even
|
73
|
+
)
|
74
|
+
```
|
75
|
+
|
76
|
+
**Necklace** attempts to replicate at least some aspects of the Clojure
|
77
|
+
threading macros (`->`, `->>`) in ruby so that long chains of enumerable
|
78
|
+
transformations can be called without sacrificing readablility.
|
79
|
+
|
80
|
+
## Usage
|
81
|
+
|
82
|
+
simply `include Necklace` in a class
|
83
|
+
|
84
|
+
Then define methods that take each successive result as their argument.
|
85
|
+
|
86
|
+
Then call `through` with the enumerable as the first argument and an array of
|
87
|
+
symbols corresponding to the method names as the second argument.
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
through([1, 3, 5, 7, 9],
|
91
|
+
[:increment,
|
92
|
+
:double,
|
93
|
+
:subtract_three,
|
94
|
+
:even
|
95
|
+
]
|
96
|
+
)
|
97
|
+
```
|
98
|
+
|
99
|
+
## License
|
100
|
+
### MIT
|
101
|
+
|
102
|
+
```
|
103
|
+
Copyright (c) 2015 Stuart Terrett
|
104
|
+
|
105
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
106
|
+
this software and associated documentation files (the "Software"), to deal in
|
107
|
+
the Software without restriction, including without limitation the rights to
|
108
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
109
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
110
|
+
subject to the following conditions:
|
111
|
+
|
112
|
+
The above copyright notice and this permission notice shall be included in all
|
113
|
+
copies or substantial portions of the Software.
|
114
|
+
|
115
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
116
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
117
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
118
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
119
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
120
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
121
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
desc 'Default: run specs.'
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
desc 'Run specs'
|
9
|
+
RSpec::Core::RakeTask.new(:spec)
|
10
|
+
|
11
|
+
desc 'Generate code coverage'
|
12
|
+
RSpec::Core::RakeTask.new(:coverage)
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/necklace.rb
CHANGED
data/necklace.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'necklace'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2010-04-28'
|
5
|
+
s.summary = "Clojures -> macro for ruby"
|
6
|
+
s.description = "Implements Clojures threading macros for Ruby Enumerable methods"
|
7
|
+
s.authors = ["Stuart Terrett"]
|
8
|
+
s.email = 'shterrett@gmail.com'
|
9
|
+
s.files = ["lib/necklace.rb"]
|
10
|
+
s.homepage = 'http://github.com/shterrett/necklace'
|
11
|
+
s.license = 'MIT'
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
16
|
+
s.require_paths = ['lib']
|
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')
|
22
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
|
3
|
+
SimpleCov.start do
|
4
|
+
add_filter 'spec/'
|
5
|
+
end
|
6
|
+
|
7
|
+
# encoding: utf-8
|
8
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
9
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
10
|
+
|
11
|
+
Dir['./spec/shared/**/*.rb'].sort.each { |f| require f }
|
12
|
+
require_relative '../lib/necklace'
|
13
|
+
|
14
|
+
require 'rspec'
|
15
|
+
|
16
|
+
RSpec.configure do |config|
|
17
|
+
config.order = 'random'
|
18
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Threading enumerable as argument' do
|
4
|
+
it 'passes the given enumerable to the function named by the symbol in an array' do
|
5
|
+
class Test
|
6
|
+
include Necklace
|
7
|
+
|
8
|
+
def increment(numbers)
|
9
|
+
numbers.map { |n| n + 1 }
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_increment
|
13
|
+
through([1, 3, 5, 7, 9],
|
14
|
+
[:increment]
|
15
|
+
)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
test = Test.new
|
20
|
+
expect(test.test_increment).to eq([2, 4, 6, 8, 10])
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'passes the result of each method to the next from top to bottom' do
|
24
|
+
class Test
|
25
|
+
include Necklace
|
26
|
+
|
27
|
+
def increment(numbers)
|
28
|
+
numbers.map { |n| n + 1 }
|
29
|
+
end
|
30
|
+
|
31
|
+
def double(numbers)
|
32
|
+
numbers.map { |n| n * 2 }
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_increment_double
|
36
|
+
through([1, 3, 5, 7 , 9],
|
37
|
+
[:increment,
|
38
|
+
:double
|
39
|
+
]
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_double_increment
|
44
|
+
through([1, 3, 5, 7, 9],
|
45
|
+
[:double,
|
46
|
+
:increment
|
47
|
+
]
|
48
|
+
)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
test = Test.new
|
53
|
+
|
54
|
+
expect(test.test_increment_double).to eq([4, 8, 12, 16, 20])
|
55
|
+
expect(test.test_double_increment).to eq([3, 7, 11, 15, 19])
|
56
|
+
end
|
57
|
+
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.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stuart Terrett
|
@@ -9,14 +9,79 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2010-04-28 00:00:00.000000000 Z
|
12
|
-
dependencies:
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: simplecov
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
13
69
|
description: Implements Clojures threading macros for Ruby Enumerable methods
|
14
70
|
email: shterrett@gmail.com
|
15
71
|
executables: []
|
16
72
|
extensions: []
|
17
73
|
extra_rdoc_files: []
|
18
74
|
files:
|
75
|
+
- ".gitignore"
|
76
|
+
- Gemfile
|
77
|
+
- Gemfile.lock
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- VERSION
|
19
81
|
- lib/necklace.rb
|
82
|
+
- necklace.gemspec
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
- spec/threading_arguments_spec.rb
|
20
85
|
homepage: http://github.com/shterrett/necklace
|
21
86
|
licenses:
|
22
87
|
- MIT
|
@@ -41,4 +106,6 @@ rubygems_version: 2.2.2
|
|
41
106
|
signing_key:
|
42
107
|
specification_version: 4
|
43
108
|
summary: Clojures -> macro for ruby
|
44
|
-
test_files:
|
109
|
+
test_files:
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
- spec/threading_arguments_spec.rb
|