ree_lib 1.0.93 → 1.0.95

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -0
  3. data/Gemfile.lock +41 -36
  4. data/lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_array.rb +34 -26
  5. data/lib/ree_lib/packages/ree_dao/package/ree_dao/wrappers/pg_jsonb.rb +35 -25
  6. data/lib/ree_lib/packages/ree_enum/package/ree_enum/base_enum_mapper.rb +6 -18
  7. data/lib/ree_lib/packages/ree_enum/package/ree_enum/integer_value_enum_mapper.rb +6 -18
  8. data/lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb +6 -18
  9. data/lib/ree_lib/packages/ree_errors/Package.schema.json +10 -7
  10. data/lib/ree_lib/packages/ree_errors/package/ree_errors/auth_error.rb +8 -8
  11. data/lib/ree_lib/packages/ree_errors/package/ree_errors/build_error.rb +4 -2
  12. data/lib/ree_lib/packages/ree_errors/package/ree_errors/conflict_error.rb +8 -8
  13. data/lib/ree_lib/packages/ree_errors/package/ree_errors/error.rb +89 -6
  14. data/lib/ree_lib/packages/ree_errors/package/ree_errors/invalid_param_error.rb +9 -9
  15. data/lib/ree_lib/packages/ree_errors/package/ree_errors/not_found_error.rb +8 -8
  16. data/lib/ree_lib/packages/ree_errors/package/ree_errors/payment_required_error.rb +8 -8
  17. data/lib/ree_lib/packages/ree_errors/package/ree_errors/permission_error.rb +8 -8
  18. data/lib/ree_lib/packages/ree_errors/package/ree_errors/validation_error.rb +8 -8
  19. data/lib/ree_lib/packages/ree_errors/package/ree_errors.rb +1 -0
  20. data/lib/ree_lib/packages/ree_errors/schemas/ree_errors/auth_error.schema.json +24 -1
  21. data/lib/ree_lib/packages/ree_errors/schemas/ree_errors/build_error.schema.json +10 -0
  22. data/lib/ree_lib/packages/ree_errors/schemas/ree_errors/conflict_error.schema.json +24 -1
  23. data/lib/ree_lib/packages/ree_errors/schemas/ree_errors/invalid_param_error.schema.json +24 -1
  24. data/lib/ree_lib/packages/ree_errors/schemas/ree_errors/not_found_error.schema.json +24 -1
  25. data/lib/ree_lib/packages/ree_errors/schemas/ree_errors/payment_required_error.schema.json +24 -1
  26. data/lib/ree_lib/packages/ree_errors/schemas/ree_errors/permission_error.schema.json +24 -1
  27. data/lib/ree_lib/packages/ree_errors/schemas/ree_errors/validation_error.schema.json +24 -1
  28. data/lib/ree_lib/packages/ree_errors/spec/ree_errors/auth_error_spec.rb +22 -8
  29. data/lib/ree_lib/packages/ree_errors/spec/ree_errors/invalid_param_error_spec.rb +3 -1
  30. data/lib/ree_lib/packages/ree_errors/spec/ree_errors/locales/en.yml +6 -1
  31. data/lib/ree_lib/packages/ree_errors/spec/ree_errors/not_found_error_spec.rb +3 -1
  32. data/lib/ree_lib/packages/ree_errors/spec/ree_errors/payment_required_error_spec.rb +3 -1
  33. data/lib/ree_lib/packages/ree_errors/spec/ree_errors/permission_error_spec.rb +3 -1
  34. data/lib/ree_lib/packages/ree_errors/spec/ree_errors/validation_error_spec.rb +3 -1
  35. data/lib/ree_lib/packages/ree_i18n/Package.schema.json +7 -0
  36. data/lib/ree_lib/packages/ree_i18n/package/ree_i18n/functions/check_locale_exists.rb +12 -0
  37. data/lib/ree_lib/packages/ree_i18n/schemas/ree_i18n/functions/check_locale_exists.schema.json +33 -0
  38. data/lib/ree_lib/packages/ree_i18n/spec/ree_i18n/functions/check_locale_exists.rb +16 -0
  39. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/errors/error_with_location.rb +47 -14
  40. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/field.rb +1 -1
  41. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/fields_filter.rb +11 -20
  42. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper.rb +36 -48
  43. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/abstract_type.rb +2 -2
  44. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/any.rb +8 -8
  45. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb +12 -12
  46. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date.rb +14 -14
  47. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb +13 -13
  48. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb +14 -14
  49. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb +13 -13
  50. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/rational.rb +14 -14
  51. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/string.rb +12 -12
  52. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/time.rb +13 -13
  53. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/wrappers/array.rb +88 -80
  54. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/benchmarks/mapper_benchmark_spec.rb +41 -1
  55. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/mapper_factory_spec.rb +17 -6
  56. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb +1 -1
  57. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/type_options_spec.rb +7 -26
  58. data/lib/ree_lib/packages/ree_roda/spec/ree_roda/services/build_swagger_from_routes_spec.rb +7 -4
  59. data/lib/ree_lib/packages/ree_roda/spec/ree_roda/services/locales/en.yml +3 -1
  60. data/lib/ree_lib/packages/ree_string/package/ree_string/functions/deconstantize.rb +1 -1
  61. data/lib/ree_lib/packages/ree_swagger/Package.schema.json +3 -0
  62. data/lib/ree_lib/packages/ree_swagger/package/ree_swagger/functions/build_parameters.rb +2 -1
  63. data/lib/ree_lib/packages/ree_swagger/package/ree_swagger/functions/build_request_body_schema.rb +12 -5
  64. data/lib/ree_lib/packages/ree_swagger/package/ree_swagger/functions/build_serializer_schema.rb +12 -5
  65. data/lib/ree_lib/packages/ree_swagger/package/ree_swagger.rb +1 -0
  66. data/lib/ree_lib/packages/ree_swagger/schemas/ree_swagger/functions/build_parameters.schema.json +8 -0
  67. data/lib/ree_lib/packages/ree_swagger/schemas/ree_swagger/functions/build_request_body_schema.schema.json +1 -1
  68. data/lib/ree_lib/packages/ree_swagger/schemas/ree_swagger/functions/build_serializer_schema.schema.json +1 -1
  69. data/lib/ree_lib/version.rb +1 -1
  70. metadata +5 -3
  71. data/lib/ree_lib/packages/ree_errors/package/ree_errors/error_factory.rb +0 -15
@@ -1,8 +1,10 @@
1
1
  package_require('ree_errors/validation_error')
2
2
 
3
3
  RSpec.describe ReeErrors::ValidationError do
4
+ link :validation_error, from: :ree_errors
5
+
4
6
  it {
5
- klass = described_class.build(:code)
7
+ klass = validation_error(:code)
6
8
  error = klass.new('message')
7
9
 
8
10
  expect(klass).to be_a(Class)
@@ -25,6 +25,13 @@
25
25
  "fn"
26
26
  ]
27
27
  },
28
+ {
29
+ "name": "check_locale_exists",
30
+ "schema": "packages/ree_i18n/schemas/ree_i18n/functions/check_locale_exists.schema.json",
31
+ "tags": [
32
+ "fn"
33
+ ]
34
+ },
28
35
  {
29
36
  "name": "get_default_locale",
30
37
  "schema": "packages/ree_i18n/schemas/ree_i18n/functions/get_default_locale.schema.json",
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ReeI18n::CheckLocaleExists
4
+ include Ree::FnDSL
5
+
6
+ fn :check_locale_exists
7
+
8
+ contract(String, Nilor[Or[String, Symbol]] => Bool)
9
+ def call(value, locale = nil)
10
+ I18n.exists?(value, locale || I18n.default_locale)
11
+ end
12
+ end
@@ -0,0 +1,33 @@
1
+ {
2
+ "schema_type": "object",
3
+ "schema_version": "1.1",
4
+ "name": "check_locale_exists",
5
+ "path": "packages/ree_i18n/package/ree_i18n/functions/check_locale_exists.rb",
6
+ "mount_as": "fn",
7
+ "class": "ReeI18n::CheckLocaleExists",
8
+ "factory": null,
9
+ "methods": [
10
+ {
11
+ "doc": "",
12
+ "throws": [
13
+
14
+ ],
15
+ "return": "Bool",
16
+ "args": [
17
+ {
18
+ "arg": "value",
19
+ "arg_type": "req",
20
+ "type": "String"
21
+ },
22
+ {
23
+ "arg": "locale",
24
+ "arg_type": "opt",
25
+ "type": "Nilor[Or[String, Symbol]]"
26
+ }
27
+ ]
28
+ }
29
+ ],
30
+ "links": [
31
+
32
+ ]
33
+ }
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe :check_locale_exists do
4
+ link :set_locale, from: :ree_i18n
5
+ link :get_locale, from: :ree_i18n
6
+ link :add_load_path, from: :ree_i18n
7
+
8
+ it {
9
+ add_load_path(Dir[File.join(__dir__, 'locales/*.yml')])
10
+
11
+ expect(check_locale_exists("count.zero", "ru")).to eq(true)
12
+ expect(check_locale_exists("count.zero")).to eq(true)
13
+ expect(check_locale_exists("count.zero", :fr)).to eq(false)
14
+ expect(check_locale_exists("count.none", :ru)).to eq(false)
15
+ }
16
+ end
@@ -1,25 +1,58 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ReeMapper::ErrorWithLocation < ReeMapper::Error
4
- attr_reader :location
4
+ attr_accessor :location
5
5
 
6
- def initialize(message, location = nil)
7
- if message.is_a?(String) && location && ENV["RUBY_ENV"] == "test"
8
- message = "#{message}, located at #{location}"
9
- end
10
-
11
- super(message)
6
+ contract(String, String, ArrayOf[String] => Any)
7
+ def initialize(message, location = nil, field_name_parts = [])
8
+ @message = message
12
9
  @location = location
10
+ @field_name_parts = field_name_parts
13
11
  end
14
12
 
15
- def full_message(...)
16
- msg = super
17
- return msg if location.nil?
13
+ contract(String => nil)
14
+ def prepend_field_name(part)
15
+ @field_name_parts.unshift part
16
+ nil
17
+ end
18
+
19
+ contract(None => Nilor[String])
20
+ def field_name
21
+ @field_name_parts.reduce { "#{_1}[#{_2}]" }
22
+ end
23
+
24
+ if ENV["RUBY_ENV"] == "test"
25
+
26
+ contract(None => String)
27
+ def message
28
+ msg = @message
29
+
30
+ if location
31
+ msg = "#{msg}, located at #{location}"
32
+ end
33
+
34
+ return msg if @field_name_parts.empty?
18
35
 
19
- idx = msg.index(/\).*\n/)
20
- return msg if idx.nil?
21
- return msg if ENV["RUBY_ENV"] == "test"
36
+ "`#{field_name}` #{msg}"
37
+ end
38
+
39
+ else
40
+
41
+ def message
42
+ return @message if @field_name_parts.empty?
43
+
44
+ "`#{field_name}` #{@message}"
45
+ end
46
+
47
+ def full_message(...)
48
+ msg = super
49
+ return msg if location.nil?
50
+
51
+ last_sym_idx = msg.index(/\).*\n/)
52
+ return msg if last_sym_idx.nil?
53
+
54
+ msg.insert(last_sym_idx + 1, ", located at #{location}")
55
+ end
22
56
 
23
- msg.insert(idx + 1, ", located at #{location}")
24
57
  end
25
58
  end
@@ -33,7 +33,7 @@ class ReeMapper::Field
33
33
  @roles = Array(role)
34
34
  @default = default
35
35
 
36
- @fields_filter = ReeMapper::FieldsFilter.build(only: only, except: except)
36
+ @fields_filter = ReeMapper::FieldsFilter.build(only, except)
37
37
 
38
38
  @name_as_str = @name.to_s
39
39
  @name_as_instance_var_name = :"@#{@name}"
@@ -55,35 +55,26 @@ class ReeMapper::FieldsFilter
55
55
  attr_reader :fields
56
56
  end
57
57
 
58
- class NoneStrategy
59
- def self.allow?(field)
60
- true
61
- end
62
- end
63
-
64
- def self.empty_filter
65
- @empty_filter ||= new(NoneStrategy, {}).freeze
66
- end
67
-
68
- contract Nilor[ReeMapper::FilterFieldsContract], Nilor[ReeMapper::FilterFieldsContract] => Any
69
- def self.build(only:, except:)
70
- return empty_filter if only.nil? && except.nil?
71
-
58
+ contract(
59
+ Nilor[ReeMapper::FilterFieldsContract],
60
+ Nilor[ReeMapper::FilterFieldsContract] => Nilor[ReeMapper::FieldsFilter]
61
+ )
62
+ def self.build(only, except)
72
63
  strategy = if !only.nil?
73
64
  OnlyStrategy.new(only, except)
74
65
  elsif !except.nil?
75
66
  ExceptStrategy.new(except)
76
67
  else
77
- NoneStrategy
68
+ return nil
78
69
  end
79
70
 
80
71
  nested_fields_filters = {}
81
-
72
+
82
73
  only = only&.select { _1.is_a? Hash }&.reduce(&:merge)
83
74
  except = except&.select { _1.is_a? Hash }&.reduce(&:merge)
84
75
 
85
- only&.each { nested_fields_filters[_1] = build(only: _2, except: except&.dig(_1)) }
86
- except&.each { nested_fields_filters[_1] ||= build(only: nil, except: _2) }
76
+ only&.each { nested_fields_filters[_1] = build(_2, except&.dig(_1)) }
77
+ except&.each { nested_fields_filters[_1] ||= build(nil, _2) }
87
78
 
88
79
  new(strategy, nested_fields_filters)
89
80
  end
@@ -98,9 +89,9 @@ class ReeMapper::FieldsFilter
98
89
  end
99
90
 
100
91
  def filter_for(field)
101
- nested_fields_filters.fetch(field, self.class.empty_filter)
92
+ nested_fields_filters[field]
102
93
  end
103
94
 
104
95
  private
105
96
  attr_reader :strategy, :nested_fields_filters
106
- end
97
+ end
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ReeMapper::Mapper
4
- EMPTY_ARY = [].freeze
5
-
6
4
  contract(
7
5
  ArrayOf[ReeMapper::MapperStrategy],
8
6
  Nilor[ReeMapper::AbstractType, ReeMapper::AbstractWrapper] => self
@@ -25,77 +23,67 @@ class ReeMapper::Mapper
25
23
 
26
24
  if type
27
25
  class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
28
- def #{method}(obj, name: nil, role: nil, only: nil, except: nil, fields_filters: EMPTY_ARY, location: nil)
26
+ def #{method}(obj, role: nil, only: nil, except: nil, fields_filters: nil)
29
27
  #{
30
28
  if type.is_a?(ReeMapper::AbstractWrapper)
31
- "@type.#{method}(obj, name: name, role: role, fields_filters: fields_filters, location: location)"
29
+ "@type.#{method}(obj, role:, fields_filters:)"
32
30
  else
33
- "@type.#{method}(obj, name: name, location: location)"
31
+ "@type.#{method}(obj)"
34
32
  end
35
33
  }
36
34
  end
37
35
  RUBY
38
36
  else
39
37
  class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
40
- def #{method}(obj, name: nil, role: nil, only: nil, except: nil, fields_filters: EMPTY_ARY, location: nil)
41
- if only && !ReeMapper::FilterFieldsContract.valid?(only)
42
- raise ReeMapper::ArgumentError, "Invalid `only` format"
43
- end
44
-
45
- if except && !ReeMapper::FilterFieldsContract.valid?(except)
46
- raise ReeMapper::ArgumentError, "Invalid `except` format"
47
- end
38
+ def #{method}(obj, role: nil, only: nil, except: nil, fields_filters: nil)
39
+ user_fields_filter = ReeMapper::FieldsFilter.build(only, except)
48
40
 
49
- user_fields_filter = ReeMapper::FieldsFilter.build(only: only, except: except)
50
-
51
- @fields.each_with_object(@#{method}_strategy.build_object) do |(_, field), acc|
52
- field_fields_filters = if user_fields_filter == ReeMapper::FieldsFilter::NoneStrategy
53
- fields_filters
41
+ if !user_fields_filter.nil?
42
+ fields_filters = if fields_filters.nil?
43
+ [user_fields_filter]
54
44
  else
55
45
  fields_filters + [user_fields_filter]
56
46
  end
47
+ end
57
48
 
58
- next unless field_fields_filters.all? { _1.allow? field.name }
49
+ @fields.each_with_object(@#{method}_strategy.build_object) do |(_, field), acc|
50
+ next unless fields_filters.nil? || fields_filters.all? { _1.allow? field.name }
59
51
  next unless field.has_role?(role)
60
52
 
61
- is_with_value = @#{method}_strategy.has_value?(obj, field)
62
- is_optional = field.optional || @#{method}_strategy.always_optional
63
-
64
- if !is_with_value && !is_optional
65
- raise ReeMapper::TypeError.new(
66
- "Missing required field `\#{field.from_as_str}` for `\#{name || 'root'}`",
67
- field.location
68
- )
69
- end
70
-
71
- next if !is_with_value && !field.has_default?
72
-
73
- value = if is_with_value
53
+ value = if @#{method}_strategy.has_value?(obj, field)
74
54
  @#{method}_strategy.get_value(obj, field)
75
55
  else
56
+ if !field.optional && !@#{method}_strategy.always_optional
57
+ raise ReeMapper::TypeError.new(
58
+ "is missing required field",
59
+ field.location,
60
+ [field.from_as_str]
61
+ )
62
+ end
63
+
64
+ next unless field.has_default?
65
+
76
66
  field.default
77
67
  end
78
68
 
79
- unless value.nil? && field.null
80
- nested_name = name ? "\#{name}[\#{field.name_as_str}]" : field.name_as_str
69
+ if !value.nil? || !field.null
70
+ nested_fields_filters = fields_filters&.filter_map { _1.filter_for(field.name) }
81
71
 
82
- nested_fields_filters = if field_fields_filters.empty?
83
- field_fields_filters
84
- else
85
- field_fields_filters.map { _1.filter_for(field.name) }
72
+ if field.fields_filter
73
+ nested_fields_filters = if nested_fields_filters
74
+ nested_fields_filters + [field.fields_filter]
75
+ else
76
+ [field.fields_filter]
77
+ end
86
78
  end
87
79
 
88
- if field.fields_filter != ReeMapper::FieldsFilter::NoneStrategy
89
- nested_fields_filters += [field.fields_filter]
80
+ value = begin
81
+ field.type.#{method}(value, role:, fields_filters: nested_fields_filters)
82
+ rescue ReeMapper::ErrorWithLocation => e
83
+ e.prepend_field_name field.name_as_str
84
+ e.location ||= field.location
85
+ raise e
90
86
  end
91
-
92
- value = field.type.#{method}(
93
- value,
94
- name: nested_name,
95
- role: role,
96
- fields_filters: nested_fields_filters,
97
- location: field.location,
98
- )
99
87
  end
100
88
 
101
89
  @#{method}_strategy.assign_value(acc, field, value)
@@ -2,7 +2,7 @@ class ReeMapper::AbstractType
2
2
  private
3
3
 
4
4
  def truncate(str, limit = 180)
5
- @trancator ||= ReeString::Truncate.new
6
- @trancator.call(str, limit)
5
+ @truncate ||= ReeString::Truncate.new
6
+ @truncate.(str, limit)
7
7
  end
8
8
  end
@@ -1,23 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ReeMapper::Any < ReeMapper::AbstractType
4
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Any)
5
- def serialize(value, name:, location: nil)
4
+ contract(Any => Any)
5
+ def serialize(value)
6
6
  value
7
7
  end
8
8
 
9
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Any)
10
- def cast(value, name:, location: nil)
9
+ contract(Any => Any)
10
+ def cast(value)
11
11
  value
12
12
  end
13
13
 
14
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Any)
15
- def db_dump(value, name:, location: nil)
14
+ contract(Any => Any)
15
+ def db_dump(value)
16
16
  value
17
17
  end
18
18
 
19
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Any)
20
- def db_load(value, name:, location: nil)
19
+ contract(Any => Any)
20
+ def db_load(value)
21
21
  value
22
22
  end
23
23
  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[name: String, location: Nilor[String]] => Bool).throws(ReeMapper::TypeError)
8
- def serialize(value, name:, location: nil)
7
+ contract(Any => Bool).throws(ReeMapper::TypeError)
8
+ def serialize(value)
9
9
  if value.is_a?(TrueClass) || value.is_a?(FalseClass)
10
10
  value
11
11
  else
12
- raise ReeMapper::TypeError.new("`#{name}` should be a boolean, got `#{truncate(value.inspect)}`", location)
12
+ raise ReeMapper::TypeError.new("should be a boolean, got `#{truncate(value.inspect)}`")
13
13
  end
14
14
  end
15
15
 
16
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Bool).throws(ReeMapper::CoercionError)
17
- def cast(value, name:, location: nil)
16
+ contract(Any => Bool).throws(ReeMapper::CoercionError)
17
+ def cast(value)
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.new("`#{name}` is invalid boolean, got `#{truncate(value.inspect)}`", location)
23
+ raise ReeMapper::CoercionError.new("is invalid boolean, got `#{truncate(value.inspect)}`")
24
24
  end
25
25
  end
26
26
 
27
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Bool).throws(ReeMapper::TypeError)
28
- def db_dump(value, name:, location: nil)
29
- serialize(value, name: name, location: location)
27
+ contract(Any => Bool)
28
+ def db_dump(value)
29
+ serialize(value)
30
30
  end
31
31
 
32
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Bool).throws(ReeMapper::CoercionError)
33
- def db_load(value, name:, location: nil)
34
- cast(value, name: name, location: location)
32
+ contract(Any => Bool)
33
+ def db_load(value)
34
+ cast(value)
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[name: String, location: Nilor[String]] => Date).throws(ReeMapper::TypeError)
7
- def serialize(value, name:, location: nil)
6
+ contract(Any => Date).throws(ReeMapper::TypeError)
7
+ def serialize(value)
8
8
  if value.class == Date
9
9
  value
10
10
  else
11
- raise ReeMapper::TypeError.new("`#{name}` should be a date, got `#{truncate(value.inspect)}`", location)
11
+ raise ReeMapper::TypeError.new("should be a date, got `#{truncate(value.inspect)}`")
12
12
  end
13
13
  end
14
14
 
15
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Date).throws(ReeMapper::TypeError, ReeMapper::CoercionError)
16
- def cast(value, name:, location: nil)
15
+ contract(Any => Date).throws(ReeMapper::TypeError, ReeMapper::CoercionError)
16
+ def cast(value)
17
17
  if value.class == Date
18
18
  value
19
19
  elsif value.class == DateTime || value.class == Time
@@ -21,21 +21,21 @@ class ReeMapper::Date < ReeMapper::AbstractType
21
21
  elsif value.is_a?(String)
22
22
  begin
23
23
  Date.parse(value)
24
- rescue ArgumentError => e
25
- raise ReeMapper::CoercionError.new("`#{name}` is invalid date, got `#{truncate(value.inspect)}`", location)
24
+ rescue ArgumentError
25
+ raise ReeMapper::CoercionError.new("is invalid date, got `#{truncate(value.inspect)}`")
26
26
  end
27
27
  else
28
- raise ReeMapper::TypeError.new("`#{name}` should be a date, got `#{truncate(value.inspect)}`", location)
28
+ raise ReeMapper::TypeError.new("should be a date, got `#{truncate(value.inspect)}`")
29
29
  end
30
30
  end
31
31
 
32
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Date).throws(ReeMapper::TypeError)
33
- def db_dump(value, name:, location: nil)
34
- serialize(value, name: name, location: location)
32
+ contract(Any => Date).throws(ReeMapper::TypeError)
33
+ def db_dump(value)
34
+ serialize(value)
35
35
  end
36
36
 
37
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Date).throws(ReeMapper::TypeError, ReeMapper::CoercionError)
38
- def db_load(value, name:, location: nil)
39
- cast(value, name: name, location: location)
37
+ contract(Any => Date).throws(ReeMapper::TypeError, ReeMapper::CoercionError)
38
+ def db_load(value)
39
+ cast(value)
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[name: String, location: Nilor[String]] => DateTime).throws(ReeMapper::TypeError)
7
- def serialize(value, name:, location: nil)
6
+ contract(Any => DateTime).throws(ReeMapper::TypeError)
7
+ def serialize(value)
8
8
  if value.class == DateTime
9
9
  value
10
10
  else
11
- raise ReeMapper::TypeError.new("`#{name}` should be a datetime, got `#{truncate(value.inspect)}`", location)
11
+ raise ReeMapper::TypeError.new("should be a datetime, got `#{truncate(value.inspect)}`")
12
12
  end
13
13
  end
14
14
 
15
- contract(Any, Kwargs[name: String, location: Nilor[String]] => DateTime).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
16
- def cast(value, name:, location: nil)
15
+ contract(Any => DateTime).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
16
+ def cast(value)
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.new("`#{name}` is invalid datetime, got `#{truncate(value.inspect)}`", location)
25
+ raise ReeMapper::CoercionError.new("is invalid datetime, got `#{truncate(value.inspect)}`")
26
26
  end
27
27
  else
28
- raise ReeMapper::TypeError.new("`#{name}` should be a datetime, got `#{truncate(value.inspect)}`", location)
28
+ raise ReeMapper::TypeError.new("should be a datetime, got `#{truncate(value.inspect)}`")
29
29
  end
30
30
  end
31
31
 
32
- contract(Any, Kwargs[name: String, location: Nilor[String]] => DateTime).throws(ReeMapper::TypeError)
33
- def db_dump(value, name:, location: nil)
34
- serialize(value, name: name, location: location)
32
+ contract(Any => DateTime)
33
+ def db_dump(value)
34
+ serialize(value)
35
35
  end
36
36
 
37
- contract(Any, Kwargs[name: String, location: Nilor[String]] => DateTime).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
38
- def db_load(value, name:, location: nil)
39
- cast(value, name: name, location: location)
37
+ contract(Any => DateTime)
38
+ def db_load(value)
39
+ cast(value)
40
40
  end
41
41
  end
@@ -1,39 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ReeMapper::Float < ReeMapper::AbstractType
4
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Float).throws(ReeMapper::TypeError)
5
- def serialize(value, name:, location: nil)
4
+ contract(Any => Float).throws(ReeMapper::TypeError)
5
+ def serialize(value)
6
6
  if value.is_a?(Float)
7
7
  value
8
8
  else
9
- raise ReeMapper::TypeError.new("`#{name}` should be a float, got `#{truncate(value.inspect)}`", location)
9
+ raise ReeMapper::TypeError.new("should be a float, got `#{truncate(value.inspect)}`")
10
10
  end
11
11
  end
12
12
 
13
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Float).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
14
- def cast(value, name:, location: nil)
13
+ contract(Any => Float).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
14
+ def cast(value)
15
15
  if value.is_a?(Numeric)
16
16
  value.to_f
17
17
  elsif value.is_a?(String)
18
18
  begin
19
19
  Float(value)
20
- rescue ArgumentError => e
21
- raise ReeMapper::CoercionError.new("`#{name}` is invalid float, got `#{truncate(value.inspect)}`", location)
20
+ rescue ArgumentError
21
+ raise ReeMapper::CoercionError.new("is invalid float, got `#{truncate(value.inspect)}`")
22
22
  end
23
23
  elsif defined?(BigDecimal) && value.is_a?(BigDecimal)
24
24
  value.to_f
25
25
  else
26
- raise ReeMapper::TypeError.new("`#{name}` should be a float, got `#{truncate(value.inspect)}`", location)
26
+ raise ReeMapper::TypeError.new("should be a float, got `#{truncate(value.inspect)}`")
27
27
  end
28
28
  end
29
29
 
30
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Float).throws(ReeMapper::CoercionError)
31
- def db_dump(value, name:, location: nil)
32
- serialize(value, name: name, location: location)
30
+ contract(Any => Float).throws(ReeMapper::CoercionError)
31
+ def db_dump(value)
32
+ serialize(value)
33
33
  end
34
34
 
35
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Float).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
36
- def db_load(value, name:, location: nil)
37
- cast(value, name: name, location: location)
35
+ contract(Any => Float).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
36
+ def db_load(value)
37
+ cast(value)
38
38
  end
39
39
  end
@@ -1,37 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ReeMapper::Integer < ReeMapper::AbstractType
4
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Integer).throws(ReeMapper::TypeError)
5
- def serialize(value, name:, location: nil)
4
+ contract(Any => Integer).throws(ReeMapper::TypeError)
5
+ def serialize(value)
6
6
  if value.is_a? Integer
7
7
  value
8
8
  else
9
- raise ReeMapper::TypeError.new("`#{name}` should be an integer, got `#{truncate(value.inspect)}`", location)
9
+ raise ReeMapper::TypeError.new("should be an integer, got `#{truncate(value.inspect)}`")
10
10
  end
11
11
  end
12
12
 
13
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Integer).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
14
- def cast(value, name:, location: nil)
13
+ contract(Any => Integer).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
14
+ def cast(value)
15
15
  if value.is_a?(Integer)
16
16
  value
17
17
  elsif value.is_a?(String)
18
18
  coerced_value = Integer(value, exception: false)
19
19
  if coerced_value.nil?
20
- raise ReeMapper::CoercionError.new("`#{name}` is invalid integer, got `#{truncate(value.inspect)}`", location)
20
+ raise ReeMapper::CoercionError.new("is invalid integer, got `#{truncate(value.inspect)}`")
21
21
  end
22
22
  coerced_value
23
23
  else
24
- raise ReeMapper::TypeError.new("`#{name}` should be an integer, got `#{truncate(value.inspect)}`", location)
24
+ raise ReeMapper::TypeError.new("should be an integer, got `#{truncate(value.inspect)}`")
25
25
  end
26
26
  end
27
27
 
28
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Integer).throws(ReeMapper::TypeError)
29
- def db_dump(value, name:, location: nil)
30
- serialize(value, name: name, location: location)
28
+ contract(Any => Integer).throws(ReeMapper::TypeError)
29
+ def db_dump(value)
30
+ serialize(value)
31
31
  end
32
32
 
33
- contract(Any, Kwargs[name: String, location: Nilor[String]] => Integer).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
34
- def db_load(value, name:, location: nil)
35
- cast(value, name: name, location: location)
33
+ contract(Any => Integer).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
34
+ def db_load(value)
35
+ cast(value)
36
36
  end
37
37
  end