darthjee-core_ext 1.4.1 → 1.5.0
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/docker-compose.yml +18 -0
- data/lib/darthjee/core_ext/array.rb +19 -4
- data/lib/darthjee/core_ext/version.rb +1 -1
- data/spec/lib/array_spec.rb +78 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81ad7c5b475cdb0f4592f21c3101636586cabba2
|
4
|
+
data.tar.gz: 91e2d9111a4d05a870f12612fa196aa1000299cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3364a9163155827ad9f5d893b24be28e7d4217f38ee04954d110bbc131d1bcab6bc8363c3e195671d15e37b4744418457f325a445f949ec2c85759d15097dc9
|
7
|
+
data.tar.gz: 3b2460647e948351d001080baf180bcacd70e37120b35782fc58a5af0186cb0ac116cffa7fcab904e24c9a692518d0f5e4896db41de27a885045c60bf3976de5
|
data/docker-compose.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
version: '2'
|
2
|
+
services:
|
3
|
+
base: &base
|
4
|
+
image: ruby:2.4.0
|
5
|
+
working_dir: /home/app/core_ext
|
6
|
+
volumes:
|
7
|
+
- .:/home/app/core_ext
|
8
|
+
- core_ext_gems_2_4_0:/usr/local/bundle
|
9
|
+
|
10
|
+
#################### CONTAINERS ####################
|
11
|
+
|
12
|
+
core_ext:
|
13
|
+
<<: *base
|
14
|
+
container_name: core_ext
|
15
|
+
command: bash -c 'gem install bundler; bundle install; bundle exec rspec'
|
16
|
+
|
17
|
+
volumes:
|
18
|
+
core_ext_gems_2_4_0:
|
@@ -1,12 +1,27 @@
|
|
1
1
|
require 'darthjee/core_ext/array/hash_builder'
|
2
2
|
|
3
3
|
class Array
|
4
|
-
def
|
5
|
-
|
6
|
-
|
4
|
+
def procedural_join(mapper = proc(&:to_s))
|
5
|
+
return '' if empty?
|
6
|
+
list = dup
|
7
|
+
prev = first
|
8
|
+
list[0] = mapper.call(prev).to_s
|
9
|
+
|
10
|
+
list.inject do |string, val|
|
11
|
+
j = yield(prev, val) if block_given?
|
12
|
+
"#{string}#{j}#{mapper.call(val)}".tap do
|
13
|
+
prev = val
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def chain_map(*methods, &block)
|
19
|
+
result = methods.inject(self) do |array, method|
|
20
|
+
array.map(&method)
|
21
|
+
end
|
7
22
|
|
8
23
|
return result unless block_given?
|
9
|
-
result.map
|
24
|
+
result.map(&block)
|
10
25
|
end
|
11
26
|
|
12
27
|
def as_hash(keys)
|
data/spec/lib/array_spec.rb
CHANGED
@@ -3,6 +3,76 @@ require 'spec_helper'
|
|
3
3
|
describe Array do
|
4
4
|
it_behaves_like 'an array with map_to_hash method'
|
5
5
|
|
6
|
+
describe '#procedural_join' do
|
7
|
+
let(:array) { [1, 2, -3, -4, 5] }
|
8
|
+
let(:map_proc) { proc { |v| v > 0 ? v + 1 : v - 1 } }
|
9
|
+
let(:result) do
|
10
|
+
array.procedural_join(map_proc) do |previous, nexte|
|
11
|
+
previous * nexte > 0 ? ',' : '|'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'joins proceduraly' do
|
16
|
+
expect(result).to eq('2,3|-4,-5|6')
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'does not change the array' do
|
20
|
+
expect do
|
21
|
+
result
|
22
|
+
end.not_to change { array }
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when array is empty' do
|
26
|
+
let(:array) { [] }
|
27
|
+
|
28
|
+
it do
|
29
|
+
expect do
|
30
|
+
result
|
31
|
+
end.not_to raise_error
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'acts as join for an empty array' do
|
35
|
+
expect(result).to eq(array.join)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'when array has only one element' do
|
40
|
+
let(:array) { [ 2 ] }
|
41
|
+
|
42
|
+
it do
|
43
|
+
expect do
|
44
|
+
result
|
45
|
+
end.not_to raise_error
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'acts as map join for a single element array' do
|
49
|
+
expect(result).to eq(array.map(&map_proc).join)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'when no mapping proc is passed' do
|
54
|
+
let(:result) do
|
55
|
+
array.procedural_join do |previous, nexte|
|
56
|
+
previous * nexte > 0 ? ',' : '|'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'proceduraly joins without mapping' do
|
61
|
+
expect(result).to eq('1,2|-3,-4|5')
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'when no block is given' do
|
66
|
+
let(:result) do
|
67
|
+
array.procedural_join(map_proc)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'acts as map join for a single element array' do
|
71
|
+
expect(result).to eq(array.map(&map_proc).join)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
6
76
|
describe '#chain_map' do
|
7
77
|
let(:array) { [ :a, :long_name, :sym ] }
|
8
78
|
let(:mapped) { array.chain_map(:to_s, :size, :to_s) }
|
@@ -95,6 +165,14 @@ describe Array do
|
|
95
165
|
|
96
166
|
describe '#random' do
|
97
167
|
it_behaves_like 'a method that returns a random element', :random
|
168
|
+
|
169
|
+
let(:array) { [ 8,4,2 ] }
|
170
|
+
|
171
|
+
it 'removes an the returned element' do
|
172
|
+
expect do
|
173
|
+
array.random
|
174
|
+
end.not_to change { array }
|
175
|
+
end
|
98
176
|
end
|
99
177
|
|
100
178
|
describe '#random!' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: darthjee-core_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Darthjee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -142,6 +142,7 @@ files:
|
|
142
142
|
- SYMBOL_README.md
|
143
143
|
- circle.yml
|
144
144
|
- core_ext.gemspec
|
145
|
+
- docker-compose.yml
|
145
146
|
- lib/darthjee.rb
|
146
147
|
- lib/darthjee/core_ext.rb
|
147
148
|
- lib/darthjee/core_ext/array.rb
|