• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fast-programmer / outboxer / 14571123183

21 Apr 2025 09:30AM UTC coverage: 98.335% (+0.07%) from 98.261%
14571123183

Pull #272

github

web-flow
Merge ba354684e into d8ccd6fd4
Pull Request #272: Optimise EventCreatedJob.perform_async

69 of 69 new or added lines in 2 files covered. (100.0%)

1595 of 1622 relevant lines covered (98.34%)

6.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/app/jobs/event_created_job.rb
1
class EventCreatedJob
1✔
2
  include Sidekiq::Job
1✔
3

4
  def self.perform_async(*args)
1✔
5
    can_handle?(type: args[0]["type"]) ? super : nil
14✔
6
  end
7

8
  def perform(args)
1✔
9
    job_class_name = to_job_class_name(type: args["type"])
21✔
10

11
    if job_class_name.nil?
21✔
12
      Sidekiq.logger.debug("Could not get job class name from event type: #{args["type"]}")
6✔
13

14
      return
6✔
15
    end
16

17
    job_class = job_class_name.safe_constantize
15✔
18

19
    if job_class.nil?
15✔
20
      Sidekiq.logger.debug("Could not constantize job class name: #{job_class_name}")
3✔
21

22
      return
3✔
23
    end
24

25
    job_class.perform_async(args["id"])
12✔
26
  end
27

28
  TYPE_REGEX = /\A(::)?([A-Z][\w]*::)*[A-Z][\w]*Event\z/
1✔
29

30
  def to_job_class_name(type:)
1✔
31
    self.class.can_handle?(type: type) ? type.sub(/Event\z/, "Job") : nil
21✔
32
  end
33

34
  def self.can_handle?(type:)
1✔
35
    # \A            => start of string
36
    # (::)?         => optional leading ::
37
    # ([A-Z]\w*::)* => zero or more namespace segments like Foo:: or FooBar::
38
    # [A-Z]\w*      => final constant segment (starts with capital letter)
39
    # Event\z       => ends with 'Event', anchors to end of string
40
    #
41
    # Valid examples:
42
    #   ✔ ContactCreatedEvent
43
    #   ✔ ::ContactCreatedEvent
44
    #   ✔ Accountify::ContactCreatedEvent
45
    #   ✔ Accountify::Invoice::CreatedEvent
46
    #   ✔ MyApp::Domain::Event::UserSignedUpEvent
47
    #
48
    # Invalid examples:
49
    #   ✘ contactCreatedEvent          # starts lowercase
50
    #   ✘ ContactCreated               # missing 'Event'
51
    #   ✘ Event                        # just 'Event'
52
    #   ✘ 123::InvalidEvent            # starts with digit
53
    #   ✘ Foo::                        # incomplete constant
54

55
    /\A(::)?([A-Z][\w]*::)*[A-Z][\w]*Event\z/.match?(type)
35✔
56
  end
57
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc