lib/ruote/workitem.rb in ruote-2.1.11 vs lib/ruote/workitem.rb in ruote-2.2.0

- old
+ new

@@ -1,7 +1,7 @@ #-- -# Copyright (c) 2005-2010, John Mettraux, jmettraux@gmail.com +# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -37,22 +37,22 @@ # class Workitem attr_reader :h - def initialize (h) + def initialize(h) @h = h class << @h; include Ruote::HashDot; end #class << @h['fields'] # alias_method :__get, :[] # alias_method :__set, :[]= - # def [] (key) + # def [](key) # __get(key.to_s) # end - # def []= (key, value) + # def []=(key, value) # __set(key.to_s, value) # end #end # indifferent access, not activated for now end @@ -116,11 +116,11 @@ # Sets all the fields in one sweep. # # Remember : the fields must be a JSONifiable hash. # - def fields= (fields) + def fields=(fields) @h['fields'] = fields end # A shortcut to the value in the field named __result__ @@ -135,11 +135,11 @@ # Sets the value of the 'special' field __result__ # # See #result # - def result= (r) + def result=(r) fields['__result__'] = r end # When was this workitem dispatched ? @@ -149,11 +149,11 @@ fields['dispatched_at'] end # Warning : equality is based on fei and not on payload ! # - def == (other) + def ==(other) return false if other.class != self.class self.h['fei'] == other.h['fei'] end @@ -180,11 +180,11 @@ # # is equivalent to # # workitem.fields['toto']['address'] # - def lookup (key, container_lookup=false) + def lookup(key, container_lookup=false) Ruote.lookup(@h['fields'], key, container_lookup) end # 'lf' for 'lookup field' @@ -198,11 +198,11 @@ # # Warning : if the customer and address field and subfield are not present # 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) + def set_field(key, value) Ruote.set(@h['fields'], key, value) end # Shortcut for wi.fields['__timed_out__'] @@ -252,15 +252,68 @@ # Shortcut for wi.fields['__command__'] = x # # __command__ is read by the 'cursor' and the 'iterator' expressions # when a workitem reaches it (apply and reply). # - def command= (com) + def command=(com) com = com.is_a?(Array) ? com : com.split(' ') - com[1] = com[1].to_i if com[1] + com = [ com.first, com.last ] + com[1] = com[1].to_i if com[1] and com[0] != 'jump' @h['fields']['__command__'] = com + end + + # Shortcut for wi.fields['__tags__'] + # + def tags + + @h['fields']['__tags__'] || [] + end + + # Used by FlowExpression when entering a tag. + # + def self.add_tag(hworkitem, tag) + + (hworkitem['fields']['__tags__'] ||= []) << tag + end + + # Used by FlowExpression when leaving a tag. + # + def self.remove_tag(hworkitem, tag) + + # it's a bit convoluted... trying to cope with potential inconsistencies + # + # normally, it should only be a tags.pop(), but since user have + # access to the workitem and its fields... better be safe than sorry + + tags = (hworkitem['fields']['__tags__'] || []) + + if index = tags.rindex(tag) + tags.delete_at(index) + end + end + + # Encodes this workitem as JSON. If pretty is set to true, will output + # prettified JSON. + # + def as_json(pretty=false) + + pretty ? Rufus::Json.pretty_encode(@h) : Rufus::Json.encode(@h) + end + + # Given a JSON String, decodes and returns a Ruote::Workitem instance.3 + # If the decode thing is not an object/hash, will raise an ArgumentError. + # + def self.from_json(json) + + h = Rufus::Json.decode(json) + + raise ArgumentError( + "Arg not a JSON hash/object, but a #{h.class}. Cannot create workitem" + ) unless h.is_a?(Hash) + + self.new(h) end end end