addressing 0.2.0 → 0.3.0

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: 0f6ea26657e38313826abc6c9dbab406b426b07ac6d03af922ab130d6b00e29f
4
- data.tar.gz: d3037ec9973ae1e7f0054eece0bd3e1a7a683eda4f2c4b1ad04294b3112684c6
3
+ metadata.gz: b46d807aa97c2e089222447115d7862105246f37f850e65f824f8599af5d714d
4
+ data.tar.gz: b2f82f834eb10ead8e14e9b63083093b94a27bdec7a157a7102ee6c01eb15e6a
5
5
  SHA512:
6
- metadata.gz: 6db0b294a6ee4fb0cbb787a86e0fe506c539ea5a94ae7cb170373524540dd44a2139c90d0b6951d34728b42b19a84e3eed60bb7d50e8685ce4b65f9c21bbabb6
7
- data.tar.gz: d6a857986eff573f2e136ccd1a07eb8b0da618ce00cb78af44590708a7dca54a79f1facbd2bd48ab48a9bcd66f7cce919e0c3d906572c8eebc40f12f0b5e976a
6
+ metadata.gz: fb86d0f70a24db611837a597b3f8846a545f9c16d0df2099d6baab0235cc27ff0730b97f55b138b1d946715e3a9a671b6af23544ba59851d6f10b1cde428d9e5
7
+ data.tar.gz: 4b4c5ce9982c8faaf53a6eab5117b8250d17fc3e11ffb34e24e3765ad931b5ad1fbd4031633a132c23c7c4d489131ff5364af22a0d695acad02e349b23414d25
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  All notable changes to `addressing` will be documented in this file.
4
4
 
5
+ ## 0.3.0 (04-04-2022)
6
+
7
+ - Allow field validation to be overridden
8
+ - Only verify address when one of the field changes
9
+ - Allow used address fields to be configured
10
+
5
11
  ## 0.2.0 (25-02-2022)
6
12
 
7
13
  - Add ActiveRecord validations
data/README.md CHANGED
@@ -182,6 +182,14 @@ class User < ApplicationRecord
182
182
  end
183
183
  ```
184
184
 
185
+ For performance, the address is only verified if at least one of the fields changes. Set your own condition with:
186
+
187
+ ```rb
188
+ class User < ApplicationRecord
189
+ validates_address if: -> { something_changed? }, ...
190
+ end
191
+ ```
192
+
185
193
  ## Changelog
186
194
 
187
195
  Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
@@ -141,4 +141,25 @@ module Addressing
141
141
  fields & used_fields
142
142
  end
143
143
  end
144
+
145
+ class AddressFormatHelper
146
+ class << self
147
+ # Gets the required fields.
148
+ #
149
+ # Applies field overrides to the required fields
150
+ # specified by the address format.
151
+ def required_fields(address_format, field_overrides)
152
+ required_fields = address_format.required_fields
153
+ required_fields = required_fields - field_overrides.optional_fields
154
+ required_fields = required_fields - field_overrides.hidden_fields
155
+
156
+ if field_overrides.required_fields
157
+ required_fields = required_fields + field_overrides.required_fields
158
+ required_fields = required_fields.uniq
159
+ end
160
+
161
+ required_fields
162
+ end
163
+ end
164
+ end
144
165
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Addressing
4
+ class FieldOverride < Enum
5
+ HIDDEN = "hidden"
6
+ OPTIONAL = "optional"
7
+ REQUIRED = "required"
8
+ end
9
+
10
+ class FieldOverrides
11
+ attr_reader :hidden_fields, :optional_fields, :required_fields
12
+
13
+ def initialize(definition)
14
+ AddressField.assert_all_exist(definition.keys)
15
+ FieldOverride.assert_all_exist(definition.values)
16
+
17
+ @hidden_fields = []
18
+ @optional_fields = []
19
+ @required_fields = []
20
+
21
+ definition.each do |field, override|
22
+ case override
23
+ when FieldOverride::HIDDEN
24
+ @hidden_fields << field
25
+ when FieldOverride::OPTIONAL
26
+ @optional_fields << field
27
+ when FieldOverride::REQUIRED
28
+ @required_fields << field
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -2,16 +2,20 @@
2
2
 
3
3
  module Addressing
4
4
  module Model
5
- def validates_address_format(**options)
5
+ def validates_address_format(
6
+ fields: [:country_code, :administrative_area, :locality, :dependent_locality, :postal_code, :sorting_code, :address_line1, :address_line2, :organization, :given_name, :additional_name, :family_name, :locale], field_overrides: nil, **options)
7
+ fields = Array(fields)
8
+ field_overrides ||= FieldOverrides.new({})
9
+
10
+ options[:if] ||= -> { fields.any? { |f| changes.key?(f.to_s) } } unless options[:unless]
11
+
6
12
  class_eval do
7
13
  validate :verify_address_format, **options
8
14
 
9
15
  define_method :verify_address_format do
10
- fields = [:country_code, :administrative_area, :locality, :dependent_locality, :postal_code, :sorting_code, :address_line1, :address_line2, :organization, :given_name, :additional_name, :family_name, :locale].each_with_object({}) do |field, fields|
11
- fields[field] = send(field)
12
- end
16
+ values = fields.each_with_object({}) { |f, v| v[f] = send(f) }
17
+ address = Address.new(**values)
13
18
 
14
- address = Address.new(**fields)
15
19
  return unless address.country_code.present?
16
20
 
17
21
  address_format = AddressFormat.get(address.country_code)
@@ -23,14 +27,16 @@ module Addressing
23
27
  address_format.used_fields
24
28
 
25
29
  # Validate the presence of required fields.
26
- address_format.required_fields.each do |required_field|
30
+ AddressFormatHelper::required_fields(address_format, field_overrides).each do |required_field|
27
31
  next unless address.send(required_field).blank?
28
32
 
29
33
  errors.add(required_field, "should not be blank")
30
34
  end
31
35
 
36
+ used_fields = address_format.used_fields - field_overrides.hidden_fields
37
+
32
38
  # Validate the absence of unused fields.
33
- unused_fields = AddressField.all.values - address_format.used_fields
39
+ unused_fields = AddressField.all.values - used_fields
34
40
  unused_fields.each do |unused_field|
35
41
  next if address.send(unused_field).blank?
36
42
 
@@ -41,7 +47,7 @@ module Addressing
41
47
  subdivisions = verify_subdivisions(address, address_format)
42
48
 
43
49
  # Validate postal code.
44
- verify_postal_code(address.postal_code, subdivisions, address_format) if address_format.used_fields.include?(AddressField::POSTAL_CODE)
50
+ verify_postal_code(address.postal_code, subdivisions, address_format) if used_fields.include?(AddressField::POSTAL_CODE)
45
51
  end
46
52
 
47
53
  define_method :verify_subdivisions do |address, address_format|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Addressing
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/addressing.rb CHANGED
@@ -12,6 +12,7 @@ require "addressing/administrative_area_type"
12
12
  require "addressing/country"
13
13
  require "addressing/default_formatter"
14
14
  require "addressing/dependent_locality_type"
15
+ require "addressing/field_override"
15
16
  require "addressing/lazy_subdivisions"
16
17
  require "addressing/locale"
17
18
  require "addressing/locality_type"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: addressing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin van der Vleuten
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-25 00:00:00.000000000 Z
11
+ date: 2022-04-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: robinvdvleuten@gmail.com
@@ -689,6 +689,7 @@ files:
689
689
  - lib/addressing/default_formatter.rb
690
690
  - lib/addressing/dependent_locality_type.rb
691
691
  - lib/addressing/enum.rb
692
+ - lib/addressing/field_override.rb
692
693
  - lib/addressing/lazy_subdivisions.rb
693
694
  - lib/addressing/locale.rb
694
695
  - lib/addressing/locality_type.rb