Class: Powirb::Workitem
- Inherits:
-
Object
- Object
- Powirb::Workitem
- Defined in:
- lib/powirb/workitem.rb
Overview
This class represent a workitem instance, that is serialized in a XML file under Polarion subversion repository (for us - more conveniently - a working copy of it).
- Author
-
Carlo Pecchia ([email protected])
- Copyright
-
Copyright © 2011 Carlo Pecchia
- License
-
See LICENSE file
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Instance Method Summary collapse
-
#[](fname) ⇒ Object
Return a field value.
- #[]=(f, v) ⇒ Object
-
#add_link(lh) ⇒ Object
Add a link to another workitem with specified role.
-
#fields ⇒ Object
Return a list with all field names.
-
#initialize(filename) ⇒ Workitem
constructor
Create a new instance of a workitem belonging to its representation in the standard way Polarion does it.
-
#links ⇒ Object
Return the list of linked workitems [‘role1:wid1’, …, ‘roleN:widN’].
-
#save! ⇒ Object
Save workitem on filesystem.
-
#space ⇒ Object
Return the “space” under which the workitem lives (tracker xor document).
-
#to_xml ⇒ Object
Return XML content.
-
#wid ⇒ Object
Return workitem ID.
Constructor Details
#initialize(filename) ⇒ Workitem
Create a new instance of a workitem belonging to its representation in the standard way Polarion does it
17 18 19 20 21 22 23 24 25 |
# File 'lib/powirb/workitem.rb', line 17 def initialize(filename) @filename = filename Powirb.log.debug("Retrieving workitem from #{@filename}") begin @doc = Nokogiri::XML(open(@filename)) rescue Exception => e Powirb.log.error(e) end end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
13 14 15 |
# File 'lib/powirb/workitem.rb', line 13 def filename @filename end |
Instance Method Details
#[](fname) ⇒ Object
Return a field value
28 29 30 31 32 33 |
# File 'lib/powirb/workitem.rb', line 28 def [](fname) fname = fname.to_s node = @doc.xpath("//field[@id=\"#{fname}\"]") return nil if node.text.empty? node.text end |
#[]=(f, v) ⇒ Object
Set/remove a field, optionally is possible to specify a ‘type’ eg: wi = nil -> remove the field ‘foo’
wi[:foo] = 'bar' -> add/update the field 'foo'
wi[:foo] = {:value => 'bar', :type => 'enum:foo'} -> the same as above
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 71 72 73 74 75 76 77 |
# File 'lib/powirb/workitem.rb', line 39 def []=(f,v) f = f.to_s # with a null value we remove and existing field if v.nil? # Delete Powirb.log.debug("[#{wid}] removing field '#{f}'") @doc.xpath("//field[@id=\"#{f}\"]").last.remove return end # assert: v is defined (String or Hash) # retrieve the 'value' and the optional 'type' if v.instance_of?(Hash) value = v[:value] type = v[:type] else value = v type = nil end # assert: 'value' and 'type' are defined if self[f].nil? # Create: the field is not already present Powirb.log.debug("[#{wid}] adding new field '#{f}' with value '#{value}'") e = Nokogiri::XML::Node.new('field', @doc) e['id'] = f e['type'] = type unless type.nil? e.content = value # we are sure the 'type' field always exists for any workitem, so attach after that @doc.xpath('//field[@id="type"]').last.add_next_sibling(e) else # Update: the field is already present Powirb.log.debug("[#{wid}] updating existing field '#{f}' with value '#{value}'") e = @doc.xpath("//field[@id=\"#{f}\"]").last e['type'] = type unless type.nil? e.content = value end end |
#add_link(lh) ⇒ Object
Add a link to another workitem with specified role
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/powirb/workitem.rb', line 92 def add_link(lh) lnk_wid = lh[:wid] lnk_role = lh[:role] # find or create the attach node if @doc.xpath('//field[@id="linkedWorkItems"]/list').last.nil? Nokogiri::XML::Builder.with(@doc.xpath('//work-item').last) do field(:id => 'linkedWorkItems') { list {} } end end # build and attach the link struct Nokogiri::XML::Builder.with(@doc.xpath('//field[@id="linkedWorkItems"]/list').last) do struct { item(:id => 'revision') item(:id => 'workItem') { text lnk_wid } item(:id => 'role') { text lnk_role } } end end |
#fields ⇒ Object
Return a list with all field names
136 137 138 |
# File 'lib/powirb/workitem.rb', line 136 def fields @doc.xpath("//field").map{|node| node['id']}.sort end |
#links ⇒ Object
Return the list of linked workitems [‘role1:wid1’, …, ‘roleN:widN’]
81 82 83 84 85 86 87 88 89 |
# File 'lib/powirb/workitem.rb', line 81 def links tmp = [] @doc.xpath('//field[@id="linkedWorkItems"]/list/struct').each do |struct| linked_wid = struct.xpath('item[@id="workItem"]').text role = struct.xpath('item[@id="role"]').text tmp << "#{role}:#{linked_wid}" end return tmp end |
#save! ⇒ Object
Save workitem on filesystem
120 121 122 123 |
# File 'lib/powirb/workitem.rb', line 120 def save! Powirb.log.debug("[#{wid}] saving on #{@filename}") File.open(@filename, 'w+') {|io| io.puts @doc} end |
#space ⇒ Object
Return the “space” under which the workitem lives (tracker xor document)
141 142 143 144 145 146 147 |
# File 'lib/powirb/workitem.rb', line 141 def space if @filename.include?('/.polarion/tracker/') return 'tracker' else File.dirname(@filename).split(/\//)[4] end end |
#to_xml ⇒ Object
Return XML content
131 132 133 |
# File 'lib/powirb/workitem.rb', line 131 def to_xml @doc.to_xml end |
#wid ⇒ Object
Return workitem ID
126 127 128 |
# File 'lib/powirb/workitem.rb', line 126 def wid File.basename(File.dirname(@filename)) end |