class Markdoc::Sequence::Diagram

Attributes

attributes[RW]
input[RW]
messages[RW]
output[RW]
roles[RW]
rows[RW]

Public Class Methods

new(input, options = {}) click to toggle source
# File lib/markdoc/sequence.rb, line 222
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

Public Instance Methods

find(id) click to toggle source
# File lib/markdoc/sequence.rb, line 235
def find(id)
  roles.detect{|role| role.id == id.strip} or raise("Non-declared role: #{id}")
end
height() click to toggle source
# File lib/markdoc/sequence.rb, line 300
def height
  attributes[:message][:offset] +
    attributes[:message][:spacing] * rows +
    attributes[:diagram][:offsety] * 2
end
parse() click to toggle source
# File lib/markdoc/sequence.rb, line 239
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 > 0
    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
print() click to toggle source
width() click to toggle source
# File lib/markdoc/sequence.rb, line 306
def width
  (attributes[:role][:spacing] + attributes[:role][:width]) * roles.size +
    attributes[:diagram][:offsetx]
end

Private Instance Methods

template() click to toggle source
# File lib/markdoc/sequence.rb, line 313
def template
  '<svg xmlns="http://www.w3.org/2000/svg" '+
    'xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" '+
    'width="%{width}" height="%{height}" '+
    'viewBox="0 0 %{width} %{height}">'+
    '%{content}</svg>'
end