lib/ruote/exp/ro_attributes.rb in ruote-2.2.0 vs lib/ruote/exp/ro_attributes.rb in ruote-2.3.0

- old
+ new

@@ -1,7 +1,7 @@ #-- -# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com +# Copyright (c) 2005-2012, 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 @@ -58,11 +58,11 @@ v = if v == nil default elsif escape v else - @context.dollar_sub.s(v, self, workitem) + dsub(v, workitem) end v = v.to_s if v and string v @@ -70,24 +70,16 @@ # Returns the value for attribute 'key', this value should be present # in the array list 'values'. If not, the default value is returned. # By default, the default value is the first element of 'values'. # - def att(key, values, opts={}) + def att(keys, values, opts={}) default = opts[:default] || values.first - val = attribute(key) - val = val.to_s if val + val = Array(keys).collect { |key| attribute(key) }.compact.first.to_s - #raise( - # ArgumentError.new("attribute '#{key}' missing in #{tree}") - #) if opts[:mandatory] && val == nil - #raise( - # ArgumentError.new("attribute '#{key}' has invalid value in #{tree}") - #) if opts[:enforce] && (not values.include?(val)) - values.include?(val) ? val : default end # prefix = 'on' => will lookup on, on_val, on_value, on_v, on_var, # on_variable, on_f, on_fld, on_field... @@ -113,31 +105,15 @@ # Returns a Hash containing all attributes set for an expression with # their values resolved. # def compile_atts(opts={}) - attributes.keys.inject({}) { |r, k| - r[k] = attribute(k, h.applied_workitem, opts) - r + attributes.keys.each_with_object({}) { |k, r| + r[dsub(k)] = attribute(k, h.applied_workitem, opts) } end - # Like compile_atts, but the keys are expanded as well. - # - # Useful for things like - # - # set "f:${v:field_name}" => "${v:that_variable}" - # - def expand_atts(opts={}) - - attributes.keys.inject({}) { |r, k| - kk = @context.dollar_sub.s(k, self, h.applied_workitem) - r[kk] = attribute(k, h.applied_workitem, opts) - r - } - end - # Given something like # # sequence do # participant 'alpha' # end @@ -152,19 +128,55 @@ # def attribute_text(workitem=h.applied_workitem) text = attributes.keys.find { |k| attributes[k] == nil } - @context.dollar_sub.s(text.to_s, self, workitem) + dsub(text.to_s, workitem) end + # Equivalent to #attribute_text, but will return nil if there + # is no attribute whose values is nil. + # + def att_text(workitem=h.applied_workitem) + + text = attributes.keys.find { |k| attributes[k] == nil } + + text ? dsub(text.to_s, workitem) : nil + end + protected + # dollar substitution for expressions. + # + def dsub(o, wi=h.applied_workitem) + + case o + when String; @context.dollar_sub.s(o, self, wi) + when Array; o.collect { |e| dsub(e, wi) } + when Hash; o.remap { |(k, v), h| h[dsub(k, wi)] = dsub(v, wi) } + else o + end + end + + # 'tos' meaning 'many "to"' + # def determine_tos - [ attribute(:to_v) || attribute(:to_var) || attribute(:to_variable), - attribute(:to_f) || attribute(:to_fld) || attribute(:to_field) ] + to_v = attribute(:to_v) || attribute(:to_var) || attribute(:to_variable) + to_f = attribute(:to_f) || attribute(:to_fld) || attribute(:to_field) + + if to = attribute(:to) + pre, key = to.split(':') + pre, key = [ 'f', pre ] if key == nil + if pre.match(/^f/) + to_f = key + else + to_v = key + end + end + + [ to_v, to_f ] end # Val and Value (Sense and Sensibility ?) # VV = %w[ val value ] @@ -186,12 +198,10 @@ lookup_variable(k) elsif k = has_att(*flds) k = attribute(k, h.applied_workitem, att_options) - h.applied_workitem['fields'][k] - - # TODO : what about leveraging workitem#lookup ? + Ruote.lookup(h.applied_workitem['fields'], k) else nil end