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.



222
223
224
225
226
227
228
229
230
231
232
233
# 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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



220
221
222
# File 'lib/markdoc/sequence.rb', line 220

def attributes
  @attributes
end

#inputObject

Returns the value of attribute input.



220
221
222
# File 'lib/markdoc/sequence.rb', line 220

def input
  @input
end

#messagesObject

Returns the value of attribute messages.



220
221
222
# File 'lib/markdoc/sequence.rb', line 220

def messages
  @messages
end

#outputObject

Returns the value of attribute output.



220
221
222
# File 'lib/markdoc/sequence.rb', line 220

def output
  @output
end

#rolesObject

Returns the value of attribute roles.



220
221
222
# File 'lib/markdoc/sequence.rb', line 220

def roles
  @roles
end

#rowsObject

Returns the value of attribute rows.



220
221
222
# File 'lib/markdoc/sequence.rb', line 220

def rows
  @rows
end

Instance Method Details

#find(id) ⇒ Object



235
236
237
# File 'lib/markdoc/sequence.rb', line 235

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

#heightObject



300
301
302
303
304
# File 'lib/markdoc/sequence.rb', line 300

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

#parseObject



239
240
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
# 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


292
293
294
295
296
297
298
# File 'lib/markdoc/sequence.rb', line 292

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

#widthObject



306
307
308
309
# File 'lib/markdoc/sequence.rb', line 306

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