Class: Ea::Diagram::StyleResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/ea/diagram/style_resolver.rb

Overview

Resolves styles for diagram elements by merging multiple sources

Priority order (highest to lowest):

  1. EA Data from DiagramObject.style (BCol, LCol, etc.)
  2. User Configuration (YAML)
  3. Built-in Defaults

This ensures that:

  • EA's original styling is preserved when present
  • Users can override defaults via configuration
  • Sensible defaults are always available

Constant Summary collapse

CONNECTOR_TYPE_MAP =

Maps UML connector classes to their style type names. New connector types are added here — no method changes needed.

{
  Lutaml::Uml::Generalization => "generalization",
  Lutaml::Uml::Association => "association",
  Lutaml::Uml::Dependency => "dependency",
  Lutaml::Uml::Realization => "realization",
}.freeze
ASSOCIATION_SUBTYPE_MAP =

Association sub-type precedence for style resolution

{
  "aggregation" => "aggregation",
  "composition" => "composition",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path = nil) ⇒ StyleResolver

Initialize with configuration

Parameters:

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

    Path to configuration file



22
23
24
25
# File 'lib/ea/diagram/style_resolver.rb', line 22

def initialize(config_path = nil)
  @configuration = Configuration.new(config_path)
  @style_parser = StyleParser.new
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



17
18
19
# File 'lib/ea/diagram/style_resolver.rb', line 17

def configuration
  @configuration
end

#style_parserObject (readonly)

Returns the value of attribute style_parser.



17
18
19
# File 'lib/ea/diagram/style_resolver.rb', line 17

def style_parser
  @style_parser
end

Instance Method Details

#determine_association_type(connector) ⇒ String

Determine specific association type

Parameters:

  • connector (Object)

    Association connector

Returns:

  • (String)

    Specific association type



264
265
266
267
268
269
270
271
272
273
# File 'lib/ea/diagram/style_resolver.rb', line 264

def determine_association_type(connector)
  return "association" unless connector.is_a?(Lutaml::Uml::Association)

  [connector.owner_end_type, connector.member_end_type].each do |type|
    resolved = ASSOCIATION_SUBTYPE_MAP[type&.downcase]
    return resolved if resolved
  end

  "association"
end

#determine_connector_type(connector) ⇒ String

Determine connector type from connector object

Parameters:

  • connector (Object)

    UML connector

Returns:

  • (String)

    Connector type name



251
252
253
254
255
256
257
258
259
# File 'lib/ea/diagram/style_resolver.rb', line 251

def determine_connector_type(connector)
  return "association" unless connector

  type_name = CONNECTOR_TYPE_MAP[connector.class]
  return type_name if type_name && type_name != "association"
  return determine_association_type(connector) if type_name == "association"

  "association"
end

Parse DiagramLink.style string

Parameters:

  • style_string (String)

    EA style string

Returns:

  • (Hash)

    Parsed style



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ea/diagram/style_resolver.rb', line 201

def parse_diagram_link_style(style_string) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
  return {} unless style_string

  style = {}
  pairs = style_string.split(";")

  pairs.each do |pair|
    key, value = pair.split("=", 2)
    next unless key && value

    case key.strip
    when "LCol"
      # Line color
      style[:stroke] =
        style_parser.color_from_ea_color(value.to_i)
    when "LWth"
      # Line width
      style[:stroke_width] = value.to_i
    when "LStyle"
      # Line style (0=solid, 1=dash, 2=dot, etc.)
      case value.to_i
      when 1
        style[:stroke_dasharray] = "5,5"
      when 2
        style[:stroke_dasharray] = "2,2"
      end
    end
  end

  style
end

#parse_diagram_object_style(style_string) ⇒ Hash

Parse DiagramObject.style string (EA format: "BCol=16764159;LCol=0;SOID=123")

Parameters:

  • style_string (String)

    EA style string

Returns:

  • (Hash)

    Parsed style with fill and stroke colors



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/ea/diagram/style_resolver.rb', line 163

def parse_diagram_object_style(style_string) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  return {} unless style_string

  style = {}
  pairs = style_string.split(";")

  pairs.each do |pair|
    key, value = pair.split("=", 2)
    next unless key && value

    case key.strip
    when "BCol"
      # Background color (BGR integer)
      style[:fill] =
        style_parser.color_from_ea_color(value.to_i)
    when "LCol"
      # Line color (BGR integer)
      style[:stroke] =
        style_parser.color_from_ea_color(value.to_i)
    when "BFol"
      # Bold font (0 or 1)
      style[:font_weight] = value == "1" ? 700 : 400
    when "IFol"
      # Italic font (0 or 1)
      style[:font_style] = value == "1" ? "italic" : "normal"
    when "LWth"
      # Line width
      style[:stroke_width] = value.to_i
    end
  end

  style
end

#resolve_connector_style(connector, diagram_link = nil) ⇒ Hash

Resolve complete style for a connector

(Association, Generalization, etc.) Diagram routing data

Parameters:

  • connector (Object)

    UML connector

  • diagram_link (Lutaml::Uml::DiagramLink, nil) (defaults to: nil)

Returns:

  • (Hash)

    Complete resolved style



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ea/diagram/style_resolver.rb', line 78

def resolve_connector_style(connector, diagram_link = nil) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  # Determine connector type
  connector_type = determine_connector_type(connector)

  style = {}

  # Start with configuration defaults for this connector type
  style[:arrow_type] =
    configuration.connector_style(connector_type, "arrow.type")
  style[:arrow_size] =
    configuration.connector_style(connector_type, "arrow.size")
  style[:stroke] =
    configuration.connector_style(connector_type, "line.stroke")
  style[:stroke_width] =
    configuration.connector_style(connector_type, "line.stroke_width")
  style[:stroke_dasharray] =
    configuration.connector_style(connector_type,
                                  "line.stroke_dasharray")
  style[:fill] =
    configuration.connector_style(connector_type, "line.fill") || "none"

  # Override with EA data if present (highest priority)
  if diagram_link&.style
    ea_style = parse_diagram_link_style(diagram_link.style)
    style.merge!(ea_style)
  end

  style.compact
end

#resolve_element_style(element, diagram_object = nil) ⇒ Hash

Resolve complete style for an element

Diagram placement data

Parameters:

  • element (Object)

    UML element (Class, DataType, etc.)

  • diagram_object (Lutaml::Uml::DiagramObject, nil) (defaults to: nil)

Returns:

  • (Hash)

    Complete resolved style



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ea/diagram/style_resolver.rb', line 33

def resolve_element_style(element, diagram_object = nil) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  style = {}

  # Start with configuration defaults
  style[:fill] = configuration.style_for(element, "colors.fill")
  style[:stroke] = configuration.style_for(element, "colors.stroke")
  style[:stroke_width] =
    configuration.style_for(element, "box.stroke_width")
  style[:stroke_linecap] =
    configuration.style_for(element, "box.stroke_linecap")
  style[:stroke_linejoin] =
    configuration.style_for(element, "box.stroke_linejoin")
  style[:corner_radius] =
    configuration.style_for(element, "box.corner_radius")
  style[:fill_opacity] =
    configuration.style_for(element, "box.fill_opacity")
  style[:stroke_opacity] =
    configuration.style_for(element, "box.stroke_opacity")

  # Font configuration
  style[:font_family] =
    configuration.style_for(element, "fonts.class_name.family")
  style[:font_size] =
    configuration.style_for(element, "fonts.class_name.size")
  style[:font_weight] =
    configuration.style_for(element, "fonts.class_name.weight")
  style[:font_style] =
    configuration.style_for(element, "fonts.class_name.style")

  # Override with EA data if present (highest priority)
  if diagram_object&.style
    ea_style = parse_diagram_object_style(diagram_object.style)
    style.merge!(ea_style)
  end

  style.compact
end

#resolve_fill_color(element, diagram_object = nil) ⇒ String

Resolve fill color specifically

Diagram placement data

Parameters:

  • element (Object)

    UML element

  • diagram_object (Lutaml::Uml::DiagramObject, nil) (defaults to: nil)

Returns:

  • (String)

    Resolved fill color



114
115
116
117
118
119
120
121
122
123
# File 'lib/ea/diagram/style_resolver.rb', line 114

def resolve_fill_color(element, diagram_object = nil)
  # Priority 1: EA data from DiagramObject.style
  if diagram_object&.style
    ea_style = parse_diagram_object_style(diagram_object.style)
    return ea_style[:fill] if ea_style[:fill]
  end

  # Priority 2: Configuration (Class > Package > Stereotype > Defaults)
  configuration.style_for(element, "colors.fill")
end

#resolve_font(element, context = :class_name) ⇒ Hash

Resolve font properties

(:class_name, :attribute, :operation, :stereotype)

Parameters:

  • element (Object)

    UML element

  • context (Symbol) (defaults to: :class_name)

    Font context

Returns:

  • (Hash)

    Font properties (family, size, weight, style)



148
149
150
151
152
153
154
155
# File 'lib/ea/diagram/style_resolver.rb', line 148

def resolve_font(element, context = :class_name)
  {
    family: configuration.style_for(element, "fonts.#{context}.family"),
    size: configuration.style_for(element, "fonts.#{context}.size"),
    weight: configuration.style_for(element, "fonts.#{context}.weight"),
    style: configuration.style_for(element, "fonts.#{context}.style"),
  }.compact
end

#resolve_stroke_color(element, diagram_object = nil) ⇒ String

Resolve stroke color specifically

Diagram placement data

Parameters:

  • element (Object)

    UML element

  • diagram_object (Lutaml::Uml::DiagramObject, nil) (defaults to: nil)

Returns:

  • (String)

    Resolved stroke color



131
132
133
134
135
136
137
138
139
140
# File 'lib/ea/diagram/style_resolver.rb', line 131

def resolve_stroke_color(element, diagram_object = nil)
  # Priority 1: EA data from DiagramObject.style
  if diagram_object&.style
    ea_style = parse_diagram_object_style(diagram_object.style)
    return ea_style[:stroke] if ea_style[:stroke]
  end

  # Priority 2: Configuration
  configuration.style_for(element, "colors.stroke")
end