class Rtml::Widgets::ScreenVariableProcessing < Rtml::Widget include Rtml::TmlizedConditions affects :screen entry_point :set # Sets one or more TML variables to specified values. # Examples: # screen :main do # set :counter => 'tmlvar:counter + 1', # :amount => 100 # end # def set(assignments) variables = parent.document.variable_definitions(:all).collect { |c| c[:name] } format = assignments.delete(:format) assignments.each do |key, value| if variables.include?(key.to_s) options = options_for(value_or_condition(value)).merge(:name => key, :format => format) #options.each { |key, value| options.delete(key) if value.nil? } build :setvar, validate_options(options) else raise Rtml::Errors::InvalidOptionError, "Variable named #{key} is not defined" end end end private def validate_options(hash) hash.each do |key, value| if key == :lo || key == :ro case value when Numeric, String, Symbol, TrueClass, FalseClass, NilClass else raise ArgumentError, "Incompatible type: #{value}" end end end hash end def options_for(value_or_array) if value_or_array.kind_of? Array value_or_array = tmlize_condition(*value_or_array) { :lo => value_or_array[0], :op => value_or_array[1], :ro => value_or_array[2] } else { :lo => value_or_array } end end def value_or_condition(value) if value.kind_of? String operands = value.split if operands.length == 3 operands else value end else value end end end