| Class | OpenWFE::RawExpression::Parameter |
| In: |
lib/openwfe/expressions/raw.rb
|
| Parent: | Object |
Encapsulating
<parameter field="x" default="y" type="z" match="m" />
Somehow I have that : OpenWFEru is not a strongly typed language … Anyway I implemented that to please Pat.
# File lib/openwfe/expressions/raw.rb, line 431
431: def initialize (field, match, default, type)
432:
433: @field = to_s field
434: @match = to_s match
435: @default = to_s default
436: @type = to_s type
437: end
Will raise an exception if this param requirement is not met by the workitem.
# File lib/openwfe/expressions/raw.rb, line 443
443: def check (workitem)
444:
445: unless @field
446: raise \
447: OpenWFE::ParameterException,
448: "'parameter'/'param' without a 'field' attribute"
449: end
450:
451: field_value = workitem.attributes[@field]
452: field_value = @default unless field_value
453:
454: unless field_value
455: raise \
456: OpenWFE::ParameterException,
457: "field '#{@field}' is missing" \
458: end
459:
460: check_match(field_value)
461:
462: enforce_type(workitem, field_value)
463: end
# File lib/openwfe/expressions/raw.rb, line 497
497: def check_match (value)
498:
499: return unless @match
500:
501: unless value.to_s.match(@match)
502: raise \
503: OpenWFE::ParameterException,
504: "value of field '#{@field}' doesn't match"
505: end
506: end
Will raise an exception if it cannot coerce the type of the value to the one desired.
# File lib/openwfe/expressions/raw.rb, line 479
479: def enforce_type (workitem, value)
480:
481: value = if not @type
482: value
483: elsif @type == "string"
484: value.to_s
485: elsif @type == "int" or @type == "integer"
486: Integer(value)
487: elsif @type == "float"
488: Float(value)
489: else
490: raise
491: "unknown type '#{@type}' for field '#{@field}'"
492: end
493:
494: workitem.attributes[@field] = value
495: end