Class: RailsLens::ERD::DomainColorMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_lens/erd/domain_color_mapper.rb

Overview

Assigns a consistent color to a domain from a predefined palette.

Constant Summary collapse

DEFAULT_COLORS =

Default color palette for domains using CSS named colors.

%w[
  lightblue
  lightcoral
  lightgreen
  lightyellow
  plum
  lightcyan
  lightgray
].freeze
FALLBACK_COLOR =

The color used for domains not found in the initial list.

'lightgray'

Instance Method Summary collapse

Constructor Details

#initialize(domains, colors: DEFAULT_COLORS) ⇒ DomainColorMapper

Returns a new instance of DomainColorMapper.

Parameters:

  • domains (Array<Symbol>)

    A unique list of domain names.

  • colors (Array<String>) (defaults to: DEFAULT_COLORS)

    A list of hex color codes to cycle through.



23
24
25
26
# File 'lib/rails_lens/erd/domain_color_mapper.rb', line 23

def initialize(domains, colors: DEFAULT_COLORS)
  @colors = colors.empty? ? [FALLBACK_COLOR] : colors
  @domain_map = domains.uniq.each_with_index.to_h
end

Instance Method Details

#color_for(domain) ⇒ String

Returns the color for a given domain.

Parameters:

  • domain (Symbol)

    The domain name.

Returns:

  • (String)

    The hex color code.



32
33
34
35
36
37
# File 'lib/rails_lens/erd/domain_color_mapper.rb', line 32

def color_for(domain)
  domain_index = @domain_map[domain]
  return FALLBACK_COLOR unless domain_index

  @colors[domain_index % @colors.length]
end