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.



92
93
94
95
96
97
98
99
# File 'lib/gantty.rb', line 92

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

Instance Attribute Details

#resourcesObject

Returns the value of attribute resources.



70
71
72
# File 'lib/gantty.rb', line 70

def resources
  @resources
end

#tasksObject

Returns the value of attribute tasks.



70
71
72
# File 'lib/gantty.rb', line 70

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 }


161
162
163
164
165
166
167
168
169
170
# File 'lib/gantty.rb', line 161

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

#company(set = nil) ⇒ Object



72
73
74
75
# File 'lib/gantty.rb', line 72

def company(set = nil)
  return @company unless set
  @company = set
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)


141
142
143
# File 'lib/gantty.rb', line 141

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

#details(set = nil) ⇒ Object



82
83
84
85
# File 'lib/gantty.rb', line 82

def details set = nil
  return @details unless set
  @details = set
end

#name(set = nil) ⇒ Object



87
88
89
90
# File 'lib/gantty.rb', line 87

def name set = nil
  return @name unless set
  @name = set
end

#parse_ganttObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/gantty.rb', line 107

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(name, &block) ⇒ Object



101
102
103
104
105
# File 'lib/gantty.rb', line 101

def task(name, &block)
  task = Task.new(name)
  task.instance_eval(&block)
  @tasks << task      
end

#task_by_id(id) ⇒ Object

Find a task by its GanttProject id field.



173
174
175
# File 'lib/gantty.rb', line 173

def task_by_id id
  task_where :id => id
end

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



145
146
147
148
149
150
151
152
153
154
# File 'lib/gantty.rb', line 145

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



273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/gantty.rb', line 273

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.



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
235
236
237
238
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
# File 'lib/gantty.rb', line 178

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 << <<-XML
        <taskproperty id="tpd0" name="type" type="default" valuetype="icon"/>
        <taskproperty id="tpd1" name="priority" type="default" valuetype="icon"/>
        <taskproperty id="tpd2" name="info" type="default" valuetype="icon"/>
        <taskproperty id="tpd3" name="name" type="default" valuetype="text"/>
        <taskproperty id="tpd4" name="begindate" type="default" valuetype="date"/>
        <taskproperty id="tpd5" name="enddate" type="default" valuetype="date"/>
        <taskproperty id="tpd6" name="duration" type="default" valuetype="int"/>
        <taskproperty id="tpd7" name="completion" type="default" valuetype="int"/>
        <taskproperty id="tpd8" name="coordinator" type="default" valuetype="text"/>
        <taskproperty id="tpd9" name="predecessorsr" type="default" valuetype="text"/>
        XML
      end
      
      tasks.each do |t|
        task_xml x, t, dependencies
      end
      
    end
          
    x.resources do
      resources.each do |resource|
        x << resource.to_xml
      end
    end
    
    x.allocations do
      all_tasks.each do |task|
        task.xml_coordinator_properties.each do |coordinator|
          x.allocation(coordinator)
        end
      end
    end
    
    x << <<-XML
      <vacations/>
      <taskdisplaycolumns>
        <displaycolumn property-id="tpd3" order="0" width="172"/>
        <displaycolumn property-id="tpd4" order="1" width="27"/>
        <displaycolumn property-id="tpd5" order="2" width="26"/>
        <displaycolumn property-id="tpd8" order="3" width="75"/>
      </taskdisplaycolumns>
      <previous/>
      <roles roleset-name="Default"/>        
    XML
    
  end
  
end

#website(set = nil) ⇒ Object



77
78
79
80
# File 'lib/gantty.rb', line 77

def website set = nil
  return @website unless set
  @website = set
end