darthjee-core_ext 1.4.1 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd5dd16fc0fc9a7a42a9ab2947bd5b2826545c8c
4
- data.tar.gz: 66d35f5d8765636a1d47855a746505d0631246b2
3
+ metadata.gz: 81ad7c5b475cdb0f4592f21c3101636586cabba2
4
+ data.tar.gz: 91e2d9111a4d05a870f12612fa196aa1000299cc
5
5
  SHA512:
6
- metadata.gz: b44cc8d55182fc41c3374bf6fa63257ac95a693ecaaed69d9ac7d3438b820cc2fdb0927c42acc51ddd2b8d41ce9940322d1e0c77d38710c8e550541f4fefd99a
7
- data.tar.gz: 6f2749c189ae42dde35ee2b836485d00612bec1def1f33303fd69a95e59f555d596cf3e1ee39f019931b3db559faa920f29ce884dd94bb77666e5a3e2f041f87
6
+ metadata.gz: e3364a9163155827ad9f5d893b24be28e7d4217f38ee04954d110bbc131d1bcab6bc8363c3e195671d15e37b4744418457f325a445f949ec2c85759d15097dc9
7
+ data.tar.gz: 3b2460647e948351d001080baf180bcacd70e37120b35782fc58a5af0186cb0ac116cffa7fcab904e24c9a692518d0f5e4896db41de27a885045c60bf3976de5
@@ -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 chain_map(*methods)
5
- result = self
6
- result = result.map(&(methods.shift)) until methods.empty?
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 { |*args| yield(*args) }
24
+ result.map(&block)
10
25
  end
11
26
 
12
27
  def as_hash(keys)
@@ -1,5 +1,5 @@
1
1
  module Darthjee
2
2
  module CoreExt
3
- VERSION = '1.4.1'
3
+ VERSION = '1.5.0'
4
4
  end
5
5
  end
@@ -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.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-07-16 00:00:00.000000000 Z
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