lib/ruote/workitem.rb in ruote-2.1.6 vs lib/ruote/workitem.rb in ruote-2.1.7
- old
+ new
@@ -27,10 +27,16 @@
require 'ruote/util/hashdot'
module Ruote
+ #
+ # A workitem can be thought of an "execution token", but with a payload
+ # (fields).
+ #
+ # The payload/fields MUST be JSONifiable.
+ #
class Workitem
attr_reader :h
def initialize (h)
@@ -42,31 +48,45 @@
def to_h
@h
end
+ # Returns a Ruote::FlowExpressionId instance.
+ #
def fei
FlowExpressionId.new(h.fei)
end
+ # Returns a complete copy of this workitem.
+ #
def dup
- Ruote.fulldup(self)
+ Workitem.new(Rufus::Json.dup(@h))
end
+ # The participant for which this item is destined. Will be nil when
+ # the workitem is transiting inside of its process instance (as opposed
+ # to when it's being delivered outside of the engine).
+ #
def participant_name
@h['participant_name']
end
+ # Returns the payload, ie the fields hash.
+ #
def fields
@h['fields']
end
- def fields=( fields )
+ # Sets all the fields in one sweep.
+ #
+ # Remember : the fields must be a JSONifiable hash.
+ #
+ def fields= (fields)
@h['fields'] = fields
end
# A shortcut to the value in the field named __result__
@@ -85,29 +105,26 @@
#
def result= (r)
fields['__result__'] = r
end
- end
- #
- # TODO : clean me out !
- #
- class BakWorkitem
+ # Warning : equality is based on fei and not on payload !
+ #
+ def == (other)
- attr_accessor :fei
- attr_accessor :fields
- attr_accessor :participant_name
+ return false if other.class != self.class
+ self.h['fei'] == other.h['fei']
+ end
- alias :f :fields
- alias :attributes :fields
- alias :attributes= :fields=
+ alias eql? ==
- def initialize (fields={})
+ # Warning : hash is fei's hash.
+ #
+ def hash
- @fei = nil
- @fields = fields
+ self.h['fei'].hash
end
# For a simple key
#
# workitem.lookup('toto')
@@ -124,11 +141,11 @@
#
# workitem.fields['toto']['address']
#
def lookup (key, container_lookup=false)
- Ruote.lookup(@fields, key, container_lookup)
+ Ruote.lookup(@h['fields'], key, container_lookup)
end
# 'lf' for 'lookup field'
#
alias :lf :lookup
@@ -142,40 +159,10 @@
# or are not hashes, set_field will simply create a "customer.address.city"
# field and set its value to "Pleasantville".
#
def set_field (key, value)
- Ruote.set(@fields, key, value)
- end
-
- # Returns a deep copy of this workitem instance.
- #
- def dup
-
- Ruote.fulldup(self)
- end
-
- # Turns a workitem into a Ruby Hash (useful for JSON serializations)
- #
- def to_h
-
- h = {}
- h['fei'] = @fei.to_h
- h['participant_name'] = @participant_name
- h['fields'] = @fields
-
- h
- end
-
- # Turns back a Ruby Hash into a workitem (well, attempts to)
- #
- def self.from_h (h)
-
- wi = Workitem.new(h['fields'])
- wi.fei = FlowExpressionId.from_h(h['fei'])
- wi.participant_name = h['participant_name']
-
- wi
+ Ruote.set(@h['fields'], key, value)
end
end
end