ree_lib 1.0.109 → 1.0.111

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
  SHA256:
3
- metadata.gz: f2c475acec8b61f3686477e9ae4643427b3f956c8abb2ae8fe6c4d8e8ebb2c82
4
- data.tar.gz: 0a2138c853bd35f2340a9469b960e0c2cad3f89e4fdc4662692661048c9a3638
3
+ metadata.gz: 59f41ff2397eb9507b0e565246430ccde3467b8137bb747991181cecbb99a8d0
4
+ data.tar.gz: c267d40f7cd30a41ab86f428c0f30eec225a96fc6ed3ca33e9ab58cf130c0529
5
5
  SHA512:
6
- metadata.gz: bb6bab53e49f860719f7b342df2500a16f5ce97e431cc2e96d04df39bdc749521a5f639f4d0d6f6b3068ed840e35adaa5144847805b8a5254d12e3969f36c566
7
- data.tar.gz: 1bb7d488e89e1c5824012f6e1cde805bcc1d5bd095515c0e90608fca7995884115b2053fd6527306c0d6476884601c2d446b445260cabc46229e9e73cc8d11e4
6
+ metadata.gz: 04367c6369bd195fa8f2094159f999a1897485632b3e9c86d7e4ed7909513aeef7ea3beba05d544f7660b5bc9a22522f93cd7e3035da6650fbe60ea580d92045
7
+ data.tar.gz: 305010534eb1ed83b5e7a900b2cd88a9429ce6d1c629527af13dd1ecfcc8951b65a3ca68eb3c74c1a65f1d41e6ebce4cff788b4d5525cc787ab3bf13b0ffdebf
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ree_lib (1.0.109)
4
+ ree_lib (1.0.111)
5
5
  bigdecimal (~> 3.1.6)
6
6
  binding_of_caller (~> 1.0.0)
7
7
  i18n (~> 1.14.1)
@@ -6,7 +6,7 @@ class ReeArray::GroupBy
6
6
  fn :group_by
7
7
 
8
8
  contract(
9
- ArrayOf[Any], Block => HashOf[Any, ArrayOf[Any]]
9
+ Or[ArrayOf[Any], Enumerable], Block => HashOf[Any, ArrayOf[Any]]
10
10
  )
11
11
  def call(list, &proc)
12
12
  result = {}
@@ -8,24 +8,24 @@ class ReeArray::InGroups
8
8
  doc(<<~DOC)
9
9
  Splits or iterates over the array in +number+ of groups, padding any
10
10
  remaining slots with +fill_with+ unless it is +false+.
11
-
11
+
12
12
  in_groups(%w(1 2 3 4 5 6 7 8 9 10), 3, fill_with: nil) {|group| p group}
13
13
  ["1", "2", "3", "4"]
14
14
  ["5", "6", "7", nil]
15
15
  ["8", "9", "10", nil]
16
-
16
+
17
17
  in_groups(%w(1 2 3 4 5 6 7 8 9 10, 3, fill_with: '&nbsp;') {|group| p group}
18
18
  ["1", "2", "3", "4"]
19
19
  ["5", "6", "7", "&nbsp;"]
20
20
  ["8", "9", "10", "&nbsp;"]
21
-
21
+
22
22
  in_groups(%w(1 2 3 4 5 6 7), 3) {|group| p group}
23
23
  ["1", "2", "3"]
24
24
  ["4", "5"]
25
25
  ["6", "7"]
26
26
  DOC
27
27
  contract(
28
- ArrayOf[Any],
28
+ Or[ArrayOf[Any], Enumerable],
29
29
  Integer,
30
30
  Ksplat[fill_with?: Any],
31
31
  Optblock => Or[ArrayOf[Any], Any]
@@ -8,25 +8,25 @@ class ReeArray::InGroupsOf
8
8
  doc(<<~DOC)
9
9
  Splits or iterates over the array in groups of size +number+,
10
10
  padding any remaining slots with +fill_with+ unless it is +false+.
11
-
11
+
12
12
  in_groups_of(%w(1 2 3 4 5 6 7 8 9 10), 3, fill_with: nil) {|group| p group}
13
13
  ["1", "2", "3"]
14
14
  ["4", "5", "6"]
15
15
  ["7", "8", "9"]
16
16
  ["10", nil, nil]
17
-
17
+
18
18
  in_groups_of(%w(1 2 3 4 5), 2, fill_with: '&nbsp;') {|group| p group}
19
19
  ["1", "2"]
20
20
  ["3", "4"]
21
21
  ["5", "&nbsp;"]
22
-
22
+
23
23
  in_groups_of(%w(1 2 3 4 5), 2) {|group| p group}
24
24
  ["1", "2"]
25
25
  ["3", "4"]
26
26
  ["5"]
27
27
  DOC
28
28
  contract(
29
- ArrayOf[Any],
29
+ Or[ArrayOf[Any], Enumerable],
30
30
  Integer,
31
31
  Ksplat[fill_with?: Any],
32
32
  Optblock => Or[ArrayOf[Any], ArrayOf[ArrayOf[Any]]]
@@ -5,7 +5,7 @@ class ReeArray::IndexBy
5
5
 
6
6
  fn :index_by
7
7
 
8
- contract(ArrayOf[Any], Block => Hash)
8
+ contract(Or[ArrayOf[Any], Enumerable], Block => Hash)
9
9
  def call(list, &proc)
10
10
  result = {}
11
11
  list.each { result[yield(_1)] = _1 }
@@ -8,11 +8,11 @@ class ReeArray::SplitBy
8
8
  doc(<<~DOC)
9
9
  Divides the array into one or more subarrays based on a delimiting +value+
10
10
  or the result of an optional block.
11
-
11
+
12
12
  split([1, 2, 3, 4, 5], 3) # => [[1, 2], [4, 5]]
13
13
  split((1..10).to_a) { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
14
14
  DOC
15
- contract(ArrayOf[Any], Any, Optblock => ArrayOf[Any])
15
+ contract(Or[ArrayOf[Any], Enumerable], Any, Optblock => ArrayOf[Any])
16
16
  def call(array, value = nil, &block)
17
17
  arr = array.dup
18
18
  result = []
@@ -7,13 +7,13 @@ class ReeArray::Wrap
7
7
 
8
8
  doc(<<~DOC)
9
9
  Wraps its argument in an array unless it is already an array (or array-like).
10
-
10
+
11
11
  Specifically:
12
-
12
+
13
13
  * If the argument is +nil+ an empty array is returned.
14
14
  * Otherwise, if the argument responds to +to_ary+ it is invoked, and its result returned.
15
15
  * Otherwise, returns an array with the argument as its single element.
16
-
16
+
17
17
  wrap(nil) # => []
18
18
  wrap([1, 2, 3]) # => [1, 2, 3]
19
19
  wrap(0) # => [0]
@@ -22,7 +22,7 @@ class ReeArray::Wrap
22
22
  def call(object)
23
23
  if object.nil?
24
24
  []
25
- elsif object.is_a?(Array)
25
+ elsif object.is_a?(Array) || object.is_a?(Enumerable)
26
26
  object
27
27
  else
28
28
  [object]
@@ -14,4 +14,35 @@ RSpec.describe :index_by do
14
14
  }
15
15
  )
16
16
  }
17
+
18
+ it {
19
+ class EnumerableArray
20
+ include Enumerable
21
+
22
+ def initialize
23
+ @list = []
24
+ end
25
+
26
+ def each(&proc)
27
+ @list.each &proc
28
+ end
29
+
30
+ def add(v)
31
+ @list << v
32
+ end
33
+ end
34
+
35
+ list = EnumerableArray.new
36
+ list.add({id: 1})
37
+ list.add({id: 2})
38
+
39
+ result = index_by(list) { _1[:id] }
40
+
41
+ expect(result).to eq(
42
+ {
43
+ 1 => {id: 1},
44
+ 2 => {id: 2}
45
+ }
46
+ )
47
+ }
17
48
  end
@@ -9,7 +9,7 @@ class ReeMapper::Array < ReeMapper::AbstractWrapper
9
9
  ] => Array
10
10
  ).throws(ReeMapper::TypeError)
11
11
  def serialize(value, role: nil, fields_filters: nil)
12
- if !value.is_a?(Array)
12
+ if !check_value(value)
13
13
  raise ReeMapper::TypeError.new("should be an array, got `#{truncate(value.inspect)}`")
14
14
  end
15
15
 
@@ -40,7 +40,7 @@ class ReeMapper::Array < ReeMapper::AbstractWrapper
40
40
  ] => Array
41
41
  ).throws(ReeMapper::TypeError)
42
42
  def cast(value, role: nil, fields_filters: nil)
43
- if !value.is_a?(Array)
43
+ if !check_value(value)
44
44
  raise ReeMapper::TypeError.new("should be an array, got `#{truncate(value.inspect)}`")
45
45
  end
46
46
 
@@ -71,7 +71,7 @@ class ReeMapper::Array < ReeMapper::AbstractWrapper
71
71
  ] => Array
72
72
  ).throws(ReeMapper::TypeError)
73
73
  def db_dump(value, role: nil, fields_filters: nil)
74
- if !value.is_a?(Array)
74
+ if !check_value(value)
75
75
  raise ReeMapper::TypeError.new("should be an array, got `#{truncate(value.inspect)}`")
76
76
  end
77
77
 
@@ -102,7 +102,7 @@ class ReeMapper::Array < ReeMapper::AbstractWrapper
102
102
  ] => Array
103
103
  ).throws(ReeMapper::TypeError)
104
104
  def db_load(value, role: nil, fields_filters: nil)
105
- if !value.is_a?(Array)
105
+ if !check_value(value)
106
106
  raise ReeMapper::TypeError.new("should be an array, got `#{truncate(value.inspect)}`")
107
107
  end
108
108
 
@@ -124,4 +124,10 @@ class ReeMapper::Array < ReeMapper::AbstractWrapper
124
124
  raise e
125
125
  end
126
126
  end
127
+
128
+ private
129
+
130
+ def check_value(value)
131
+ value.is_a?(Array) || value.class.include?(Enumerable)
132
+ end
127
133
  end
@@ -22,7 +22,31 @@ RSpec.describe 'ReeMapper::Array' do
22
22
  }
23
23
  }
24
24
 
25
+ class EnumerableArray
26
+ include Enumerable
27
+
28
+ def initialize
29
+ @list = []
30
+ end
31
+
32
+ def each(&proc)
33
+ @list.each &proc
34
+ end
35
+
36
+ def add(v)
37
+ @list << v
38
+ end
39
+ end
40
+
25
41
  describe '#serialize' do
42
+ it {
43
+ ar = EnumerableArray.new
44
+ ar.add(1)
45
+ ar.add(2)
46
+
47
+ expect(mapper.serialize({ tags: ar, ary_of_ary: [[1]] })).to eq({ tags: [1, 2], ary_of_ary: [[1]] })
48
+ }
49
+
26
50
  it {
27
51
  expect(mapper.serialize({ tags: [1, 2], ary_of_ary: [[1]] })).to eq({ tags: [1, 2], ary_of_ary: [[1]] })
28
52
  }
@@ -41,6 +65,14 @@ RSpec.describe 'ReeMapper::Array' do
41
65
  end
42
66
 
43
67
  describe '#cast' do
68
+ it {
69
+ ar = EnumerableArray.new
70
+ ar.add(1)
71
+ ar.add(2)
72
+
73
+ expect(mapper.cast({ 'tags' => ar })).to eq({ tags: [1, 2] })
74
+ }
75
+
44
76
  it {
45
77
  expect(mapper.cast({ 'tags' => [1, 2] })).to eq({ tags: [1, 2] })
46
78
  }
@@ -51,6 +83,14 @@ RSpec.describe 'ReeMapper::Array' do
51
83
  end
52
84
 
53
85
  describe '#db_dump' do
86
+ it {
87
+ ar = EnumerableArray.new
88
+ ar.add(1)
89
+ ar.add(2)
90
+
91
+ expect(mapper.db_dump(OpenStruct.new({ tags: ar }))).to eq({ tags: [1, 2] })
92
+ }
93
+
54
94
  it {
55
95
  expect(mapper.db_dump(OpenStruct.new({ tags: [1, 2] }))).to eq({ tags: [1, 2] })
56
96
  }
@@ -61,6 +101,14 @@ RSpec.describe 'ReeMapper::Array' do
61
101
  end
62
102
 
63
103
  describe '#db_load' do
104
+ it {
105
+ ar = EnumerableArray.new
106
+ ar.add(1)
107
+ ar.add(2)
108
+
109
+ expect(mapper.db_load({ 'tags' => ar })).to eq({ tags: [1, 2] })
110
+ }
111
+
64
112
  it {
65
113
  expect(mapper.db_load({ 'tags' => [1, 2] })).to eq({ tags: [1, 2] })
66
114
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ReeLib
4
- VERSION = "1.0.109"
4
+ VERSION = "1.0.111"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ree_lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.109
4
+ version: 1.0.111
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ruslan Gatiyatov