abstractivator 0.0.21 → 0.0.22

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 225d5358712a6832210c7f813bc04131d388f7c4
4
- data.tar.gz: d04cf93b9969771af87f578a3fee1eaa3053ac0b
3
+ metadata.gz: ed70b3e745e17ec7a24ef81a0f8734a88408f6ee
4
+ data.tar.gz: 7f463093462a219c2f055da69e14f867ad45d5cd
5
5
  SHA512:
6
- metadata.gz: 9715f8fd66114314cb1eed58c43bed53e60c12e28d814c4c7414686d15d79b21cd078d234ce49d63ea7e7811a5f5f4ed97a5872512549969ee1b673d7e12d00a
7
- data.tar.gz: d00027043d191a6eae479d5a57971f6fbd6dba1d1858b7f83edf9ae791e2a97194b03179d34ea6a30e5688c4f8bbf50c44443127566c84f5e0904f0148303ca0
6
+ metadata.gz: 36cc3d351e63c62cb1a84cd073747a734a145b1712111fad30431df3f82c59b5eb7cf4eca6c096d8eb824131c2b953a28eedcf0b83cc8ba1bc327ff8260aa1f2
7
+ data.tar.gz: aabc33b65cd3f7133f78828e2f14e379c360f64accbccd2df9d6f399594d88730b8ccfd1d29904ec763f83a12f212435fd74d5873889e56606088174dccc8e9d
@@ -1,4 +1,5 @@
1
1
  require 'set'
2
+ require 'abstractivator/proc_ext'
2
3
 
3
4
  module Enumerable
4
5
 
@@ -48,7 +49,7 @@ module Enumerable
48
49
  end
49
50
 
50
51
  def hash_map(get_key=->x{x}, &get_value)
51
- Hash[self.map{|x| [get_key.(x), get_value ? get_value.(x) : x]}]
52
+ Hash[self.map{|x| [Proc.loose_call(get_key, [x]), get_value ? get_value.call(x) : x]}]
52
53
  end
53
54
 
54
55
  def outer_join(right, get_left_key, get_right_key, default_value)
@@ -30,7 +30,10 @@ class Proc
30
30
  end
31
31
 
32
32
  def self.loose_call(x, args, &block)
33
- x.respond_to?(:call) ? x.call(*args.take(x.arity).pad_right(x.arity), &block) : x
33
+ x = x.to_proc if x.respond_to?(:to_proc)
34
+ x.respond_to?(:call) or return x
35
+ args = args.take(x.arity).pad_right(x.arity) if x.arity >= 0
36
+ x.call(*args, &block)
34
37
  end
35
38
  end
36
39
 
@@ -1,3 +1,3 @@
1
1
  module Abstractivator
2
- VERSION = '0.0.21'
2
+ VERSION = '0.0.22'
3
3
  end
@@ -141,4 +141,24 @@ describe Enumerable do
141
141
  expect(result).to eql [:x, 1, 2, 3]
142
142
  end
143
143
  end
144
+
145
+ describe '#hash_map' do
146
+ let!(:xs) { [Object, Hash, Array] }
147
+ it 'turns an array into a map' do
148
+ result = xs.hash_map(proc{|x| x.to_s[0]}) {|x| x.to_s }
149
+ expect(result).to eql({ 'O' => 'Object', 'H' => 'Hash', 'A' => 'Array' })
150
+ end
151
+ it 'the value transformer can be omitted' do
152
+ result = xs.hash_map(proc{|x| x.to_s[0]})
153
+ expect(result).to eql({ 'O' => Object, 'H' => Hash, 'A' => Array })
154
+ end
155
+ it 'the key transformer can be omitted' do
156
+ result = xs.hash_map(&:to_s)
157
+ expect(result).to eql({ Object => 'Object', Hash => 'Hash', Array => 'Array' })
158
+ end
159
+ it 'the key transformer is called loosely' do
160
+ result = xs.hash_map(:to_s)
161
+ expect(result).to eql({ 'Object' => Object, 'Hash' => Hash, 'Array' => Array })
162
+ end
163
+ end
144
164
  end
@@ -34,7 +34,10 @@ context 'in the world of functional programming' do
34
34
 
35
35
  describe 'Proc::loose_call' do
36
36
  it 'returns the first argument if it is not a proc' do
37
- expect(Proc.loose_call(:a, [:b, :c])).to eql :a
37
+ expect(Proc.loose_call('a', ['b', 'c'])).to eql 'a'
38
+ end
39
+ it 'attempts to convert the first argument to proc' do
40
+ expect(Proc.loose_call(:to_s, ['5'])).to eql '5'
38
41
  end
39
42
  it 'calls the proc with an appropriate number of arguments' do
40
43
  events = []
@@ -48,6 +51,9 @@ context 'in the world of functional programming' do
48
51
  it 'pads with nils' do
49
52
  expect(Proc.loose_call(->(a, b) {[a, b]}, [1])).to eql [1, nil]
50
53
  end
54
+ it 'works with variable-arity procs' do
55
+ expect(Proc.loose_call(->(*args) {args}, [1, 2])).to eql [1, 2]
56
+ end
51
57
  end
52
58
 
53
59
  describe 'Proc#loosen_args' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abstractivator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.21
4
+ version: 0.0.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Winton