Class: Markdoc::Sequence::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/markdoc/sequence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Message

Returns a new instance of Message.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/markdoc/sequence.rb', line 122

def initialize(args)
  self.options = []
  self.label = args[:label].strip
  self.comment = args[:comment].strip

  self.op = args[:op]
  self.row = args[:row]
  # ui
  self.offset  = args[:ui][:offset]
  self.color   = args[:ui][:color]
  self.font    = args[:ui][:font]
  self.size    = args[:ui][:size]
  self.spacing = args[:ui][:spacing]
  self.line    = args[:ui][:line]
  self.dash    = args[:ui][:dash]

  if op.index('~')
    options << %(stroke-dasharray="#{dash}")
  elsif op.index('-').nil?
    raise 'Message direction must be one of ->, ~>, <-, <~'
  end

  if op.index('>')
    self.source = args[:role1]
    self.dest = args[:role2]
  elsif op.index('<')
    self.source = args[:role2]
    self.dest = args[:role1]
  else
    raise 'Message direction must be one of ->, ~>, <-, <~'
  end

  source.messages << self
  dest.messages << self unless source.eql?(dest)
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def color
  @color
end

#commentObject

Returns the value of attribute comment.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def comment
  @comment
end

#dashObject

Returns the value of attribute dash.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def dash
  @dash
end

#destObject

Returns the value of attribute dest.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def dest
  @dest
end

#fontObject

Returns the value of attribute font.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def font
  @font
end

#labelObject

Returns the value of attribute label.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def label
  @label
end

#lineObject

Returns the value of attribute line.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def line
  @line
end

#offsetObject

Returns the value of attribute offset.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def offset
  @offset
end

#opObject

Returns the value of attribute op.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def op
  @op
end

#optionsObject

Returns the value of attribute options.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def options
  @options
end

#rowObject

Returns the value of attribute row.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def row
  @row
end

#sizeObject

Returns the value of attribute size.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def size
  @size
end

#sourceObject

Returns the value of attribute source.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def source
  @source
end

#spacingObject

Returns the value of attribute spacing.



118
119
120
# File 'lib/markdoc/sequence.rb', line 118

def spacing
  @spacing
end

Instance Method Details



158
159
160
161
162
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
196
197
# File 'lib/markdoc/sequence.rb', line 158

def print
  role1, role2 = *(source < dest ? [source, dest] : [dest, source])
  elements = []

  x1 = role1.center
  if role1.eql?(role2)
    y1 = y
    x2 = x1 + 50
    y2 = y1 + spacing

    elements << %(<polyline points="#{x1},#{y1} #{x2},#{y1} #{x2},#{y2} #{x1 + 5},#{y2}" fill="none" stroke-width="2" stroke-linejoin="round" stroke="#{color}" #{options.join ' '}/>)
    elements << %(<polygon points="#{x1 + 10},#{y2 - 5} #{x1},#{y2} #{x1 + 10},#{y2 + 5}" fill="#{color}"/>)
    elements << %(<text x="#{x1 + 10}" y="#{y1 - 5}" font-family="#{font}" font-size="#{size}" fill="#{color}">#{label}</text>)
  else
    x2 = role2.center

    if role1 == source
      x2 -= 10
      elements << %(<polygon points="#{x2},#{y - 5} #{x2 + 10},#{y} #{x2},#{y + 5}" fill="#{color}"/>)
    else
      x1 += 10
      elements << %(<polygon points="#{x1},#{y - 5} #{x1 - 10},#{y} #{x1},#{y + 5}" fill="#{color}"/>)
    end

    elements << %(<line x1="#{x1}" y1="#{y}" x2="#{x2}" y2="#{y}" stroke="#{color}" stroke-width="2" #{options.join ' '}/>)
    elements << %(<text x="#{x1 + 40}" y="#{y - 5}" font-family="#{font}" font-size="#{size}" fill="#{color}">#{label}</text>)

    if comment.size.positive?
      x = role2.prev.center + 15
      elements << %(<path fill="#eeeeee" d="M#{x2 - 30},#{y + 1} L#{x2 - 30},#{y + 10} H#{x} V#{y + 2 * spacing - 25} H#{x2} V#{y + 10} H#{x2 - 20} z" />)
      elements << %(<text x="#{x + 5}" y="#{y + 23}" font-family="#{font}" font-size="#{size}" fill="#{color}">)
      split(comment).each_with_index do |line, i|
        elements << %(<tspan x="#{x + 5}" y="#{y + 23 + 13 * i}">#{line}</tspan>)
      end
      elements << '</text>'
    end
  end

  elements.join("\n")
end

#yObject



199
200
201
# File 'lib/markdoc/sequence.rb', line 199

def y
  offset + row * spacing
end