Class: Yes::Core::Utils::EventNameResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/yes/core/utils/event_name_resolver.rb

Overview

Resolves event names from command names by converting command verbs to their past tense form

Examples:

EventNameResolver.call('ChangeLocation') # => :location_changed
EventNameResolver.call('AddUser') # => :user_added

Constant Summary collapse

COMMAND_TO_EVENT_VERBS =

Returns Mapping of command verbs to their corresponding event (past tense) forms.

Returns:

  • (Hash<String, String>)

    Mapping of command verbs to their corresponding event (past tense) forms

{
  'Activate' => 'Activated',
  'Add' => 'Added',
  'Approve' => 'Approved',
  'Archive' => 'Archived',
  'Assign' => 'Assigned',
  'Cancel' => 'Cancelled',
  'Change' => 'Changed',
  'Close' => 'Closed',
  'Complete' => 'Completed',
  'Confirm' => 'Confirmed',
  'Deactivate' => 'Deactivated',
  'Delete' => 'Deleted',
  'Disable' => 'Disabled',
  'Enable' => 'Enabled',
  'Fail' => 'Failed',
  'Open' => 'Opened',
  'Publish' => 'Published',
  'Reactivate' => 'Reactivated',
  'Reject' => 'Rejected',
  'Remove' => 'Removed',
  'Reopen' => 'Reopened',
  'Resolve' => 'Resolved',
  'Restore' => 'Restored',
  'Start' => 'Started',
  'Stop' => 'Stopped',
  'Submit' => 'Submitted',
  'Unassign' => 'Unassigned',
  'Unpublish' => 'Unpublished',
  'Update' => 'Updated'
}.freeze

Class Method Summary collapse

Class Method Details

.call(command_name) ⇒ Symbol?

Converts a command name to its corresponding event name

Examples:

EventNameResolver.call('ChangeLocation') # => :location_changed
EventNameResolver.call(:add_user) # => :user_added
EventNameResolver.call('InvalidCommand') # => nil

Parameters:

  • command_name (String, Symbol)

    The name of the command to convert

Returns:

  • (Symbol, nil)

    The converted event name as an underscored symbol, or nil if no conversion is possible



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/yes/core/utils/event_name_resolver.rb', line 51

def self.call(command_name)
  normalized_command_name = command_name.to_s.camelize
  COMMAND_TO_EVENT_VERBS.each do |command_verb, event_verb|
    next unless normalized_command_name.start_with?(command_verb)

    # Extract the subject (e.g. "Location" from "ChangeLocation")
    subject = normalized_command_name.delete_prefix(command_verb)
    # Return subject + verb (e.g. "LocationChanged")
    return "#{subject}#{event_verb}".underscore.to_sym
  end

  nil
end