Class: Jekyll::Archimate::ApplicationInteractionMatrixTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll/archimate/application_interaction_matrix_tag.rb

Overview

Insert a diagram from the ArchiMate model.

{% application_interaction_matrix plateau:"Today" | caption: "Today's Application Interaction" }

Constant Summary collapse

EMPTY_CELL =
"<td></td>"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ ApplicationInteractionMatrixTag



17
18
19
20
21
22
23
# File 'lib/jekyll/archimate/application_interaction_matrix_tag.rb', line 17

def initialize(tag_name, markup, tokens)
  @markup = markup
  @context = nil
  @caption = nil
  @plateau = nil
  super
end

Instance Attribute Details

#captionObject (readonly)

Returns the value of attribute caption.



14
15
16
# File 'lib/jekyll/archimate/application_interaction_matrix_tag.rb', line 14

def caption
  @caption
end

#contextObject (readonly)

Returns the value of attribute context.



12
13
14
# File 'lib/jekyll/archimate/application_interaction_matrix_tag.rb', line 12

def context
  @context
end

#markupObject (readonly)

Returns the value of attribute markup.



13
14
15
# File 'lib/jekyll/archimate/application_interaction_matrix_tag.rb', line 13

def markup
  @markup
end

#plateauObject (readonly)

Returns the value of attribute plateau.



15
16
17
# File 'lib/jekyll/archimate/application_interaction_matrix_tag.rb', line 15

def plateau
  @plateau
end

Instance Method Details

#application_interactionObject

Here I want all of the Serving relationships between 2 app components included or derived.

attrs: source_selector: Element selector for source elements target_selector: Element selector for target elements relationship_selector



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
70
71
72
# File 'lib/jekyll/archimate/application_interaction_matrix_tag.rb', line 39

def application_interaction
  model = ArchimateCache.instance.model
  dr_engine = ::Archimate::DerivedRelations.new(model)

  relationship_filter = ->(rel) { rel.weight >= ::Archimate::DataModel::Serving::WEIGHT }

  plateau_today = dr_engine.element_by_name(plateau)
  today_rels = model.relationships.select do |rel|
    rel.source.id == plateau_today.id &&
      %w[CompositionRelationship AggregationRelationship].include?(rel.type) &&
      rel.target.type == "ApplicationComponent"
  end
  today_apps = today_rels.map(&:target)
  target_filter = ->(el) { today_apps.map(&:id).include?(el.id) }
  stop_filter = ->(el) { el.type == "ApplicationComponent" }

  concrete_rels = model.relationships.select do |rel|
    rel.type == "ServingRelationship" &&
      today_apps.include?(rel.source.id) &&
      today_apps.include?(rel.target.id)
  end

  derived_rels = dr_engine.derived_relations(
    today_apps,
    relationship_filter,
    target_filter,
    stop_filter
  )

  @all_rels = [concrete_rels, derived_rels].flatten

  @callers = @all_rels.map(&:source).uniq.sort { |a, b| a.name.to_s <=> b.name.to_s }
  @callees = @all_rels.map(&:target).uniq.sort { |a, b| a.name.to_s <=> b.name.to_s }
end

#cell_content(caller, callee) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/jekyll/archimate/application_interaction_matrix_tag.rb', line 107

def cell_content(caller, callee)
  rels = @all_rels.select { |rel| rel.source == caller && rel.target == callee }
  return EMPTY_CELL if rels.empty?
  derived = rels.all?(&:derived)
  span_class = derived ? "text-danger" : "text-primary"
  tooltip = "#{caller.name} &rarr; #{callee.name} #{'(derived)' if derived}"
  "    <td>\n    <a href=\"#\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"\#{tooltip}\">\n    <span class=\"\#{span_class}\">&crarr; calls</span>\n    </a>\n    </td>\n  TABLE_CELL\nend\n"

#render(context) ⇒ Object



25
26
27
28
29
30
# File 'lib/jekyll/archimate/application_interaction_matrix_tag.rb', line 25

def render(context)
  @context = context
  scan_attributes(context)
  application_interaction
  render_table
end

#render_rowsObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/jekyll/archimate/application_interaction_matrix_tag.rb', line 95

def render_rows
  return "<tr><td>No Items</td></tr>" if @callees.empty?
  @callees.map do |callee|
    "      <tr>\n      <th class=\"info\" scope=\"row\">\#{callee.name}</th>\n      \#{@callers.map { |caller| cell_content(caller, callee) }.join('')}\n      </tr>\n    TABLE_ROW\n  end.join(\"\")\nend\n"

#render_tableObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/jekyll/archimate/application_interaction_matrix_tag.rb', line 74

def render_table
  "    <table class=\"table table-condensed table-hover table-striped\">\n    <caption>\#{caption}</caption>\n    <thead>\n    <tr>\n    <th>&nbsp;</th>\n    <th class=\"success\" scope=\"col\" colspan=\"\#{@callers.size}\">Callers</th>\n    </tr>\n    <tr>\n    <th class=\"info\" scope=\"col\">Callees</th>\n    \#{@callers.map { |ac| \"<th class=\\\"success\\\" scope=\\\"col\\\" style=\\\"text-transform: capitalize\\\">\#{ac.name}</th>\" }.join(\"\\n\")}\n    </tr>\n    </thead>\n    <tbody>\n    \#{render_rows.strip}\n    </tbody>\n    </table>\n  TABLE\nend\n"

#scan_attributes(context) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/jekyll/archimate/application_interaction_matrix_tag.rb', line 122

def scan_attributes(context)
  # Render any liquid variables
  markup = Liquid::Template.parse(@markup).render(context)

  # Extract tag attributes
  attributes = {}
  markup.scan(Liquid::TagAttributes) do |key, value|
    attributes[key] = value
  end
  @caption = attributes['caption']&.gsub!(/\A"|"\Z/, '')
  @plateau = attributes['plateau']&.gsub!(/\A"|"\Z/, '')
end

#siteObject



135
136
137
# File 'lib/jekyll/archimate/application_interaction_matrix_tag.rb', line 135

def site
  @site ||= context.registers[:site]
end