Class: Place::WorkItem

Inherits:
Ohm::Model
  • Object
show all
Defined in:
lib/place/work_item.rb

Overview

Each WorkItem has a work-item id (wid) and an url that refers to its main xml file (workitem.xml) located under its own directory (that terminates with its Polarion ID = our wid).

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.collect_all(base_url) ⇒ Object

Collect all workitems (wid and url) under a given project url



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/place/work_item.rb', line 27

def self.collect_all(base_url)
  Dir.glob(base_url + '/tracker/workitems/**/workitem.xml').each do |filename|
 wi = WorkItem.create(
:wid	=> File.dirname(filename).split(/\//).last,
:url	=> filename,
:fields => "{}"
 )
 project_prefix = wi.wid.split(/\-/)[0]
 wi.project = Project.find_by_prefix(project_prefix)
 Place.logger.info("collected workitem at #{wi.url}")
 wi.save	  
	end
end

.find_by_type(type) ⇒ Object

Returns all workitems of the given type



119
120
121
122
# File 'lib/place/work_item.rb', line 119

def self.find_by_type(type)
  #self.all.select{|wi| wi.type == type.to_s}
	self.find(:type => type.to_s)
end

.find_by_wid(wid) ⇒ Object

Returns a workitem by its wid, nil if not present



113
114
115
116
# File 'lib/place/work_item.rb', line 113

def self.find_by_wid(wid)
  wis = self.find(:wid => wid)
	wis.nil? ? nil : wis[0]
end

Instance Method Details

#[](fname) ⇒ Object

Get value from specified field, nil if not present



101
102
103
# File 'lib/place/work_item.rb', line 101

def [](fname)
  JSON.parse(fields)[fname.to_s]
end

#[]=(fname, fvalue) ⇒ Object

Set value for specified field



106
107
108
109
110
# File 'lib/place/work_item.rb', line 106

def []=(fname, fvalue)
  h = JSON.parse(self.fields)
  h[fname.to_s] = fvalue
	self.fields = h.to_json
end

#field_namesObject

Returns all the fields name for the workitem



143
144
145
# File 'lib/place/work_item.rb', line 143

def field_names
  JSON.parse(self.fields).keys.sort
end

Returns all links to the current workitems from others, eventually only with a specified role



134
135
136
137
138
139
140
# File 'lib/place/work_item.rb', line 134

def links_in(role=nil)
	if role.nil?
 Link.find(:to_wid => self.wid)
	else
 Link.find(:to_wid => self.wid, :role => role.to_s)
	end
end

Returns all links from the current workitems to others, eventually only with a specified role



125
126
127
128
129
130
131
# File 'lib/place/work_item.rb', line 125

def links_out(role=nil)    
	if role.nil?
 Link.find(:from_wid => self.wid)
	else
 Link.find(:from_wid => self.wid, :role => role.to_s)
	end
end

#retrieveObject

Retrieve a specific workitem (based on wid and url only)



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/place/work_item.rb', line 42

def retrieve
  content = IO.readlines(url).join('')
	doc = Nokogiri::XML(content)
 
  doc.xpath('//work-item/field').each do |field|
    self[field.attributes['id'].to_s] = field.content
  end

  self.type = self[:type]
	
	self.author = User.find_by_name(self[:author])

	unless self[:assignee].nil?
 self[:assignee].split.each do |name|
   u = User.find_by_name(name)
unless u.nil?
     self.assignees << u
  u.assignments << self  
end
 end
	end

	self.timepoint = TimePoint.find_by_tid(self['timePoint'])
	
	%w(initialEstimate timeSpent remainingEstimate).each do |t|
 self[t] = to_hours(self[t])
	end
	
	# links
  doc.xpath('/work-item/field[@id="linkedWorkItems"]/list/struct').each do |struct|
 l = Link.create(
:role		=> struct.xpath('item[@id="role"]').text,
:suspect	=> struct.xpath('item[@id="suspect"]').text == 'true' ? true : false,
:revision	=> struct.xpath('item[@id="revision"]').empty? ? false : true,
:from		=> self,
:to			=> WorkItem.find_by_wid(struct.xpath('item[@id="workItem"]').text),
:from_wid	=> self.wid,
:to_wid		=> struct.xpath('item[@id="workItem"]').text
)
	end
	
	# comments
	Dir.glob(File.dirname(url) + '/comment-*.xml').each do |filename|
 c = Comment.create(:url => filename)
 c.retrieve
	end
	
	# workrecords
	Dir.glob(File.dirname(url) + '/workrecord-*.xml').each do |filename|
 wr = WorkRecord.create(:url => filename)
 wr.retrieve
	end
	
	Place.logger.info("retrieved workitem #{self.wid}")	
	
	self.save
end

#total_on(field) ⇒ Object

Returns the total sum, for a given field, from descendant and workitem itself Note: descendants = children with parent role



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/place/work_item.rb', line 149

def total_on(field)
  # return a past computed value if present
	return self["_total_on_#{field}"]  unless self["_total_on_#{field}"].nil?

  # computing the total
	total = self[field].nil? ? 0 : self[field]
	children.each{|c| total += c.total_on(field) }
	
	# saving (momoizing) for future invocations
	self["_total_on_#{field}"] = total.to_f
	self.save
	
	total.to_f
end