pipe_envy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cc7f84cca8becdf9541994ec94d158ce3945cb38
4
+ data.tar.gz: 3f0b3de20726b92e2d9931a1421882bad6911607
5
+ SHA512:
6
+ metadata.gz: c274e5702a5c5e9d847f47814abf7227a2b57a808fd0d88aea67b4480d4207e6fa7f2ffea5568c0207cdbde05d4da0d60179f19993a01723a14374d2a78807cd
7
+ data.tar.gz: e803e74b35a6978856b4aaa52b848bcd860c8756fbbd8cb67ee2a56edd3e4e2d3a75f17b87717655bee43f91310aa4a45fe97c50bbbe4b7888ac567496ce97b3
@@ -0,0 +1,48 @@
1
+ require "delegate"
2
+ require_relative "./pipe_envy/version"
3
+
4
+ PIPE_ENVY_PIPE = -> (arg) do
5
+ #puts "#{self.class.name}, arg=#{arg.inspect}"
6
+ if arg.is_a?(Array)
7
+ method = arg.shift
8
+ block = arg.pop if arg.last.is_a?(Proc)
9
+ args = arg
10
+ end
11
+ method ||= arg
12
+ args ||= []
13
+
14
+ #puts "#{self.class.name}, method=#{method.inspect}, args=#{args.inspect}, block=#{block.inspect}"
15
+ if method.is_a?(Method)
16
+ result = method.call(*([self] + args), &block)
17
+ else
18
+ result = send(method, *args, &block)
19
+ end
20
+ result = PipeEnvyArray.new(result) if result.is_a?(Array)
21
+ #puts "#{self.class.name}, method=#{method.inspect}, args=#{args.inspect}, block=#{block.inspect}, result=#{result.inspect}"
22
+ #puts
23
+ result
24
+ end
25
+
26
+ class PipeEnvyArray < SimpleDelegator
27
+ define_method :|, &PIPE_ENVY_PIPE
28
+
29
+ # Original Array pipe operator method logic
30
+ # SEE: https://ruby-doc.org/core-2.4.1/Array.html#method-i-7C
31
+ def union(other_array=[])
32
+ (self + (other_array - self)).uniq
33
+ end
34
+ end
35
+
36
+ module PipeEnvy
37
+ refine Object do
38
+ define_method :|, &PIPE_ENVY_PIPE
39
+ end
40
+
41
+ refine Array do
42
+ define_method :|, &PIPE_ENVY_PIPE
43
+ end
44
+
45
+ refine Integer do
46
+ define_method :|, &PIPE_ENVY_PIPE
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module PipeEnvy
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,46 @@
1
+ require_relative "../lib/pipe_envy"
2
+ using PipeEnvy
3
+
4
+ class PipeEnvyTest < PryTest::Test
5
+ test "simple readme example" do
6
+ assert "Ruby Rocks" | :upcase | :split == ["RUBY", "ROCKS"]
7
+ end
8
+
9
+ test "chained pipes" do
10
+ output = "example" \
11
+ | :chars \
12
+ | :last \
13
+ | :upcase
14
+ assert output == "E"
15
+ end
16
+
17
+ test "array pipes with union" do
18
+ output = "example" \
19
+ | :chars \
20
+ | [:union, ["a", "b", "c"]]
21
+ assert output == ["e", "x", "a", "m", "p", "l", "b", "c"]
22
+ assert output | :size == 8
23
+ end
24
+
25
+ test "pipes with block" do
26
+ output = [1, 2, 3, 4, 5, 6, 7, 8, 9] \
27
+ | [:select!, -> (i) { i.even? }]
28
+ assert output == [2, 4, 6, 8]
29
+ end
30
+
31
+ test "complex pipeline" do
32
+ output = (1..100) \
33
+ | :to_a \
34
+ | [:select!, -> (i) { i.even? }] \
35
+ | [:map, -> (i) { i ** 10 }] \
36
+ | :sum \
37
+ | Math.method(:sqrt) \
38
+ | :to_s \
39
+ | :chars \
40
+ | :reverse \
41
+ | [:[], 3] \
42
+ | :to_i
43
+
44
+ assert output == 7
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pipe_envy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Hopkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
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: rake
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: pry-test
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
+ description:
56
+ email:
57
+ - natehop@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/pipe_envy.rb
63
+ - lib/pipe_envy/version.rb
64
+ - test/pipe_envy_test.rb
65
+ homepage: https://github.com/hopsoft/pipe_envy
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '2.4'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.6.11
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Elixir style pipe operator for Ruby
89
+ test_files:
90
+ - test/pipe_envy_test.rb