Class: OFlow::Graffle

Inherits:
Object
  • Object
show all
Defined in:
lib/oflow/graffle.rb

Defined Under Namespace

Classes: Element, LineInfo, TaskInfo

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, filename, doc) ⇒ Graffle

Returns a new instance of Graffle.

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/oflow/graffle.rb', line 17

def initialize(env, filename, doc)
  @line_info = { } # key is id
  @task_info = { } # key is id
  @name = File.basename(filename, '.graffle')

  nodes = doc.locate('plist/dict')[0].nodes
  sheets = Graffle.get_key_value(nodes, 'Sheets')
  if sheets.nil?
	nodes = Graffle.get_key_value(nodes, 'GraphicsList')
  else
	nodes = Graffle.get_key_value(sheets[0].nodes, 'GraphicsList')
  end
  raise ConfigError.new("Empty flow.") if nodes.nil?
  
  nodes.each do |node|
    load_node(node)
  end

  # set label in lines
  task_info.each do |id,ti|
    next if ti.line_id.nil?
    if !(li = line_info[ti.line_id]).nil?
      li.label, li.op = ti.first_option
    end
  end
  task_info.each do |_, ti|
    if ti.name.nil? && !(n = ti.options[:flow]).nil?
      @name = n.strip
    end
  end
  env.flow(@name.to_sym) { |f|
    task_info.each_value { |ti|
      next unless ti.line_id.nil?
      next if ti.name.nil?
      c = ti.get_class()
      next if c.nil?
      f.task(ti.name, c, ti.options) { |t|
        t.bounds = ti.bounds
        t.color = ti.color
        t.shape = ti.shape
        line_info.each_value { |li|
          next unless li.tail == ti.id
          target_info = task_info[li.head]
          if target_info.nil?
            flow_name, task_name, op = li.op.split('/')
            t.link(li.label, task_name, op, flow_name)
          else
            t.link(li.label, target_info.name, li.op)
          end
        }
      }
    }
  }
end

Instance Attribute Details

#line_infoObject

Returns the value of attribute line_info.



8
9
10
# File 'lib/oflow/graffle.rb', line 8

def line_info
  @line_info
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/oflow/graffle.rb', line 7

def name
  @name
end

#task_infoObject

Returns the value of attribute task_info.



9
10
11
# File 'lib/oflow/graffle.rb', line 9

def task_info
  @task_info
end

Class Method Details

.get_key_value(nodes, key) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/oflow/graffle.rb', line 93

def self.get_key_value(nodes, key)
  return if nodes.nil?
  nodes.each_with_index do |node,i|
    next unless 'key' == node.name && key == node.text
    n = nodes[i + 1]
    if 'dict' == n.name || 'array' == n.name
      return n.nodes
    else
      return n.text
    end
  end
  nil
end

.get_text(nodes) ⇒ Object



107
108
109
110
# File 'lib/oflow/graffle.rb', line 107

def self.get_text(nodes)
  return nil if nodes.nil?
  strip_rtf(get_key_value(nodes, 'Text'))
end

.load(env, filename) ⇒ Object



11
12
13
14
15
# File 'lib/oflow/graffle.rb', line 11

def self.load(env, filename)
  doc = Ox.load_file(filename, mode: :generic)
  g = Graffle.new(env, filename, doc)
  env.debug(g.to_s()) if Logger::Severity::DEBUG >= ::OFlow::Env.log_level
end

.rtf_ctrl_char(ctrl) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/oflow/graffle.rb', line 172

def self.rtf_ctrl_char(ctrl)
  c = ''
  case ctrl
  when '', 'line'
    c = "\n"
  when 'tab'
    c = "\t"
  else
    if "'" == ctrl[0]
      c = ctrl[1..3].hex().chr
    end
  end
  c
end

.strip_rtf(rtf) ⇒ Object



112
113
114
115
116
117
118
119
120
121
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/oflow/graffle.rb', line 112

def self.strip_rtf(rtf)
  return rtf unless rtf.start_with?('{\rtf')
  depth = 0
  txt = ''
  ctrl = nil
  str = nil
  rtf.each_char do |c|
    case c
    when ' '
      if !str.nil?
        str << ' ' unless 1 < depth
      elsif !ctrl.nil?
        txt << rtf_ctrl_char(ctrl) unless 1 < depth
        ctrl = nil
        str = ''
      else
        str = ''
      end
    when "\n", "\r"
      if !str.nil? # should not happen but...
        str << "\n" unless 1 < depth
      elsif !ctrl.nil?
        txt << rtf_ctrl_char(ctrl) unless 1 < depth
        ctrl = nil
      end
    when '\\'
      if !str.nil?
        txt << str unless 1 < depth
        str = nil
      elsif !ctrl.nil?
        txt << rtf_ctrl_char(ctrl) unless 1 < depth
      end
      ctrl = ''
    when '{'
      if !ctrl.nil?
        txt << rtf_ctrl_char(ctrl) unless 1 < depth
        ctrl = nil
      end
      depth += 1
    when '}'
      if !ctrl.nil?
        txt << rtf_ctrl_char(ctrl) unless 1 < depth
        ctrl = nil
      end
      depth -= 1
    else
      if !ctrl.nil?
        ctrl << c
      elsif 1 < depth
        # ignore
      else
        str = '' if str.nil?
        str << c
      end
    end
  end
  txt << str unless str.nil?
  txt
end

Instance Method Details

#load_node(node) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/oflow/graffle.rb', line 72

def load_node(node)
  nodes = node.nodes
  case Graffle.get_key_value(nodes, 'Class')
  when 'LineGraphic'
    line = LineInfo.new(nodes)
    @line_info[line.id] = line
  when 'ShapedGraphic'
    task = TaskInfo.new(nodes)
    @task_info[task.id] = task unless task.nil?
  end
end

#to_sObject



84
85
86
87
88
89
90
91
# File 'lib/oflow/graffle.rb', line 84

def to_s()
  s = "Graffle{\n  name: #{@name}\n  tasks{\n"
  task_info.each { |_,ti| s += "    #{ti}\n" }
  s += "  }\n  lines{\n"
  line_info.each { |_,li| s += "    #{li}\n" }
  s += "  }\n}\n"
  s
end