activemodel 6.1.4.6 → 7.0.0.alpha1

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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +27 -114
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +3 -3
  5. data/lib/active_model/api.rb +99 -0
  6. data/lib/active_model/attribute.rb +4 -0
  7. data/lib/active_model/attribute_methods.rb +99 -52
  8. data/lib/active_model/attribute_set/builder.rb +1 -1
  9. data/lib/active_model/attribute_set.rb +4 -1
  10. data/lib/active_model/attributes.rb +15 -12
  11. data/lib/active_model/callbacks.rb +1 -1
  12. data/lib/active_model/conversion.rb +2 -2
  13. data/lib/active_model/dirty.rb +6 -5
  14. data/lib/active_model/errors.rb +20 -6
  15. data/lib/active_model/gem_version.rb +4 -4
  16. data/lib/active_model/locale/en.yml +1 -0
  17. data/lib/active_model/model.rb +6 -59
  18. data/lib/active_model/naming.rb +15 -8
  19. data/lib/active_model/serialization.rb +7 -2
  20. data/lib/active_model/translation.rb +1 -1
  21. data/lib/active_model/type/helpers/numeric.rb +9 -1
  22. data/lib/active_model/type/helpers/time_value.rb +2 -2
  23. data/lib/active_model/type/integer.rb +4 -1
  24. data/lib/active_model/type/registry.rb +9 -41
  25. data/lib/active_model/type/time.rb +1 -1
  26. data/lib/active_model/type.rb +6 -5
  27. data/lib/active_model/validations/absence.rb +1 -1
  28. data/lib/active_model/validations/clusivity.rb +1 -1
  29. data/lib/active_model/validations/comparability.rb +29 -0
  30. data/lib/active_model/validations/comparison.rb +82 -0
  31. data/lib/active_model/validations/confirmation.rb +4 -4
  32. data/lib/active_model/validations/numericality.rb +27 -20
  33. data/lib/active_model/validations.rb +4 -4
  34. data/lib/active_model/validator.rb +2 -2
  35. data/lib/active_model.rb +2 -1
  36. metadata +17 -14
@@ -1,27 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_model/validations/comparability"
3
4
  require "bigdecimal/util"
4
5
 
5
6
  module ActiveModel
6
7
  module Validations
7
8
  class NumericalityValidator < EachValidator # :nodoc:
8
- CHECKS = { greater_than: :>, greater_than_or_equal_to: :>=,
9
- equal_to: :==, less_than: :<, less_than_or_equal_to: :<=,
10
- odd: :odd?, even: :even?, other_than: :!= }.freeze
9
+ include Comparability
11
10
 
12
- RESERVED_OPTIONS = CHECKS.keys + [:only_integer]
11
+ RANGE_CHECKS = { in: :in? }
12
+ NUMBER_CHECKS = { odd: :odd?, even: :even? }
13
+
14
+ RESERVED_OPTIONS = COMPARE_CHECKS.keys + NUMBER_CHECKS.keys + RANGE_CHECKS.keys + [:only_integer]
13
15
 
14
16
  INTEGER_REGEX = /\A[+-]?\d+\z/
15
17
 
16
18
  HEXADECIMAL_REGEX = /\A[+-]?0[xX]/
17
19
 
18
20
  def check_validity!
19
- keys = CHECKS.keys - [:odd, :even]
20
- options.slice(*keys).each do |option, value|
21
+ options.slice(*COMPARE_CHECKS.keys).each do |option, value|
21
22
  unless value.is_a?(Numeric) || value.is_a?(Proc) || value.is_a?(Symbol)
22
23
  raise ArgumentError, ":#{option} must be a number, a symbol or a proc"
23
24
  end
24
25
  end
26
+
27
+ options.slice(*RANGE_CHECKS.keys).each do |option, value|
28
+ unless value.is_a?(Range)
29
+ raise ArgumentError, ":#{option} must be a range"
30
+ end
31
+ end
25
32
  end
26
33
 
27
34
  def validate_each(record, attr_name, value, precision: Float::DIG, scale: nil)
@@ -37,23 +44,18 @@ module ActiveModel
37
44
 
38
45
  value = parse_as_number(value, precision, scale)
39
46
 
40
- options.slice(*CHECKS.keys).each do |option, option_value|
41
- case option
42
- when :odd, :even
43
- unless value.to_i.public_send(CHECKS[option])
47
+ options.slice(*RESERVED_OPTIONS).each do |option, option_value|
48
+ if NUMBER_CHECKS.include?(option)
49
+ unless value.to_i.public_send(NUMBER_CHECKS[option])
44
50
  record.errors.add(attr_name, option, **filtered_options(value))
45
51
  end
46
- else
47
- case option_value
48
- when Proc
49
- option_value = option_value.call(record)
50
- when Symbol
51
- option_value = record.send(option_value)
52
+ elsif RANGE_CHECKS.include?(option)
53
+ unless value.public_send(RANGE_CHECKS[option], option_value)
54
+ record.errors.add(attr_name, option, **filtered_options(value).merge!(count: option_value))
52
55
  end
53
-
54
- option_value = parse_as_number(option_value, precision, scale)
55
-
56
- unless value.public_send(CHECKS[option], option_value)
56
+ elsif COMPARE_CHECKS.include?(option)
57
+ option_value = option_as_number(record, option_value, precision, scale)
58
+ unless value.public_send(COMPARE_CHECKS[option], option_value)
57
59
  record.errors.add(attr_name, option, **filtered_options(value).merge!(count: option_value))
58
60
  end
59
61
  end
@@ -61,6 +63,10 @@ module ActiveModel
61
63
  end
62
64
 
63
65
  private
66
+ def option_as_number(record, option_value, precision, scale)
67
+ parse_as_number(option_value(record, option_value), precision, scale)
68
+ end
69
+
64
70
  def parse_as_number(raw_value, precision, scale)
65
71
  if raw_value.is_a?(Float)
66
72
  parse_float(raw_value, precision, scale)
@@ -173,6 +179,7 @@ module ActiveModel
173
179
  # supplied value.
174
180
  # * <tt>:odd</tt> - Specifies the value must be an odd number.
175
181
  # * <tt>:even</tt> - Specifies the value must be an even number.
182
+ # * <tt>:in</tt> - Check that the value is within a range.
176
183
  #
177
184
  # There is also a list of default options supported by every validator:
178
185
  # +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+ .
@@ -118,7 +118,7 @@ module ActiveModel
118
118
  # end
119
119
  # end
120
120
  #
121
- # Or with a block where self points to the current record to be validated:
121
+ # Or with a block where +self+ points to the current record to be validated:
122
122
  #
123
123
  # class Comment
124
124
  # include ActiveModel::Validations
@@ -152,7 +152,7 @@ module ActiveModel
152
152
  def validate(*args, &block)
153
153
  options = args.extract_options!
154
154
 
155
- if args.all? { |arg| arg.is_a?(Symbol) }
155
+ if args.all?(Symbol)
156
156
  options.each_key do |k|
157
157
  unless VALID_OPTIONS_FOR_VALIDATE.include?(k)
158
158
  raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{VALID_OPTIONS_FOR_VALIDATE.map(&:inspect).join(', ')}. Perhaps you meant to call `validates` instead of `validate`?")
@@ -272,7 +272,7 @@ module ActiveModel
272
272
  end
273
273
 
274
274
  # Copy validators on inheritance.
275
- def inherited(base) #:nodoc:
275
+ def inherited(base) # :nodoc:
276
276
  dup = _validators.dup
277
277
  base._validators = dup.each { |k, v| dup[k] = v.dup }
278
278
  super
@@ -280,7 +280,7 @@ module ActiveModel
280
280
  end
281
281
 
282
282
  # Clean the +Errors+ object if instance is duped.
283
- def initialize_dup(other) #:nodoc:
283
+ def initialize_dup(other) # :nodoc:
284
284
  @errors = nil
285
285
  super
286
286
  end
@@ -129,7 +129,7 @@ module ActiveModel
129
129
  # record, attribute and value.
130
130
  #
131
131
  # All \Active \Model validations are built on top of this validator.
132
- class EachValidator < Validator #:nodoc:
132
+ class EachValidator < Validator
133
133
  attr_reader :attributes
134
134
 
135
135
  # Returns a new validator instance. All options will be available via the
@@ -174,7 +174,7 @@ module ActiveModel
174
174
 
175
175
  # +BlockValidator+ is a special +EachValidator+ which receives a block on initialization
176
176
  # and call this block for each attribute being validated. +validates_each+ uses this validator.
177
- class BlockValidator < EachValidator #:nodoc:
177
+ class BlockValidator < EachValidator # :nodoc:
178
178
  def initialize(options, &block)
179
179
  @block = block
180
180
  super
data/lib/active_model.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  #--
4
- # Copyright (c) 2004-2020 David Heinemeier Hansson
4
+ # Copyright (c) 2004-2021 David Heinemeier Hansson
5
5
  #
6
6
  # Permission is hereby granted, free of charge, to any person obtaining
7
7
  # a copy of this software and associated documentation files (the
@@ -30,6 +30,7 @@ require "active_model/version"
30
30
  module ActiveModel
31
31
  extend ActiveSupport::Autoload
32
32
 
33
+ autoload :API
33
34
  autoload :Attribute
34
35
  autoload :Attributes
35
36
  autoload :AttributeAssignment
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activemodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.1.4.6
4
+ version: 7.0.0.alpha1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-11 00:00:00.000000000 Z
11
+ date: 2021-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 6.1.4.6
19
+ version: 7.0.0.alpha1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 6.1.4.6
26
+ version: 7.0.0.alpha1
27
27
  description: A toolkit for building modeling frameworks like Active Record. Rich support
28
28
  for attributes, callbacks, validations, serialization, internationalization, and
29
29
  testing.
@@ -36,6 +36,7 @@ files:
36
36
  - MIT-LICENSE
37
37
  - README.rdoc
38
38
  - lib/active_model.rb
39
+ - lib/active_model/api.rb
39
40
  - lib/active_model/attribute.rb
40
41
  - lib/active_model/attribute/user_provided_default.rb
41
42
  - lib/active_model/attribute_assignment.rb
@@ -87,6 +88,8 @@ files:
87
88
  - lib/active_model/validations/acceptance.rb
88
89
  - lib/active_model/validations/callbacks.rb
89
90
  - lib/active_model/validations/clusivity.rb
91
+ - lib/active_model/validations/comparability.rb
92
+ - lib/active_model/validations/comparison.rb
90
93
  - lib/active_model/validations/confirmation.rb
91
94
  - lib/active_model/validations/exclusion.rb
92
95
  - lib/active_model/validations/format.rb
@@ -104,11 +107,11 @@ licenses:
104
107
  - MIT
105
108
  metadata:
106
109
  bug_tracker_uri: https://github.com/rails/rails/issues
107
- changelog_uri: https://github.com/rails/rails/blob/v6.1.4.6/activemodel/CHANGELOG.md
108
- documentation_uri: https://api.rubyonrails.org/v6.1.4.6/
110
+ changelog_uri: https://github.com/rails/rails/blob/v7.0.0.alpha1/activemodel/CHANGELOG.md
111
+ documentation_uri: https://api.rubyonrails.org/v7.0.0.alpha1/
109
112
  mailing_list_uri: https://discuss.rubyonrails.org/c/rubyonrails-talk
110
- source_code_uri: https://github.com/rails/rails/tree/v6.1.4.6/activemodel
111
- post_install_message:
113
+ source_code_uri: https://github.com/rails/rails/tree/v7.0.0.alpha1/activemodel
114
+ post_install_message:
112
115
  rdoc_options: []
113
116
  require_paths:
114
117
  - lib
@@ -116,15 +119,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
119
  requirements:
117
120
  - - ">="
118
121
  - !ruby/object:Gem::Version
119
- version: 2.5.0
122
+ version: 2.7.0
120
123
  required_rubygems_version: !ruby/object:Gem::Requirement
121
124
  requirements:
122
- - - ">="
125
+ - - ">"
123
126
  - !ruby/object:Gem::Version
124
- version: '0'
127
+ version: 1.3.1
125
128
  requirements: []
126
- rubygems_version: 3.2.22
127
- signing_key:
129
+ rubygems_version: 3.1.6
130
+ signing_key:
128
131
  specification_version: 4
129
132
  summary: A toolkit for building modeling frameworks (part of Rails).
130
133
  test_files: []