Module: Yes::Core::Utils::CallerUtils

Defined in:
lib/yes/core/utils/caller_utils.rb

Overview

Utility module for handling caller-related operations

Class Method Summary collapse

Class Method Details

.console_originString?

Returns an origin string identifying the Rails console context, or nil if not in a console

Returns:

  • (String, nil)

    origin string like “CompanyManager production console”



27
28
29
30
31
32
# File 'lib/yes/core/utils/caller_utils.rb', line 27

def console_origin
  return unless defined?(Rails::Console)

  app_name = Rails.application.class.module_parent.name
  "#{app_name} #{Rails.env} console"
end

.origin_from_caller(caller) ⇒ String

Extracts a formatted origin string from a caller location

Parameters:

  • caller (Thread::Backtrace::Location)

    caller location

Returns:

  • (String)

    origin of the command, derived from caller



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/yes/core/utils/caller_utils.rb', line 12

def origin_from_caller(caller)
  root_path = defined?(Rails) ? Rails.root.to_s : ''
  # #absolute_path may be nil in case the code is run under irb for example. In this case - grab the script
  # name by calling #path
  caller_path = caller.absolute_path || caller.path
  caller_path.
    sub("#{root_path}/", '').
    sub('.rb', '').
    split('/').
    map { |s| s.split('_').map(&:capitalize).join }.
    join(' > ')
end