class Mio::Model::Workflow

Public Instance Methods

create_hash() click to toggle source
# File lib/mio/model/workflow.rb, line 13
def create_hash
  {name: @args.name,
   visibilityIds: @args.visibility}
end
go() click to toggle source
# File lib/mio/model/workflow.rb, line 66
def go
  if @args.nodes.empty? or @args.transitions.empty?
    raise Mio::Model::EmptyField, 'Field nodes to Mio::Model::Workflow must contain at least one node and one transition'
  end

  @width = 900
  @height = 1024
  @icon_width = 120
  @icon_height = 45
  @spacing = 100

  unless look_up
    @object = create
  else
    @object = look_up
    set_start :stop
  end
  @structure_path = "#{self.class.resource_name}/#{@object['id']}/structure"

  laid_out_nodes = set_node_layouts
  # Create structure minus transitions
  structure = @client.update @structure_path, structure_hash(laid_out_nodes)

  # Create structre with transitions
  @client.update @structure_path, structure_hash(laid_out_nodes,  transition_lookup(structure))

  set_enable
  return @object
end
normalize_node(n) click to toggle source
# File lib/mio/model/workflow.rb, line 18
def normalize_node n
  {
    id: n['id'],
    name: n['name'],
    path: n['path']
  }
end
set_node_layouts() click to toggle source
# File lib/mio/model/workflow.rb, line 26
def set_node_layouts
  x = @spacing
  y = @spacing

  @args.nodes.map do |n|
    n[:layout] = {
      width: @icon_width,
      height: @icon_height,
      x: x,
      y: y
    }
    x += (@icon_width + @spacing)
    if x > @width - (@icon_width + @spacing)
      x = 0
      y += (@icon_height + @spacing)
    end
    n
  end
end
structure_hash(nodes, transitions=[]) click to toggle source
# File lib/mio/model/workflow.rb, line 59
def structure_hash nodes, transitions=[]
  {nodes: nodes,
   transitions: transitions,
   width: @width,
   height: @height}
end
transition_lookup(structure) click to toggle source
# File lib/mio/model/workflow.rb, line 46
def transition_lookup structure
  @args.transitions.map do |t|
    from = structure['nodes'].find{|n| n['name'] == t[:from]}
    to = structure['nodes'].find{|n| n['name'] == t[:to]}

    raise Mio::Model::DataValueError, "#{t[:from]} to #{t[:to]} is an invalid transition" if from.nil? or to.nil?

    { name: t[:name],
      from: normalize_node(from),
      to: normalize_node(to)}
  end
end