Class: Gantty::GanttChart

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ GanttChart

Returns a new instance of GanttChart.



60
61
62
63
64
65
66
67
68
# File 'lib/gantty.rb', line 60

def initialize file = nil
  @name = nil
  @gantt = nil
  @resources, @tasks = [], []
  if file
    @gantt = Nokogiri::XML(open(File.expand_path(file)))
    parse_gantt
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



58
59
60
# File 'lib/gantty.rb', line 58

def name
  @name
end

#resourcesObject

Returns the value of attribute resources.



58
59
60
# File 'lib/gantty.rb', line 58

def resources
  @resources
end

#tasksObject

Returns the value of attribute tasks.



58
59
60
# File 'lib/gantty.rb', line 58

def tasks
  @tasks
end

Instance Method Details

#all_tasks(task_array = @tasks) ⇒ Object

Returns a flattened list of tasks in the GanttChart for iteration. Since tasks is a tree, a user can use Array#select on all_tasks instead.

my_tasks = @gantt.all_tasks.select { |t| t.coordinators.include? me }


124
125
126
127
128
129
130
131
132
133
# File 'lib/gantty.rb', line 124

def all_tasks task_array = @tasks
  found_tasks = []
  task_array.each do |task|
    found_tasks << task
    res = all_tasks task.tasks
    found_tasks << res
  end
  return [] if found_tasks.size == 0
  return found_tasks.flatten.uniq
end

#current_tasks(date = Date.today) ⇒ Object

Display all current tasks. A current task is defined as a task that has either started today, ended today, or begins before today and ends after today.

tasks = @gantt.current_tasks

Specify the parameter date to see current tasks for that date, instead of today.

tasks = @gantt.current_tasks Date.new(2008,1,6)


104
105
106
# File 'lib/gantty.rb', line 104

def current_tasks date = Date.today
  all_tasks.select { |t| t.start <= date and t.end >= date }
end

#parse_ganttObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gantty.rb', line 70

def parse_gantt
  depends = { 1 => [], 2 => [], 3 => [], 4 => [] }
  @gantt.xpath('//resource').each { |r| @resources << Resource.new(r) }
  @gantt.xpath('/project/tasks/task').each do |t| 
    created_task = Task.new(t) 
    add_subtasks_to_task t, created_task
    @tasks << created_task
  end
  
  @gantt.xpath('//task').each do |t|
    t.children.select { |c| c.name == "depend" }.each do |dependency|
      depends[dependency.attributes["type"].to_i] << [t.attributes["id"].to_i, dependency.attributes["id"].to_i]
    end
  end
  
  all_tasks.each do |task| 
    @gantt.xpath("//allocation[@task-id=#{task.id}]").each do |alloc| 
      task.coordinators += @resources.select { |r| r.id == alloc.attributes["resource-id"].to_i } 
    end 
  end
  
  depends.each do |type,queue|
    queue.each { |relation| task_by_id(relation.last).send(DEPENDS[type], task_by_id(relation.first)) }
  end      
end

#task_by_id(id) ⇒ Object

Find a task by its GanttProject id field.



136
137
138
# File 'lib/gantty.rb', line 136

def task_by_id id
  task_where :id => id
end

#task_where(cond, task_array = @tasks) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/gantty.rb', line 108

def task_where cond, task_array = @tasks
  task_array.each do |task|
    found_task = true
    cond.each { |property,value| found_task = false unless task.instance_variable_get("@#{property.to_s}") == value }
    return task if found_task
    res = task_where cond, task.tasks
    return res if res
  end
  return nil
end

#task_xml(xml, task, dependencies) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/gantty.rb', line 236

def task_xml xml, task, dependencies
  xml.task(task.xml_properties) do
    xml.notes { xml.cdata! task.notes } if task.notes
    dependencies.each do |depend_type, values|
      if values.include? task.id
       values[task.id].each do |dependency|
        xml.depend( :id => dependency, :type => REVERSE_DEPENDS[depend_type], :difference => 0, :hardness => "Strong" )
       end
      end
    end
    task.tasks.each { |t| task_xml xml, t, dependencies }
  end
end

#to_xmlObject

Generate the GanttProject XML for this GanttChart.



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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/gantty.rb', line 141

def to_xml
  task_id = 0
  resource_id = 0
  
  @resources.each do |resource|
    resource.instance_variable_set(:@id, resource_id += 1) unless resource.id
  end
  
  dependencies = { :starts_with => {}, :starts_after => {}, :finishes_with => {}, :finishes_before => {} }
  all_tasks.each do |task|
    task.instance_variable_set(:@id, task_id += 1) unless task.id
    dependencies.each do |type,set|
      task.send(type).each do |other_task|
        set[other_task.id] = [] unless set[other_task.id]
        set[other_task.id] << task.id
      end
    end
  end
  
  x = Builder::XmlMarkup.new :indent => 2
  x.instruct! :xml, :version => '1.0', :encoding => 'UTF-8'
  xml = x.project( :name => @name) do
    
    x.description ''
    x.view( 'zooming-state' => 'default:7', 'id' => 'gantt-chart' )
    x.view( 'id' => 'resource-table' ) do
      x.field( :id => 0, :name => "Name", :width => 55, :order => 0)
      x.field( :id => 1, :name => "Default role", :width => 44, :order => 1)
    end
    x.comment! ' '
    
    x.calendars do
      x.tag! "day-types" do
        x.tag! "day-type", :id => 0
        x.tag! "day-type", :id => 1
        x.calendar( :id => 1, :name => :default) do
          x.tag! "default-week", :sun => 0, :mon => 0, :tue => 0, :wed => 0, :thu => 0, :fri => 0, :sat => 0
          x.tag! "overriden-day-types"
          x.days
        end
      end
    end
    
    x.tasks( :color => "#8cb6ce" ) do
      x.taskproperties do 
        x << "        <taskproperty id=\"tpd0\" name=\"type\" type=\"default\" valuetype=\"icon\"/>\n        <taskproperty id=\"tpd1\" name=\"priority\" type=\"default\" valuetype=\"icon\"/>\n        <taskproperty id=\"tpd2\" name=\"info\" type=\"default\" valuetype=\"icon\"/>\n        <taskproperty id=\"tpd3\" name=\"name\" type=\"default\" valuetype=\"text\"/>\n        <taskproperty id=\"tpd4\" name=\"begindate\" type=\"default\" valuetype=\"date\"/>\n        <taskproperty id=\"tpd5\" name=\"enddate\" type=\"default\" valuetype=\"date\"/>\n        <taskproperty id=\"tpd6\" name=\"duration\" type=\"default\" valuetype=\"int\"/>\n        <taskproperty id=\"tpd7\" name=\"completion\" type=\"default\" valuetype=\"int\"/>\n        <taskproperty id=\"tpd8\" name=\"coordinator\" type=\"default\" valuetype=\"text\"/>\n        <taskproperty id=\"tpd9\" name=\"predecessorsr\" type=\"default\" valuetype=\"text\"/>\n        XML\n      end\n      \n      tasks.each do |t|\n        task_xml x, t, dependencies\n      end\n      \n    end\n          \n    x.resources do\n      resources.each do |resource|\n        x << resource.to_xml\n      end\n    end\n    \n    x.allocations do\n      all_tasks.each do |task|\n        task.xml_coordinator_properties.each do |coordinator|\n          x.allocation(coordinator)\n        end\n      end\n    end\n    \n    x << <<-XML\n      <vacations/>\n      <taskdisplaycolumns>\n        <displaycolumn property-id=\"tpd3\" order=\"0\" width=\"172\"/>\n        <displaycolumn property-id=\"tpd4\" order=\"1\" width=\"27\"/>\n        <displaycolumn property-id=\"tpd5\" order=\"2\" width=\"26\"/>\n        <displaycolumn property-id=\"tpd8\" order=\"3\" width=\"75\"/>\n      </taskdisplaycolumns>\n      <previous/>\n      <roles roleset-name=\"Default\"/>        \n    XML\n    \n  end\n  \nend\n"