Skip to content

Commit abb7355

Browse files
committed
BYOB
1 parent d1dbf3c commit abb7355

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ Style/StringLiterals:
1515
EnforcedStyle: double_quotes
1616
Gemspec/OrderedDependencies:
1717
Enabled: no
18+
Metrics/BlockLength:
19+
Enabled: no

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,10 @@ If you want to allow multibyte characters, set it explicitly.
145145
ValidEmail2::Address.permitted_multibyte_characters_regex = /[ÆæØøÅåÄäÖöÞþÐð]/
146146
```
147147

148-
If you want to allow load any of the lists dynamically
148+
If you want to allow load any of the lists dynamically:
149149

150150
```ruby
151-
ValidEmail2.reset_lists # reset the content
152-
# make sure to use a Set for speedy access
151+
# make sure to use a Set for speedy access and cache the result
153152
ValidEmail2.disposable_emails = -> { Set.new(['your.com']) }
154153
ValidEmail2.deny_list = -> { Set.new(['data.com']) }
155154
ValidEmail2.allow_list = -> { Set.new(['provided.com']) }

lib/valid_email2.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,27 @@ module ValidEmail2
77
ALLOW_LIST_FILE = "config/allow_listed_email_domains.yml"
88
DISPOSABLE_FILE = File.expand_path('../config/disposable_email_domains.txt', __dir__)
99

10-
class << self
11-
attr_accessor :disposable_proc, :deny_proc, :allow_proc
12-
13-
def reset_lists
14-
@disposable_emails = nil
15-
@deny_list = nil
16-
@allow_list = nil
17-
end
10+
cattr_accessor :disposable_proc do
11+
-> { @disposable_emails || load_file(DISPOSABLE_FILE) }
12+
end
13+
cattr_accessor :deny_proc do
14+
-> { @deny_list ||= load_if_exists(DENY_LIST_FILE) || Set.new }
15+
end
16+
cattr_accessor :allow_proc do
17+
-> { @allow_list ||= load_if_exists(ALLOW_LIST_FILE) || Set.new }
18+
end
1819

20+
class << self
1921
def disposable_emails
20-
@disposable_emails ||= disposable_proc&.call || load_file(DISPOSABLE_FILE)
22+
disposable_proc&.call
2123
end
2224

2325
def deny_list
24-
@deny_list ||= deny_proc&.call || load_if_exists(DENY_LIST_FILE) || Set.new
26+
deny_proc&.call
2527
end
2628

2729
def allow_list
28-
@allow_list ||= allow_proc&.call || load_if_exists(ALLOW_LIST_FILE) || Set.new
30+
allow_proc&.call
2931
end
3032

3133
private

spec/valid_email2_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,19 @@ def set_allow_list
275275
user = TestUserDisallowDenyListed.new(email: "[email protected]")
276276
expect(user.valid?).to be_falsey
277277
end
278+
end
279+
280+
describe "with deny list validation" do
281+
before do
282+
@old_proc = ValidEmail2.deny_proc
283+
end
284+
285+
after do
286+
ValidEmail2.deny_proc = @old_proc
287+
end
278288

279289
it "is invalid if the domain is deny-listed from a proc" do
280-
ValidEmail2.deny_proc = -> { Set.new(['dubbi.com']) }
281-
ValidEmail2.reset_lists
290+
ValidEmail2.deny_proc = -> { @deny_list ||= Set.new(['dubbi.com']) }
282291
user = TestUserDisallowDenyListed.new(email: "[email protected]")
283292
expect(user.valid?).to be_falsey
284293
end

0 commit comments

Comments
 (0)