Class | OpenWFE::WorkItem |
In: |
lib/openwfe/orest/worklistclient.rb
lib/openwfe/workitem.rb |
Parent: | Object |
The base class for all the workitems.
attributes | -> | fields |
attributes= | -> | fields= |
attributes | [RW] | |
last_modified | [RW] |
# File lib/openwfe/workitem.rb, line 88 88: def WorkItem.from_h (h) 89: 90: #wi = eval("#{h[:type]}.new") 91: wi = OpenWFE.get_class(h).new 92: wi.last_modified = h[:last_modified] 93: wi.attributes = h[:attributes] 94: wi 95: end
# File lib/openwfe/orest/worklistclient.rb, line 50 50: def initialize 51: @last_modified = nil 52: @attributes = {} 53: @attributes[MAP_TYPE] = E_SMAP 54: end
# File lib/openwfe/workitem.rb, line 63 63: def initialize 64: @last_modified = nil 65: @attributes = {} 66: end
A shortcut for
workitem.attributes['key']
is
workitem['key']
(Note that
workitem.key
will raise an exception if there is no attribute key).
# File lib/openwfe/workitem.rb, line 112 112: def [] (key) 113: 114: @attributes[key] 115: end
A shortcut for
workitem.attributes['key'] = value
is
workitem['key'] = value
# File lib/openwfe/workitem.rb, line 126 126: def []= (key, value) 127: 128: @attributes[key] = value 129: end
Produces a deep copy of the workitem
# File lib/openwfe/workitem.rb, line 179 179: def dup 180: OpenWFE::fulldup(self) 181: end
The partner to the lookup_attribute() method. Behaves like it.
# File lib/openwfe/workitem.rb, line 217 217: def has_attribute? (key) 218: OpenWFE.has_attribute?(@attributes, key) 219: end
A smarter alternative to
value = workitem.attributes[x]
Via this method, nested values can be reached. For example :
wi = InFlowWorkItem.new() wi.attributes = { "field0" => "value0", "field1" => [ 0, 1, 2, 3, [ "a", "b", "c" ]], "field2" => { "a" => "AA", "b" => "BB", "c" => [ "C0", "C1", "C3" ] }, "field3" => 3, "field99" => nil }
will verify the following assertions :
assert wi.lookup_attribute("field3") == 3 assert wi.lookup_attribute("field1.1") == 1 assert wi.lookup_attribute("field1.4.1") == "b" assert wi.lookup_attribute("field2.c.1") == "C1"
# File lib/openwfe/workitem.rb, line 210 210: def lookup_attribute (key) 211: OpenWFE.lookup_attribute(@attributes, key) 212: end
In order to simplify code like :
value = workitem.attributes['xyz']
to
value = workitem.xyz
or
value = workitem['xyz']
we overrode method_missing.
workitem.xyz = "my new value"
is also possible
# File lib/openwfe/workitem.rb, line 150 150: def method_missing (m, *args) 151: 152: methodname = m.to_s 153: 154: if args.length == 0 155: value = @attributes[methodname] 156: return value if value 157: raise "Missing attribute '#{methodname}' in workitem" 158: end 159: 160: #if methodname == "[]" and args.length == 1 161: # value = @attributes[args[0]] 162: # return value if value 163: # raise "Missing attribute '#{methodname}' in workitem" 164: #end 165: #if methodname == "[]=" and args.length == 2 166: # return @attributes[args[0]] = args[1] 167: #end 168: 169: if args.length == 1 and methodname[-1, 1] == "=" 170: return @attributes[methodname[0..-2]] = args[0] 171: end 172: 173: super(m, args) 174: end
set_attribute() accomodates itself with nested key constructs.
# File lib/openwfe/workitem.rb, line 224 224: def set_attribute (key, value) 225: OpenWFE.set_attribute(@attributes, key, value) 226: end
# File lib/openwfe/workitem.rb, line 79 79: def to_h 80: 81: h = {} 82: h[:type] = self.class.name 83: h[:last_modified] = @last_modified 84: h[:attributes] = @attributes 85: h 86: end
Sets the last_modified field to now
# File lib/openwfe/workitem.rb, line 74 74: def touch 75: 76: @last_modified = Time.now 77: end
unset_attribute() accomodates itself with nested key constructs.
# File lib/openwfe/workitem.rb, line 231 231: def unset_attribute (key) 232: OpenWFE.unset_attribute(@attributes, key) 233: end