Class: Yes::Core::Utils::AggregateShortcuts

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

Overview

Provides convenient shortcuts for accessing aggregate classes in Rails console.

Examples:

Multi-capital subjects use capitals-only abbreviations

# Instead of: ApprenticeshipPresentation::ContactInfo::Aggregate.new(id)
# Use: AP::CI.new(id)

Single-capital subjects keep the full name

# Instead of: TaskFlow::Board::Aggregate.new(id)
# Use: TF::Board.new(id)

Class Method Summary collapse

Class Method Details

.display(filter = nil) ⇒ Object

Display shortcuts in a formatted table

Parameters:

  • filter (String, nil) (defaults to: nil)

    Optional filter



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/yes/core/utils/aggregate_shortcuts.rb', line 43

def display(filter = nil)
  shortcuts = list(filter)

  if shortcuts.empty?
    Rails.logger.debug { "No shortcuts found#{" for '#{filter}'" if filter}." }
    return
  end

  max_shortcut_length = shortcuts.keys.map(&:length).max

  Rails.logger.debug "\nAvailable Aggregate Shortcuts:"
  Rails.logger.debug '=' * (max_shortcut_length + 70)
  shortcuts.sort.each do |shortcut, full_path|
    Rails.logger.debug "#{shortcut.ljust(max_shortcut_length)} → #{full_path}"
  end
  Rails.logger.debug '=' * (max_shortcut_length + 70)
  Rails.logger.debug { "\nUsage: #{shortcuts.keys.first}.new(id)" } if shortcuts.any?
end

.list(filter = nil) ⇒ Hash

List all available shortcuts

Examples:

AggregateShortcuts.list
AggregateShortcuts.list('AP')

Parameters:

  • filter (String, nil) (defaults to: nil)

    Optional filter to show only specific context

Returns:

  • (Hash)

    Hash of shortcuts and their full paths



35
36
37
38
39
# File 'lib/yes/core/utils/aggregate_shortcuts.rb', line 35

def list(filter = nil)
  results = @shortcuts || {}
  results = results.select { |shortcut, _| shortcut.start_with?("#{filter}::") } if filter
  results
end

.load!Object

Load aggregate shortcuts in Rails console. Creates fresh shortcut modules (e.g. TF) and assigns aggregate classes as constants on them. Shortcut modules are NOT aliases of the real context modules, so shortcut constants cannot collide with the aggregates’ own namespace modules.



20
21
22
23
24
25
26
27
# File 'lib/yes/core/utils/aggregate_shortcuts.rb', line 20

def load!
  return unless Yes::Core.configuration.aggregate_shortcuts

  load_overrides
  discover_aggregates
  create_shortcuts
  define_helper_method
end