ree_lib 1.0.16 → 1.0.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (26) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper.rb +7 -6
  4. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/abstract_type.rb +4 -4
  5. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/array.rb +20 -20
  6. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb +12 -12
  7. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date.rb +13 -13
  8. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb +13 -13
  9. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb +13 -13
  10. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb +13 -13
  11. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/string.rb +12 -12
  12. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/time.rb +13 -13
  13. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/array_spec.rb +15 -6
  14. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/bool_spec.rb +9 -9
  15. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/date_spec.rb +11 -11
  16. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/date_time_spec.rb +17 -17
  17. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/float_spec.rb +14 -14
  18. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb +5 -1
  19. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/integer_spec.rb +15 -15
  20. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/string_spec.rb +13 -13
  21. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/time_spec.rb +17 -17
  22. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/type_options_spec.rb +2 -2
  23. data/lib/ree_lib/version.rb +1 -1
  24. metadata +2 -4
  25. data/lib/ree_lib/packages/ree_dao/package/ree_dao/types/pg_array.rb +0 -43
  26. data/lib/ree_lib/packages/ree_dao/spec/ree_dao/types/pg_array_spec.rb +0 -19
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9005df5ff7af1e3f311ab2126f137560568d8e2c42bfb7da28b9a2e179675b4a
4
- data.tar.gz: b0c9cd050d90ad8a2f66a738bfd3487c27d9561a367ce3a7d204c5ba6bf8d1cf
3
+ metadata.gz: 6160f2952d35b381ccb85c48139a84187d975ec72297a13ba240656398610bb1
4
+ data.tar.gz: d8aa690997677ddf8ae5bf2f219b00e5d7cad9bb0dcea89905f496bca2389072
5
5
  SHA512:
6
- metadata.gz: 8816a34c2a7b69a9a82d61c5fc2f69055216cfe6ff02241c78850b9940fe37d09c1df87b49e0f137342f84ac304f6e3b0dfc4075af92f29a86fb4e77a92a981c
7
- data.tar.gz: 9bd5bce19e5803ae000b362efcad7721802bc1c816dc1af26eefb80fc04c893b4b8c505471b033456c4b500a84b76de9b1ea319869adfab945c75ae2c52c675c
6
+ metadata.gz: 666753ee3a5ff1057d1d8b371d84b9a2510b0d89ce4a259ded6bdf0466f61f0009c18505f70bee8e92e65058cc1d460510cf9121ee3980845c3fd05936aa1f1f
7
+ data.tar.gz: d543b184c95e126f6623e339dcfe17d6966aa19b50c19378701f250d45f599b4734f2bf0c0a194effb4af0bd194b88d244060076362e7e569215b1470cee0275
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ree_lib (1.0.16)
4
+ ree_lib (1.0.17)
5
5
  binding_of_caller (~> 1.0.0)
6
6
  i18n (~> 1.12.0)
7
7
  loofah (~> 2.18.0)
@@ -19,32 +19,33 @@ class ReeMapper::Mapper
19
19
 
20
20
  if type
21
21
  class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
22
- def #{method}(obj, role: nil)
23
- @type.#{method}(obj, role: role)
22
+ def #{method}(obj, name: nil, role: nil)
23
+ @type.#{method}(obj, name: name, role: role)
24
24
  end
25
25
  RUBY
26
26
  else
27
27
  class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
28
- def #{method}(obj, role: nil)
28
+ def #{method}(obj, name: nil, role: nil)
29
29
  @fields.each_with_object(@#{method}_strategy.build_object) do |(_, field), acc|
30
30
  next unless field.has_role?(role)
31
+ nested_name = name ? "\#{name}[\#{field.name_as_str}]" : field.name_as_str
31
32
 
32
33
  if @#{method}_strategy.has_value?(obj, field)
33
34
  value = @#{method}_strategy.get_value(obj, field)
34
35
  unless value.nil? && field.null
35
- value = field.type.public_send(:#{method}, value, role: role)
36
+ value = field.type.#{method}(value, name: nested_name, role: role)
36
37
  end
37
38
  @#{method}_strategy.assign_value(acc, field, value)
38
39
  elsif field.optional || @#{method}_strategy.always_optional
39
40
  if field.has_default?
40
41
  value = field.default
41
42
  unless value.nil? && field.null
42
- value = field.type.public_send(:#{method}, value, role: role)
43
+ value = field.type.#{method}(value, name: nested_name, role: role)
43
44
  end
44
45
  @#{method}_strategy.assign_value(acc, field, value)
45
46
  end
46
47
  else
47
- raise ReeMapper::TypeError, "Missing required field `\#{field.from}`"
48
+ raise ReeMapper::TypeError, "Missing required field `\#{field.from_as_str}` for `\#{name || 'root'}`"
48
49
  end
49
50
  end
50
51
  end
@@ -1,17 +1,17 @@
1
1
  class ReeMapper::AbstractType
2
- def serialize(value, role: nil)
2
+ def serialize(value, name:, role: nil)
3
3
  raise NotImplementedError
4
4
  end
5
5
 
6
- def cast(value, role: nil)
6
+ def cast(value, name:, role: nil)
7
7
  raise NotImplementedError
8
8
  end
9
9
 
10
- def db_dump(value, role: nil)
10
+ def db_dump(value, name:, role: nil)
11
11
  raise NotImplementedError
12
12
  end
13
13
 
14
- def db_load(value, role: nil)
14
+ def db_load(value, name:, role: nil)
15
15
  raise NotImplementedError
16
16
  end
17
17
  end
@@ -8,63 +8,63 @@ class ReeMapper::Array < ReeMapper::AbstractType
8
8
  @of = of
9
9
  end
10
10
 
11
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Array).throws(ReeMapper::TypeError)
12
- def serialize(value, role: nil)
11
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Array).throws(ReeMapper::TypeError)
12
+ def serialize(value, name:, role: nil)
13
13
  if value.is_a?(Array)
14
- value.map {
14
+ value.map.with_index {
15
15
  if _1.nil? && of.null
16
16
  _1
17
17
  else
18
- of.type.serialize(_1, role: role)
18
+ of.type.serialize(_1, name: "#{name}[#{_2}]", role: role)
19
19
  end
20
20
  }
21
21
  else
22
- raise ReeMapper::TypeError, 'should be an array'
22
+ raise ReeMapper::TypeError, "`#{name}` should be an array"
23
23
  end
24
24
  end
25
25
 
26
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Array).throws(ReeMapper::TypeError)
27
- def cast(value, role: nil)
26
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Array).throws(ReeMapper::TypeError)
27
+ def cast(value, name:, role: nil)
28
28
  if value.is_a?(Array)
29
- value.map {
29
+ value.map.with_index {
30
30
  if _1.nil? && of.null
31
31
  _1
32
32
  else
33
- of.type.cast(_1, role: role)
33
+ of.type.cast(_1, name: "#{name}[#{_2}]", role: role)
34
34
  end
35
35
  }
36
36
  else
37
- raise ReeMapper::TypeError, "should be an array"
37
+ raise ReeMapper::TypeError, "`#{name}` should be an array"
38
38
  end
39
39
  end
40
40
 
41
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Array).throws(ReeMapper::TypeError)
42
- def db_dump(value, role: nil)
41
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Array).throws(ReeMapper::TypeError)
42
+ def db_dump(value, name:, role: nil)
43
43
  if value.is_a?(Array)
44
- value.map {
44
+ value.map.with_index {
45
45
  if _1.nil? && of.null
46
46
  _1
47
47
  else
48
- of.type.db_dump(_1, role: role)
48
+ of.type.db_dump(_1, name: "#{name}[#{_2}]", role: role)
49
49
  end
50
50
  }
51
51
  else
52
- raise ReeMapper::TypeError, 'should be an array'
52
+ raise ReeMapper::TypeError, "`#{name}` should be an array"
53
53
  end
54
54
  end
55
55
 
56
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Array).throws(ReeMapper::TypeError)
57
- def db_load(value, role: nil)
56
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Array).throws(ReeMapper::TypeError)
57
+ def db_load(value, name:, role: nil)
58
58
  if value.is_a?(Array)
59
- value.map {
59
+ value.map.with_index {
60
60
  if _1.nil? && of.null
61
61
  _1
62
62
  else
63
- of.type.db_load(_1, role: role)
63
+ of.type.db_load(_1, name: "#{name}[#{_2}]", role: role)
64
64
  end
65
65
  }
66
66
  else
67
- raise ReeMapper::TypeError, "should be an array"
67
+ raise ReeMapper::TypeError, "`#{name}` should be an array"
68
68
  end
69
69
  end
70
70
  end
@@ -4,33 +4,33 @@ class ReeMapper::Bool < ReeMapper::AbstractType
4
4
  TRUE_CAST_VALUES = ['1', 'true', 'on', 1, true].freeze
5
5
  FALSE_CAST_VALUES = ['0', 'false', 'off', 0, false].freeze
6
6
 
7
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::TypeError)
8
- def serialize(value, role: nil)
7
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::TypeError)
8
+ def serialize(value, name:, role: nil)
9
9
  if value.is_a?(TrueClass) || value.is_a?(FalseClass)
10
10
  value
11
11
  else
12
- raise ReeMapper::TypeError, 'should be a boolean'
12
+ raise ReeMapper::TypeError, "`#{name}` should be a boolean"
13
13
  end
14
14
  end
15
15
 
16
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::CoercionError)
17
- def cast(value, role: nil)
16
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::CoercionError)
17
+ def cast(value, name:, role: nil)
18
18
  if TRUE_CAST_VALUES.include?(value)
19
19
  true
20
20
  elsif FALSE_CAST_VALUES.include?(value)
21
21
  false
22
22
  else
23
- raise ReeMapper::CoercionError, "should be a boolean"
23
+ raise ReeMapper::CoercionError, "`#{name}` is invalid boolean"
24
24
  end
25
25
  end
26
26
 
27
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::TypeError)
28
- def db_dump(value, role: nil)
29
- serialize(value, role: role)
27
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::TypeError)
28
+ def db_dump(value, name:, role: nil)
29
+ serialize(value, name: name, role: role)
30
30
  end
31
31
 
32
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::CoercionError)
33
- def db_load(value, role: nil)
34
- cast(value, role: role)
32
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::CoercionError)
33
+ def db_load(value, name:, role: nil)
34
+ cast(value, name: name, role: role)
35
35
  end
36
36
  end
@@ -3,17 +3,17 @@
3
3
  require 'date'
4
4
 
5
5
  class ReeMapper::Date < ReeMapper::AbstractType
6
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Date).throws(ReeMapper::TypeError)
7
- def serialize(value, role: nil)
6
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Date).throws(ReeMapper::TypeError)
7
+ def serialize(value, name:, role: nil)
8
8
  if value.class == Date
9
9
  value
10
10
  else
11
- raise ReeMapper::TypeError, "should be a date"
11
+ raise ReeMapper::TypeError, "`#{name}` should be a date"
12
12
  end
13
13
  end
14
14
 
15
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Date).throws(ReeMapper::TypeError, ReeMapper::CoercionError)
16
- def cast(value, role: nil)
15
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Date).throws(ReeMapper::TypeError, ReeMapper::CoercionError)
16
+ def cast(value, name:, role: nil)
17
17
  if value.class == Date
18
18
  value
19
19
  elsif value.class == DateTime || value.class == Time
@@ -22,20 +22,20 @@ class ReeMapper::Date < ReeMapper::AbstractType
22
22
  begin
23
23
  Date.parse(value)
24
24
  rescue ArgumentError => e
25
- raise ReeMapper::CoercionError, "is invalid date"
25
+ raise ReeMapper::CoercionError, "`#{name}` is invalid date"
26
26
  end
27
27
  else
28
- raise ReeMapper::TypeError, "should be a date"
28
+ raise ReeMapper::TypeError, "`#{name}` should be a date"
29
29
  end
30
30
  end
31
31
 
32
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Date).throws(ReeMapper::TypeError)
33
- def db_dump(value, role: nil)
34
- serialize(value, role: role)
32
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Date).throws(ReeMapper::TypeError)
33
+ def db_dump(value, name:, role: nil)
34
+ serialize(value, name: name, role: role)
35
35
  end
36
36
 
37
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Date).throws(ReeMapper::TypeError, ReeMapper::CoercionError)
38
- def db_load(value, role: nil)
39
- cast(value, role: role)
37
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Date).throws(ReeMapper::TypeError, ReeMapper::CoercionError)
38
+ def db_load(value, name:, role: nil)
39
+ cast(value, name: name, role: role)
40
40
  end
41
41
  end
@@ -3,17 +3,17 @@
3
3
  require 'date'
4
4
 
5
5
  class ReeMapper::DateTime < ReeMapper::AbstractType
6
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => DateTime).throws(ReeMapper::TypeError)
7
- def serialize(value, role: nil)
6
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => DateTime).throws(ReeMapper::TypeError)
7
+ def serialize(value, name:, role: nil)
8
8
  if value.class == DateTime
9
9
  value
10
10
  else
11
- raise ReeMapper::TypeError, "should be a datetime"
11
+ raise ReeMapper::TypeError, "`#{name}` should be a datetime"
12
12
  end
13
13
  end
14
14
 
15
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => DateTime).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
16
- def cast(value, role: nil)
15
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => DateTime).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
16
+ def cast(value, name:, role: nil)
17
17
  if value.class == DateTime
18
18
  value
19
19
  elsif value.class == Time
@@ -22,20 +22,20 @@ class ReeMapper::DateTime < ReeMapper::AbstractType
22
22
  begin
23
23
  ReeDatetime::InDefaultTimeZone.new.call(DateTime.parse(value))
24
24
  rescue ArgumentError
25
- raise ReeMapper::CoercionError, "is invalid datetime"
25
+ raise ReeMapper::CoercionError, "`#{name}` is invalid datetime"
26
26
  end
27
27
  else
28
- raise ReeMapper::TypeError, "should be a datetime"
28
+ raise ReeMapper::TypeError, "`#{name}` should be a datetime"
29
29
  end
30
30
  end
31
31
 
32
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => DateTime).throws(ReeMapper::TypeError)
33
- def db_dump(value, role: nil)
34
- serialize(value, role: role)
32
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => DateTime).throws(ReeMapper::TypeError)
33
+ def db_dump(value, name:, role: nil)
34
+ serialize(value, name: name, role: role)
35
35
  end
36
36
 
37
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => DateTime).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
38
- def db_load(value, role: nil)
39
- cast(value, role: role)
37
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => DateTime).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
38
+ def db_load(value, name:, role: nil)
39
+ cast(value, name: name, role: role)
40
40
  end
41
41
  end
@@ -1,37 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ReeMapper::Float < ReeMapper::AbstractType
4
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Float).throws(ReeMapper::TypeError)
5
- def serialize(value, role: nil)
4
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Float).throws(ReeMapper::TypeError)
5
+ def serialize(value, name:, role: nil)
6
6
  if value.is_a?(Float)
7
7
  value
8
8
  else
9
- raise ReeMapper::TypeError, 'should be a float'
9
+ raise ReeMapper::TypeError, "`#{name}` should be a float"
10
10
  end
11
11
  end
12
12
 
13
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Float).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
14
- def cast(value, role: nil)
13
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Float).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
14
+ def cast(value, name:, role: nil)
15
15
  if value.is_a?(Float)
16
16
  value
17
17
  elsif value.is_a?(String)
18
18
  begin
19
19
  Float(value)
20
20
  rescue ArgumentError => e
21
- raise ReeMapper::CoercionError, "is invalid float"
21
+ raise ReeMapper::CoercionError, "`#{name}` is invalid float"
22
22
  end
23
23
  else
24
- raise ReeMapper::TypeError, "should be a float"
24
+ raise ReeMapper::TypeError, "`#{name}` should be a float"
25
25
  end
26
26
  end
27
27
 
28
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Float).throws(ReeMapper::CoercionError)
29
- def db_dump(value, role: nil)
30
- serialize(value, role: role)
28
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Float).throws(ReeMapper::CoercionError)
29
+ def db_dump(value, name:, role: nil)
30
+ serialize(value, name: name, role: role)
31
31
  end
32
32
 
33
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Float).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
34
- def db_load(value, role: nil)
35
- cast(value, role: role)
33
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Float).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
34
+ def db_load(value, name:, role: nil)
35
+ cast(value, name: name, role: role)
36
36
  end
37
37
  end
@@ -1,37 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ReeMapper::Integer < ReeMapper::AbstractType
4
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Integer).throws(ReeMapper::TypeError)
5
- def serialize(value, role: nil)
4
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Integer).throws(ReeMapper::TypeError)
5
+ def serialize(value, name:, role: nil)
6
6
  if value.is_a? Integer
7
7
  value
8
8
  else
9
- raise ReeMapper::TypeError, 'should be an integer'
9
+ raise ReeMapper::TypeError, "`#{name}` should be an integer"
10
10
  end
11
11
  end
12
12
 
13
- contract(Any , Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]]=> Integer).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
14
- def cast(value, role: nil)
13
+ contract(Any , Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]]=> Integer).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
14
+ def cast(value, name:, role: nil)
15
15
  if value.is_a?(Integer)
16
16
  value
17
17
  elsif value.is_a?(String)
18
18
  begin
19
19
  Integer(value)
20
20
  rescue ArgumentError => e
21
- raise ReeMapper::CoercionError, "is invalid integer"
21
+ raise ReeMapper::CoercionError, "`#{name}` is invalid integer"
22
22
  end
23
23
  else
24
- raise ReeMapper::TypeError, "should be an integer"
24
+ raise ReeMapper::TypeError, "`#{name}` should be an integer"
25
25
  end
26
26
  end
27
27
 
28
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Integer).throws(ReeMapper::TypeError)
29
- def db_dump(value, role: nil)
30
- serialize(value, role: role)
28
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Integer).throws(ReeMapper::TypeError)
29
+ def db_dump(value, name:, role: nil)
30
+ serialize(value, name: name, role: role)
31
31
  end
32
32
 
33
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Integer).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
34
- def db_load(value, role: nil)
35
- cast(value, role: role)
33
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Integer).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
34
+ def db_load(value, name:, role: nil)
35
+ cast(value, name: name, role: role)
36
36
  end
37
37
  end
@@ -1,27 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ReeMapper::String < ReeMapper::AbstractType
4
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => String).throws(ReeMapper::TypeError)
5
- def serialize(value, role: nil)
4
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => String).throws(ReeMapper::TypeError)
5
+ def serialize(value, name:, role: nil)
6
6
  if value.is_a? String
7
7
  value
8
8
  else
9
- raise ReeMapper::TypeError, 'should be a string'
9
+ raise ReeMapper::TypeError, "`#{name}` should be a string"
10
10
  end
11
11
  end
12
12
 
13
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => String).throws(ReeMapper::TypeError)
14
- def cast(value, role: nil)
15
- serialize(value, role: role)
13
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => String).throws(ReeMapper::TypeError)
14
+ def cast(value, name:, role: nil)
15
+ serialize(value, name: name, role: role)
16
16
  end
17
17
 
18
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => String).throws(ReeMapper::TypeError)
19
- def db_dump(value, role: nil)
20
- serialize(value, role: role)
18
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => String).throws(ReeMapper::TypeError)
19
+ def db_dump(value, name:, role: nil)
20
+ serialize(value, name: name, role: role)
21
21
  end
22
22
 
23
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => String).throws(ReeMapper::TypeError)
24
- def db_load(value, role: nil)
25
- serialize(value, role: role)
23
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => String).throws(ReeMapper::TypeError)
24
+ def db_load(value, name:, role: nil)
25
+ serialize(value, name: name, role: role)
26
26
  end
27
27
  end
@@ -3,17 +3,17 @@
3
3
  require 'time'
4
4
 
5
5
  class ReeMapper::Time < ReeMapper::AbstractType
6
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Time).throws(ReeMapper::TypeError)
7
- def serialize(value, role: nil)
6
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Time).throws(ReeMapper::TypeError)
7
+ def serialize(value, name:, role: nil)
8
8
  if value.class == Time
9
9
  value
10
10
  else
11
- raise ReeMapper::TypeError, "should be a time"
11
+ raise ReeMapper::TypeError, "`#{name}` should be a time"
12
12
  end
13
13
  end
14
14
 
15
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Time).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
16
- def cast(value, role: nil)
15
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Time).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
16
+ def cast(value, name:, role: nil)
17
17
  if value.class == Time
18
18
  value
19
19
  elsif value.class == DateTime
@@ -22,20 +22,20 @@ class ReeMapper::Time < ReeMapper::AbstractType
22
22
  begin
23
23
  Time.parse(value)
24
24
  rescue ArgumentError
25
- raise ReeMapper::CoercionError, "is invalid time"
25
+ raise ReeMapper::CoercionError, "`#{name}` is invalid time"
26
26
  end
27
27
  else
28
- raise ReeMapper::TypeError, "should be a time"
28
+ raise ReeMapper::TypeError, "`#{name}` should be a time"
29
29
  end
30
30
  end
31
31
 
32
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Time).throws(ReeMapper::TypeError)
33
- def db_dump(value, role: nil)
34
- serialize(value, role: role)
32
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Time).throws(ReeMapper::TypeError)
33
+ def db_dump(value, name:, role: nil)
34
+ serialize(value, name: name, role: role)
35
35
  end
36
36
 
37
- contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Time).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
38
- def db_load(value, role: nil)
39
- cast(value, role: role)
37
+ contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Time).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
38
+ def db_load(value, name:, role: nil)
39
+ cast(value, name: name, role: role)
40
40
  end
41
41
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe ReeMapper::Array do
3
+ RSpec.describe 'ReeMapper::Array' do
4
4
  link :build_mapper_factory, from: :ree_mapper
5
5
  link :build_mapper_strategy, from: :ree_mapper
6
6
 
@@ -18,16 +18,25 @@ RSpec.describe ReeMapper::Array do
18
18
  let(:mapper) {
19
19
  mapper_factory.call.use(:cast).use(:serialize).use(:db_dump).use(:db_load) {
20
20
  array :tags, each: integer
21
+ array? :ary_of_ary, each: array(each: integer)
21
22
  }
22
23
  }
23
24
 
24
25
  describe '#serialize' do
25
26
  it {
26
- expect(mapper.serialize({ tags: [1, 2] })).to eq({ tags: [1, 2] })
27
+ expect(mapper.serialize({ tags: [1, 2], ary_of_ary: [[1]] })).to eq({ tags: [1, 2], ary_of_ary: [[1]] })
27
28
  }
28
29
 
29
30
  it {
30
- expect { mapper.serialize({ tags: 1 }) }.to raise_error(ReeMapper::TypeError)
31
+ expect { mapper.serialize({ tags: 1 }) }.to raise_error(ReeMapper::TypeError, "`tags` should be an array")
32
+ }
33
+
34
+ it {
35
+ expect { mapper.serialize({tags: [1], ary_of_ary: ["1"] }) }.to raise_error(ReeMapper::TypeError, "`ary_of_ary[0]` should be an array")
36
+ }
37
+
38
+ it {
39
+ expect { mapper.serialize({tags: [1], ary_of_ary: [[1, "1"]] }) }.to raise_error(ReeMapper::TypeError, "`ary_of_ary[0][1]` should be an integer")
31
40
  }
32
41
  end
33
42
 
@@ -37,7 +46,7 @@ RSpec.describe ReeMapper::Array do
37
46
  }
38
47
 
39
48
  it {
40
- expect { mapper.cast({ 'tags' => 1 }) }.to raise_error(ReeMapper::TypeError)
49
+ expect { mapper.cast({ 'tags' => 1 }) }.to raise_error(ReeMapper::TypeError, "`tags` should be an array")
41
50
  }
42
51
  end
43
52
 
@@ -47,7 +56,7 @@ RSpec.describe ReeMapper::Array do
47
56
  }
48
57
 
49
58
  it {
50
- expect { mapper.db_dump(OpenStruct.new({ tags: 1 })) }.to raise_error(ReeMapper::TypeError)
59
+ expect { mapper.db_dump(OpenStruct.new({ tags: 1 })) }.to raise_error(ReeMapper::TypeError, "`tags` should be an array")
51
60
  }
52
61
  end
53
62
 
@@ -57,7 +66,7 @@ RSpec.describe ReeMapper::Array do
57
66
  }
58
67
 
59
68
  it {
60
- expect { mapper.db_load({ 'tags' => 1 }) }.to raise_error(ReeMapper::TypeError)
69
+ expect { mapper.db_load({ 'tags' => 1 }) }.to raise_error(ReeMapper::TypeError, "`tags` should be an array")
61
70
  }
62
71
  end
63
72
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe ReeMapper::Bool do
3
+ RSpec.describe 'ReeMapper::Bool' do
4
4
  link :build_mapper_factory, from: :ree_mapper
5
5
  link :build_mapper_strategy, from: :ree_mapper
6
6
 
@@ -31,11 +31,11 @@ RSpec.describe ReeMapper::Bool do
31
31
  }
32
32
 
33
33
  it {
34
- expect { mapper.serialize({ bool: 'true' }) }.to raise_error(ReeMapper::TypeError)
34
+ expect { mapper.serialize({ bool: 'true' }) }.to raise_error(ReeMapper::TypeError, "`bool` should be a boolean")
35
35
  }
36
36
 
37
37
  it {
38
- expect { mapper.serialize({ bool: 1 }) }.to raise_error(ReeMapper::TypeError)
38
+ expect { mapper.serialize({ bool: 1 }) }.to raise_error(ReeMapper::TypeError, "`bool` should be a boolean")
39
39
  }
40
40
  end
41
41
 
@@ -81,11 +81,11 @@ RSpec.describe ReeMapper::Bool do
81
81
  }
82
82
 
83
83
  it {
84
- expect { mapper.cast({ 'bool' => 'right' }) }.to raise_error(ReeMapper::CoercionError)
84
+ expect { mapper.cast({ 'bool' => 'right' }) }.to raise_error(ReeMapper::CoercionError, "`bool` is invalid boolean")
85
85
  }
86
86
 
87
87
  it {
88
- expect { mapper.cast({ 'bool' => Object.new }) }.to raise_error(ReeMapper::CoercionError)
88
+ expect { mapper.cast({ 'bool' => Object.new }) }.to raise_error(ReeMapper::CoercionError, "`bool` is invalid boolean")
89
89
  }
90
90
  end
91
91
 
@@ -99,11 +99,11 @@ RSpec.describe ReeMapper::Bool do
99
99
  }
100
100
 
101
101
  it {
102
- expect { mapper.db_dump(OpenStruct.new({ bool: 'true' })) }.to raise_error(ReeMapper::TypeError)
102
+ expect { mapper.db_dump(OpenStruct.new({ bool: 'true' })) }.to raise_error(ReeMapper::TypeError, "`bool` should be a boolean")
103
103
  }
104
104
 
105
105
  it {
106
- expect { mapper.db_dump(OpenStruct.new({ bool: 1 })) }.to raise_error(ReeMapper::TypeError)
106
+ expect { mapper.db_dump(OpenStruct.new({ bool: 1 })) }.to raise_error(ReeMapper::TypeError, "`bool` should be a boolean")
107
107
  }
108
108
  end
109
109
 
@@ -149,11 +149,11 @@ RSpec.describe ReeMapper::Bool do
149
149
  }
150
150
 
151
151
  it {
152
- expect { mapper.db_load({ 'bool' => 'right' }) }.to raise_error(ReeMapper::CoercionError)
152
+ expect { mapper.db_load({ 'bool' => 'right' }) }.to raise_error(ReeMapper::CoercionError, "`bool` is invalid boolean")
153
153
  }
154
154
 
155
155
  it {
156
- expect { mapper.db_load({ 'bool' => Object.new }) }.to raise_error(ReeMapper::CoercionError)
156
+ expect { mapper.db_load({ 'bool' => Object.new }) }.to raise_error(ReeMapper::CoercionError, "`bool` is invalid boolean")
157
157
  }
158
158
  end
159
159
  end