Class: OpenWFE::WorkItem
- Inherits:
-
Object
- Object
- OpenWFE::WorkItem
- Defined in:
- lib/openwfe/workitem.rb,
lib/openwfe/orest/worklistclient.rb
Overview
Reopening WorkItem to set the MAP_TYPE (as it’s used by OpenWFEja…)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
(also: #fields)
Returns the value of attribute attributes.
-
#last_modified ⇒ Object
Returns the value of attribute last_modified.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
A shortcut for .
-
#[]=(key, value) ⇒ Object
A shortcut for .
-
#dup ⇒ Object
Produces a deep copy of the workitem.
-
#has_attribute?(key) ⇒ Boolean
(also: #has_field?)
The partner to the lookup_attribute() method.
-
#initialize ⇒ WorkItem
constructor
A new instance of WorkItem.
-
#lookup_attribute(key) ⇒ Object
(also: #lookup_field)
A smarter alternative to.
-
#method_missing(m, *args) ⇒ Object
In order to simplify code like :.
-
#set_attribute(key, value) ⇒ Object
(also: #set_field)
set_attribute() accomodates itself with nested key constructs.
- #to_h ⇒ Object
-
#touch ⇒ Object
Sets the last_modified field to now.
-
#unset_attribute(key) ⇒ Object
(also: #unset_field)
unset_attribute() accomodates itself with nested key constructs.
Constructor Details
#initialize ⇒ WorkItem
Returns a new instance of WorkItem.
63 64 65 66 |
# File 'lib/openwfe/workitem.rb', line 63 def initialize @last_modified = nil @attributes = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
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
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 |
# File 'lib/openwfe/workitem.rb', line 150 def method_missing (m, *args) methodname = m.to_s if args.length == 0 value = @attributes[methodname] return value if value raise "Missing attribute '#{methodname}' in workitem" end #if methodname == "[]" and args.length == 1 # value = @attributes[args[0]] # return value if value # raise "Missing attribute '#{methodname}' in workitem" #end #if methodname == "[]=" and args.length == 2 # return @attributes[args[0]] = args[1] #end if args.length == 1 and methodname[-1, 1] == "=" return @attributes[methodname[0..-2]] = args[0] end super(m, args) end |
Instance Attribute Details
#attributes ⇒ Object Also known as: fields
Returns the value of attribute attributes.
61 62 63 |
# File 'lib/openwfe/workitem.rb', line 61 def attributes @attributes end |
#last_modified ⇒ Object
Returns the value of attribute last_modified.
61 62 63 |
# File 'lib/openwfe/workitem.rb', line 61 def last_modified @last_modified end |
Class Method Details
Instance Method Details
#[](key) ⇒ Object
A shortcut for
workitem.attributes['key']
is
workitem['key']
(Note that
workitem.key
will raise an exception if there is no attribute key).
112 113 114 115 |
# File 'lib/openwfe/workitem.rb', line 112 def [] (key) @attributes[key] end |
#[]=(key, value) ⇒ Object
A shortcut for
workitem.attributes['key'] = value
is
workitem['key'] = value
126 127 128 129 |
# File 'lib/openwfe/workitem.rb', line 126 def []= (key, value) @attributes[key] = value end |
#dup ⇒ Object
Produces a deep copy of the workitem
179 180 181 |
# File 'lib/openwfe/workitem.rb', line 179 def dup OpenWFE::fulldup(self) end |
#has_attribute?(key) ⇒ Boolean Also known as: has_field?
The partner to the lookup_attribute() method. Behaves like it.
217 218 219 |
# File 'lib/openwfe/workitem.rb', line 217 def has_attribute? (key) OpenWFE.has_attribute?(@attributes, key) end |
#lookup_attribute(key) ⇒ Object Also known as: lookup_field
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"
210 211 212 |
# File 'lib/openwfe/workitem.rb', line 210 def lookup_attribute (key) OpenWFE.lookup_attribute(@attributes, key) end |
#set_attribute(key, value) ⇒ Object Also known as: set_field
set_attribute() accomodates itself with nested key constructs.
224 225 226 |
# File 'lib/openwfe/workitem.rb', line 224 def set_attribute (key, value) OpenWFE.set_attribute(@attributes, key, value) end |
#to_h ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/openwfe/workitem.rb', line 79 def to_h h = {} h[:type] = self.class.name h[:last_modified] = @last_modified h[:attributes] = @attributes h end |
#touch ⇒ Object
Sets the last_modified field to now
74 75 76 77 |
# File 'lib/openwfe/workitem.rb', line 74 def touch @last_modified = Time.now end |
#unset_attribute(key) ⇒ Object Also known as: unset_field
unset_attribute() accomodates itself with nested key constructs.
231 232 233 |
# File 'lib/openwfe/workitem.rb', line 231 def unset_attribute (key) OpenWFE.unset_attribute(@attributes, key) end |