Class: Markdoc::Sequence::Diagram

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, options = {}) ⇒ Diagram

Returns a new instance of Diagram.



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/markdoc/sequence.rb', line 224

def initialize(input, options = {})
  self.input = input
  self.output = []
  self.roles = []
  self.messages = []
  self.rows = 0
  self.attributes = DEFAULTS.dup

  options.each do |key, value|
    attributes.merge!(key => value)
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



222
223
224
# File 'lib/markdoc/sequence.rb', line 222

def attributes
  @attributes
end

#inputObject

Returns the value of attribute input.



222
223
224
# File 'lib/markdoc/sequence.rb', line 222

def input
  @input
end

#messagesObject

Returns the value of attribute messages.



222
223
224
# File 'lib/markdoc/sequence.rb', line 222

def messages
  @messages
end

#outputObject

Returns the value of attribute output.



222
223
224
# File 'lib/markdoc/sequence.rb', line 222

def output
  @output
end

#rolesObject

Returns the value of attribute roles.



222
223
224
# File 'lib/markdoc/sequence.rb', line 222

def roles
  @roles
end

#rowsObject

Returns the value of attribute rows.



222
223
224
# File 'lib/markdoc/sequence.rb', line 222

def rows
  @rows
end

Instance Method Details

#find(id) ⇒ Object



237
238
239
# File 'lib/markdoc/sequence.rb', line 237

def find(id)
  roles.detect { |role| role.id == id.strip } or raise("Non-declared role: #{id}")
end

#heightObject



298
299
300
301
302
# File 'lib/markdoc/sequence.rb', line 298

def height
  attributes[:message][:offset] +
    attributes[:message][:spacing] * rows +
    attributes[:diagram][:offsety] * 2
end

#parseObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/markdoc/sequence.rb', line 241

def parse
  input.split("\n").each do |line|
    next if line.strip.empty?

    if (matches = line.match(/^(?<id>[a-zA-Z0-9_ \t]+) *= *(?<label>[a-zA-Z0-9_ \t]+)/))
      # User = Actor
      roles << Role.new(
        id: matches[:id],
        label: matches[:label],
        column: roles.size,
        diagram: attributes[:diagram],
        ui: attributes[:role]
      )
    elsif (matches = line.match(/^(?<role1>[a-zA-Z0-9_ \t]+) *(?<op>[<\-~>]{2}) *(?<role2>[a-zA-Z0-9_ \t]+):(?<label>[^#]+)#?(?<comment>.*)/))
      # User -> Web : Login
      messages << Message.new(
        role1: find(matches[:role1]),
        role2: find(matches[:role2]),
        row: rows,
        op: matches[:op],
        label: matches[:label],
        comment: matches[:comment],
        diagram: attributes[:diagram],
        ui: attributes[:message]
      )
      self.rows += 1
      # comment takes 1 more row
      self.rows += 1 if matches[:comment].length.positive?
    elsif (matches = line.match(/^(?<role>[a-zA-Z0-9_ \t]+) *:(?<label>[^#]+)#?(?<comment>.*)/))
      # Web : Save the form
      messages << Message.new(
        role1: find(matches[:role]),
        role2: find(matches[:role]),
        row: rows,
        op: '->',
        label: matches[:label],
        comment: matches[:comment],
        diagram: attributes[:diagram],
        ui: attributes[:message]
      )
      # self message takes 2 rows
      self.rows += 2
    end
  end

  prev = nil
  roles.each do |succ|
    succ.prev = prev
    prev.succ = succ if prev
    prev = succ
  end
end


294
295
296
# File 'lib/markdoc/sequence.rb', line 294

def print
  format(template, width: width, height: height, content: (roles + messages).map(&:print).join("\n"))
end

#widthObject



304
305
306
307
# File 'lib/markdoc/sequence.rb', line 304

def width
  (attributes[:role][:spacing] + attributes[:role][:width]) * roles.size +
    attributes[:diagram][:offsetx]
end